4
1
Fork 0

Added profile_pic field as per issue #10

pull/24/head
Daniel Lim 2019-06-21 08:58:01 +08:00
parent 3bb821003c
commit cfc9ea0672
5 changed files with 31 additions and 25 deletions

BIN
core

Binary file not shown.

View File

@ -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, username, bio, first_name, last_name, phone_number) INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number)
VALUES ($1, $2, $3, $4, $5, $6) VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT(phone_number) ON CONFLICT(phone_number)
DO UPDATE SET phone_number=EXCLUDED.phone_number, username=$2, first_name=$3, last_name=$4 DO UPDATE SET phone_number=EXCLUDED.phone_number, username=$2, first_name=$5, last_name=$6
RETURNING id RETURNING id
`, user.ID, user.Username, user.Bio, user.FirstName, user.LastName, user.PhoneNumber).Scan(&finalId) `, user.ID, user.Username, user.Bio, user.ProfilePic, 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, username, bio, first_name, last_name FROM "user" WHERE phone_number = $1 SELECT id, username, bio, profile_pic, 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.Username, &user.Bio, &user.FirstName, &user.LastName); err != nil { if err := rows.Scan(&user.ID, &user.Username, &user.Bio, &user.ProfilePic, &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, username, bio, first_name, last_name, phone_number FROM "user" WHERE id = $1 SELECT id, username, bio, profile_pic, first_name, last_name, phone_number FROM "user" WHERE id = $1
`, userID).Scan(&user.ID, &user.Username, &user.Bio, &user.FirstName, &user.LastName, &user.PhoneNumber) `, userID).Scan(&user.ID, &user.Username, &user.Bio, &user.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch { switch {
case err == sql.ErrNoRows: case err == sql.ErrNoRows:
@ -135,8 +135,8 @@ func (h *Handler) GetUserByUsername(w http.ResponseWriter, r *http.Request, p ht
// Select // Select
err := h.db.QueryRow(` err := h.db.QueryRow(`
SELECT id, username, bio, first_name, last_name, phone_number FROM "user" WHERE username = $1 SELECT id, username, bio, profile_pic, first_name, last_name, phone_number FROM "user" WHERE username = $1
`, username).Scan(&user.ID, &user.Username, &user.Bio, &user.FirstName, &user.LastName, &user.PhoneNumber) `, username).Scan(&user.ID, &user.Username, &user.Bio, &user.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch { switch {
case err == sql.ErrNoRows: case err == sql.ErrNoRows:
@ -445,7 +445,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".username, "user".bio, "user".first_name, "user".last_name, "user".phone_number FROM "user" SELECT "user".id, "user".username, "user".bio, "user".profile_pic, "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
@ -460,13 +460,13 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
// Scan // Scan
for rows.Next() { for rows.Next() {
var id, username, bio, firstName, lastName, phoneNumber string var id, username, bio, profilePic, firstName, lastName, phoneNumber string
if err := rows.Scan(&id, &username, &bio, &firstName, &lastName, &phoneNumber); err != nil { if err := rows.Scan(&id, &username, &bio, &profilePic, &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, Username: username, Bio: bio, FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber}) users = append(users, User{ID: id, Username: username, Bio: bio, ProfilePic: profilePic, FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber})
} }
// Respond // Respond
@ -501,8 +501,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, username, bio, first_name, last_name, phone_number) INSERT INTO "user" (id, username, bio, profile_pic, 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
@ -538,7 +538,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, username, bio, first_name, last_name, phone_number FROM "user" SELECT id, username, bio, profile_pic, 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)
@ -551,13 +551,13 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
// Scan // Scan
for rows.Next() { for rows.Next() {
var id, username, bio, firstName, lastName, phone string var id, username, bio, profilePic, firstName, lastName, phone string
if err := rows.Scan(&id, &username, &bio, &firstName, &lastName, &phone); err != nil { if err := rows.Scan(&id, &username, &bio, &profilePic, &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, username, bio, firstName, lastName, phone}) contacts = append(contacts, User{id, username, bio, profilePic, firstName, lastName, phone})
} }
// Respond // Respond

View File

@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS "user" (
id BYTEA PRIMARY KEY, id BYTEA PRIMARY KEY,
username VARCHAR(63555) UNIQUE, username VARCHAR(63555) UNIQUE,
bio VARCHAR(63535), bio VARCHAR(63535),
profile_pic VARCHAR(63535),
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

View File

@ -1,42 +1,46 @@
INSERT INTO "user" ( INSERT INTO "user" (
id, username, bio, first_name, last_name, phone_number id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES ( ) VALUES (
'u-7f48e2f2b6f7e4d1f9c864e48bc2b0f2', 'u-7f48e2f2b6f7e4d1f9c864e48bc2b0f2',
'ambc', '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, username, bio, first_name, last_name, phone_number id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES ( ) VALUES (
'u-dc9537ca645ff34b4f289b6bd7aa08b7', 'u-dc9537ca645ff34b4f289b6bd7aa08b7',
'orcas', '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, username, bio, first_name, last_name, phone_number id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES ( ) VALUES (
'u-23e608245d0866ea937f15876adb5ef6', 'u-23e608245d0866ea937f15876adb5ef6',
'it', '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, username, bio, first_name, last_name, phone_number id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES ( ) VALUES (
'u-fb91825f564a3cc110f11836fedea6f4', 'u-fb91825f564a3cc110f11836fedea6f4',
'solderneer', 'solderneer',
'', '',
'',
'Sudharshan', 'Sudharshan',
'', '',
'+65 8143 8417' '+65 8143 8417'

View File

@ -8,7 +8,8 @@ type Conversation struct {
type User struct { type User struct {
ID string `json:"id"` // id ID string `json:"id"` // id
Username string `json:"username"` // username Username string `json:"username"` // username
Bio string `json:"bio"` // bio Bio string `json:"bio"` // bio
ProfilePic string `json:"profile_pic"` // profile_pic
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