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
var finalId string
err = h.db.QueryRow(`
INSERT INTO "user" (id, username, bio, first_name, last_name, phone_number)
VALUES ($1, $2, $3, $4, $5, $6)
INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number)
VALUES ($1, $2, $3, $4, $5, $6, $7)
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
`, 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 {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
@ -74,7 +74,7 @@ func (h *Handler) GetUsersByPhone(w http.ResponseWriter, r *http.Request, _ http
// Select
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)
if err != nil {
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
for rows.Next() {
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)
log.Print(err)
return
@ -108,8 +108,8 @@ func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.P
// Select
err := h.db.QueryRow(`
SELECT id, username, bio, 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)
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.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch {
case err == sql.ErrNoRows:
@ -135,8 +135,8 @@ func (h *Handler) GetUserByUsername(w http.ResponseWriter, r *http.Request, p ht
// Select
err := h.db.QueryRow(`
SELECT id, username, bio, 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)
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.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch {
case err == sql.ErrNoRows:
@ -445,7 +445,7 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
// Select
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 conversation ON "conversation".id = m.conversation
INNER JOIN member
@ -460,13 +460,13 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
// Scan
for rows.Next() {
var id, username, bio, firstName, lastName, phoneNumber string
if err := rows.Scan(&id, &username, &bio, &firstName, &lastName, &phoneNumber); err != nil {
var id, username, bio, profilePic, firstName, lastName, phoneNumber string
if err := rows.Scan(&id, &username, &bio, &profilePic, &firstName, &lastName, &phoneNumber); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
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
@ -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
var contactId string
err = h.db.QueryRow(`
INSERT INTO "user" (id, username, bio, first_name, last_name, phone_number)
VALUES ($1, '', '', '', '', $2)
INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number)
VALUES ($1, '', '', '', '', '', $2)
ON CONFLICT(phone_number)
DO UPDATE SET phone_number=EXCLUDED.phone_number
RETURNING id
@ -538,7 +538,7 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
// Select
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
ON contact.contact = "user".id AND contact.user = $1
`, userID)
@ -551,13 +551,13 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
// Scan
for rows.Next() {
var id, username, bio, firstName, lastName, phone string
if err := rows.Scan(&id, &username, &bio, &firstName, &lastName, &phone); err != nil {
var id, username, bio, profilePic, firstName, lastName, phone string
if err := rows.Scan(&id, &username, &bio, &profilePic, &firstName, &lastName, &phone); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
contacts = append(contacts, User{id, username, bio, firstName, lastName, phone})
contacts = append(contacts, User{id, username, bio, profilePic, firstName, lastName, phone})
}
// Respond

View File

@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS "user" (
id BYTEA PRIMARY KEY,
username VARCHAR(63555) UNIQUE,
bio VARCHAR(63535),
profile_pic VARCHAR(63535),
first_name VARCHAR(65535),
last_name VARCHAR(65535),
phone_number VARCHAR(32) UNIQUE

View File

@ -1,42 +1,46 @@
INSERT INTO "user" (
id, username, bio, first_name, last_name, phone_number
id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES (
'u-7f48e2f2b6f7e4d1f9c864e48bc2b0f2',
'ambc',
'',
'',
'Ambrose',
'Chua',
'+65 9766 3827'
) ON CONFLICT DO NOTHING;
INSERT INTO "user" (
id, username, bio, first_name, last_name, phone_number
id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES (
'u-dc9537ca645ff34b4f289b6bd7aa08b7',
'orcas',
'',
'',
'Daniel',
'Lim',
'+65 8737 7117'
) ON CONFLICT DO NOTHING;
INSERT INTO "user" (
id, username, bio, first_name, last_name, phone_number
id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES (
'u-23e608245d0866ea937f15876adb5ef6',
'it',
'',
'',
'Isaac',
'Tay',
'+65 8181 6346'
) ON CONFLICT DO NOTHING;
INSERT INTO "user" (
id, username, bio, first_name, last_name, phone_number
id, username, bio, profile_pic, first_name, last_name, phone_number
) VALUES (
'u-fb91825f564a3cc110f11836fedea6f4',
'solderneer',
'',
'',
'Sudharshan',
'',
'+65 8143 8417'

View File

@ -8,7 +8,8 @@ type Conversation struct {
type User struct {
ID string `json:"id"` // id
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
LastName string `json:"last_name"` // last_name
PhoneNumber string `json:"phone_number"` // phone_number