Added username to User as per issue #9
parent
d84bde9b4d
commit
1ec0f43517
38
handlers.go
38
handlers.go
|
@ -41,12 +41,12 @@ func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request, _ httproute
|
||||||
// Insert
|
// Insert
|
||||||
var finalId string
|
var finalId string
|
||||||
err = h.db.QueryRow(`
|
err = h.db.QueryRow(`
|
||||||
INSERT INTO "user" (id, first_name, last_name, phone_number)
|
INSERT INTO "user" (id, username, first_name, last_name, phone_number)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
ON CONFLICT(phone_number)
|
ON CONFLICT(phone_number)
|
||||||
DO UPDATE SET phone_number=EXCLUDED.phone_number, first_name=$2, last_name=$3
|
DO UPDATE SET phone_number=EXCLUDED.phone_number, username=$2, first_name=$3, last_name=$4
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`, user.ID, user.FirstName, user.LastName, user.PhoneNumber).Scan(&finalId)
|
`, user.ID, user.Username, user.FirstName, user.LastName, user.PhoneNumber).Scan(&finalId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
|
@ -74,7 +74,7 @@ func (h *Handler) GetUsersByPhone(w http.ResponseWriter, r *http.Request, _ http
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
rows, err := h.db.Query(`
|
rows, err := h.db.Query(`
|
||||||
SELECT id, first_name, last_name FROM "user" WHERE phone_number = $1
|
SELECT id, username, first_name, last_name FROM "user" WHERE phone_number = $1
|
||||||
`, phone)
|
`, phone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -86,7 +86,7 @@ func (h *Handler) GetUsersByPhone(w http.ResponseWriter, r *http.Request, _ http
|
||||||
// Scan
|
// Scan
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
user := User{}
|
user := User{}
|
||||||
if err := rows.Scan(&user.ID, &user.FirstName, &user.LastName); err != nil {
|
if err := rows.Scan(&user.ID, &user.Username, &user.FirstName, &user.LastName); err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return
|
return
|
||||||
|
@ -108,8 +108,8 @@ func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.P
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
err := h.db.QueryRow(`
|
err := h.db.QueryRow(`
|
||||||
SELECT id, first_name, last_name, phone_number FROM "user" WHERE id = $1
|
SELECT id, username, first_name, last_name, phone_number FROM "user" WHERE id = $1
|
||||||
`, userID).Scan(&user.ID, &user.FirstName, &user.LastName, &user.PhoneNumber)
|
`, userID).Scan(&user.ID, &user.Username, &user.FirstName, &user.LastName, &user.PhoneNumber)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case err == sql.ErrNoRows:
|
case err == sql.ErrNoRows:
|
||||||
|
@ -410,7 +410,7 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
rows, err := h.db.Query(`
|
rows, err := h.db.Query(`
|
||||||
SELECT "user".id, "user".first_name, "user".last_name, "user".phone_number FROM "user"
|
SELECT "user".id, "user".username, "user".first_name, "user".last_name, "user".phone_number FROM "user"
|
||||||
INNER JOIN member m ON "user".id = m.user AND "user".id != $1
|
INNER JOIN member m ON "user".id = m.user AND "user".id != $1
|
||||||
INNER JOIN conversation ON "conversation".id = m.conversation
|
INNER JOIN conversation ON "conversation".id = m.conversation
|
||||||
INNER JOIN member
|
INNER JOIN member
|
||||||
|
@ -425,13 +425,13 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
|
||||||
|
|
||||||
// Scan
|
// Scan
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id, firstName, lastName, phoneNumber string
|
var id, username, firstName, lastName, phoneNumber string
|
||||||
if err := rows.Scan(&id, &firstName, &lastName, &phoneNumber); err != nil {
|
if err := rows.Scan(&id, &username, &firstName, &lastName, &phoneNumber); err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
users = append(users, User{ID: id, FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber})
|
users = append(users, User{ID: id, Username: username, FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond
|
// Respond
|
||||||
|
@ -466,8 +466,8 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
|
||||||
// Create contact if not exists, returning the id regardless
|
// Create contact if not exists, returning the id regardless
|
||||||
var contactId string
|
var contactId string
|
||||||
err = h.db.QueryRow(`
|
err = h.db.QueryRow(`
|
||||||
INSERT INTO "user" (id, first_name, last_name, phone_number)
|
INSERT INTO "user" (id, username, first_name, last_name, phone_number)
|
||||||
VALUES ($1, '', '', $2)
|
VALUES ($1, '', '', '', $2)
|
||||||
ON CONFLICT(phone_number)
|
ON CONFLICT(phone_number)
|
||||||
DO UPDATE SET phone_number=EXCLUDED.phone_number
|
DO UPDATE SET phone_number=EXCLUDED.phone_number
|
||||||
RETURNING id
|
RETURNING id
|
||||||
|
@ -480,7 +480,7 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
|
||||||
|
|
||||||
// Insert
|
// Insert
|
||||||
_, err = h.db.Exec(`
|
_, err = h.db.Exec(`
|
||||||
INSERT INTO contact ("user", contact) VALUES ($1, $2)
|
INSERT INTO contact ("user", contact) VALUES ($1, $2)
|
||||||
`, userID, contactId)
|
`, userID, contactId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
@ -503,7 +503,7 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
|
||||||
|
|
||||||
// Select
|
// Select
|
||||||
rows, err := h.db.Query(`
|
rows, err := h.db.Query(`
|
||||||
SELECT id, first_name, last_name, phone_number FROM "user"
|
SELECT id, username, first_name, last_name, phone_number FROM "user"
|
||||||
INNER JOIN contact
|
INNER JOIN contact
|
||||||
ON contact.contact = "user".id AND contact.user = $1
|
ON contact.contact = "user".id AND contact.user = $1
|
||||||
`, userID)
|
`, userID)
|
||||||
|
@ -516,13 +516,13 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
|
||||||
|
|
||||||
// Scan
|
// Scan
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id, firstName, lastName, phone string
|
var id, username, firstName, lastName, phone string
|
||||||
if err := rows.Scan(&id, &firstName, &lastName, &phone); err != nil {
|
if err := rows.Scan(&id, &username, &firstName, &lastName, &phone); err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
contacts = append(contacts, User{id, firstName, lastName, phone})
|
contacts = append(contacts, User{id, username, firstName, lastName, phone})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond
|
// Respond
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS "user" (
|
CREATE TABLE IF NOT EXISTS "user" (
|
||||||
id BYTEA PRIMARY KEY,
|
id BYTEA PRIMARY KEY,
|
||||||
|
username VARCHAR(63555) UNIQUE,
|
||||||
first_name VARCHAR(65535),
|
first_name VARCHAR(65535),
|
||||||
last_name VARCHAR(65535),
|
last_name VARCHAR(65535),
|
||||||
phone_number VARCHAR(32) UNIQUE
|
phone_number VARCHAR(32) UNIQUE
|
||||||
|
|
|
@ -1,34 +1,38 @@
|
||||||
INSERT INTO "user" (
|
INSERT INTO "user" (
|
||||||
id, first_name, last_name, phone_number
|
id, username, first_name, last_name, phone_number
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'u-7f48e2f2b6f7e4d1f9c864e48bc2b0f2',
|
'u-7f48e2f2b6f7e4d1f9c864e48bc2b0f2',
|
||||||
|
'ambc',
|
||||||
'Ambrose',
|
'Ambrose',
|
||||||
'Chua',
|
'Chua',
|
||||||
'+65 9766 3827'
|
'+65 9766 3827'
|
||||||
) ON CONFLICT DO NOTHING;
|
) ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
INSERT INTO "user" (
|
INSERT INTO "user" (
|
||||||
id, first_name, last_name, phone_number
|
id, username, first_name, last_name, phone_number
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'u-dc9537ca645ff34b4f289b6bd7aa08b7',
|
'u-dc9537ca645ff34b4f289b6bd7aa08b7',
|
||||||
|
'orcas',
|
||||||
'Daniel',
|
'Daniel',
|
||||||
'Lim',
|
'Lim',
|
||||||
'+65 8737 7117'
|
'+65 8737 7117'
|
||||||
) ON CONFLICT DO NOTHING;
|
) ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
INSERT INTO "user" (
|
INSERT INTO "user" (
|
||||||
id, first_name, last_name, phone_number
|
id, username, first_name, last_name, phone_number
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'u-23e608245d0866ea937f15876adb5ef6',
|
'u-23e608245d0866ea937f15876adb5ef6',
|
||||||
|
'it',
|
||||||
'Isaac',
|
'Isaac',
|
||||||
'Tay',
|
'Tay',
|
||||||
'+65 8181 6346'
|
'+65 8181 6346'
|
||||||
) ON CONFLICT DO NOTHING;
|
) ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
INSERT INTO "user" (
|
INSERT INTO "user" (
|
||||||
id, first_name, last_name, phone_number
|
id, username, first_name, last_name, phone_number
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'u-fb91825f564a3cc110f11836fedea6f4',
|
'u-fb91825f564a3cc110f11836fedea6f4',
|
||||||
|
'solderneer',
|
||||||
'Sudharshan',
|
'Sudharshan',
|
||||||
'',
|
'',
|
||||||
'+65 8143 8417'
|
'+65 8143 8417'
|
||||||
|
|
1
types.go
1
types.go
|
@ -7,6 +7,7 @@ type Conversation struct {
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID string `json:"id"` // id
|
ID string `json:"id"` // id
|
||||||
|
Username string `json:"username"` // username
|
||||||
FirstName string `json:"first_name"` // first_name
|
FirstName string `json:"first_name"` // first_name
|
||||||
LastName string `json:"last_name"` // last_name
|
LastName string `json:"last_name"` // last_name
|
||||||
PhoneNumber string `json:"phone_number"` // phone_number
|
PhoneNumber string `json:"phone_number"` // phone_number
|
||||||
|
|
Loading…
Reference in New Issue