Added bio field to User as per issue #10
parent
7ab43cf9f2
commit
061611dd96
|
@ -55,6 +55,8 @@ Create a new user.
|
||||||
|
|
||||||
| Name | Type | Description | Required |
|
| Name | Type | Description | Required |
|
||||||
| ---- | ---- | ----------- | -------- |
|
| ---- | ---- | ----------- | -------- |
|
||||||
|
| username | String | Username of the added user. Must be unique. | ✓ |
|
||||||
|
| bio | String | Bio of the added user | ✓ |
|
||||||
| first_name | String | First name of the added user. | ✓ |
|
| first_name | String | First name of the added user. | ✓ |
|
||||||
| last_name | String | Last name of the added user. | ✓ |
|
| last_name | String | Last name of the added user. | ✓ |
|
||||||
| phone_number | String | Phone number of the added user. Shouldn't be needed but makes life easier. | X |
|
| phone_number | String | Phone number of the added user. Shouldn't be needed but makes life easier. | X |
|
||||||
|
@ -67,6 +69,7 @@ Created user object.
|
||||||
{
|
{
|
||||||
"id": "<id>",
|
"id": "<id>",
|
||||||
"username": "<username>",
|
"username": "<username>",
|
||||||
|
"bio": "<bio>",
|
||||||
"first_name": "<first_name>",
|
"first_name": "<first_name>",
|
||||||
"last_name": "<last_name>",
|
"last_name": "<last_name>",
|
||||||
"phone_number": "<phone_number>"
|
"phone_number": "<phone_number>"
|
||||||
|
@ -105,6 +108,7 @@ List of users.
|
||||||
{
|
{
|
||||||
"id": "<id>",
|
"id": "<id>",
|
||||||
"username": "<username>",
|
"username": "<username>",
|
||||||
|
"bio": "<bio>",
|
||||||
"first_name": "<first_name>",
|
"first_name": "<first_name>",
|
||||||
"last_name": "<last_name>"
|
"last_name": "<last_name>"
|
||||||
},
|
},
|
||||||
|
@ -144,6 +148,7 @@ User object.
|
||||||
{
|
{
|
||||||
"id": "<id>",
|
"id": "<id>",
|
||||||
"username": "<username>",
|
"username": "<username>",
|
||||||
|
"bio": "<bio>",
|
||||||
"first_name": "<first_name>",
|
"first_name": "<first_name>",
|
||||||
"last_name": "<last_name>",
|
"last_name": "<last_name>",
|
||||||
"phone_number": "<phone_number>"
|
"phone_number": "<phone_number>"
|
||||||
|
@ -181,6 +186,7 @@ User object.
|
||||||
{
|
{
|
||||||
"id": "<id>",
|
"id": "<id>",
|
||||||
"username": "<username>",
|
"username": "<username>",
|
||||||
|
"bio": "<bio>",
|
||||||
"first_name": "<first_name>",
|
"first_name": "<first_name>",
|
||||||
"last_name": "<last_name>",
|
"last_name": "<last_name>",
|
||||||
"phone_number": "<phone_number>"
|
"phone_number": "<phone_number>"
|
||||||
|
@ -475,6 +481,8 @@ List of user objects in user's contacts.
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"id": "<id>",
|
"id": "<id>",
|
||||||
|
"username": "<username>",
|
||||||
|
"bio": "<bio>",
|
||||||
"first_name": "<first_name>",
|
"first_name": "<first_name>",
|
||||||
"last_name": "<last_name>"
|
"last_name": "<last_name>"
|
||||||
},
|
},
|
||||||
|
|
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, username, first_name, last_name, phone_number)
|
INSERT INTO "user" (id, username, bio, first_name, last_name, phone_number)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
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=$3, last_name=$4
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`, user.ID, user.Username, user.FirstName, user.LastName, user.PhoneNumber).Scan(&finalId)
|
`, user.ID, user.Username, user.Bio, 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, first_name, last_name FROM "user" WHERE phone_number = $1
|
SELECT id, username, bio, 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.FirstName, &user.LastName); err != nil {
|
if err := rows.Scan(&user.ID, &user.Username, &user.Bio, &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, first_name, last_name, phone_number FROM "user" WHERE id = $1
|
SELECT id, username, bio, first_name, last_name, phone_number FROM "user" WHERE id = $1
|
||||||
`, userID).Scan(&user.ID, &user.Username, &user.FirstName, &user.LastName, &user.PhoneNumber)
|
`, userID).Scan(&user.ID, &user.Username, &user.Bio, &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, first_name, last_name, phone_number FROM "user" WHERE username = $1
|
SELECT id, username, bio, first_name, last_name, phone_number FROM "user" WHERE username = $1
|
||||||
`, username).Scan(&user.ID, &user.Username, &user.FirstName, &user.LastName, &user.PhoneNumber)
|
`, username).Scan(&user.ID, &user.Username, &user.Bio, &user.FirstName, &user.LastName, &user.PhoneNumber)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case err == sql.ErrNoRows:
|
case err == sql.ErrNoRows:
|
||||||
|
@ -437,7 +437,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".first_name, "user".last_name, "user".phone_number FROM "user"
|
SELECT "user".id, "user".username, "user".bio, "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
|
||||||
|
@ -452,13 +452,13 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
|
||||||
|
|
||||||
// Scan
|
// Scan
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id, username, firstName, lastName, phoneNumber string
|
var id, username, bio, firstName, lastName, phoneNumber string
|
||||||
if err := rows.Scan(&id, &username, &firstName, &lastName, &phoneNumber); err != nil {
|
if err := rows.Scan(&id, &username, &bio, &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, FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber})
|
users = append(users, User{ID: id, Username: username, Bio: bio, FirstName: firstName, LastName: lastName, PhoneNumber: phoneNumber})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond
|
// Respond
|
||||||
|
@ -493,8 +493,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, first_name, last_name, phone_number)
|
INSERT INTO "user" (id, username, bio, 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
|
||||||
|
@ -530,7 +530,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, first_name, last_name, phone_number FROM "user"
|
SELECT id, username, bio, 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)
|
||||||
|
@ -543,13 +543,13 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
|
||||||
|
|
||||||
// Scan
|
// Scan
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id, username, firstName, lastName, phone string
|
var id, username, bio, firstName, lastName, phone string
|
||||||
if err := rows.Scan(&id, &username, &firstName, &lastName, &phone); err != nil {
|
if err := rows.Scan(&id, &username, &bio, &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, firstName, lastName, phone})
|
contacts = append(contacts, User{id, username, bio, firstName, lastName, phone})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond
|
// Respond
|
||||||
|
|
|
@ -2,6 +2,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,
|
username VARCHAR(63555) UNIQUE,
|
||||||
|
bio 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
|
||||||
|
|
|
@ -1,38 +1,42 @@
|
||||||
INSERT INTO "user" (
|
INSERT INTO "user" (
|
||||||
id, username, first_name, last_name, phone_number
|
id, username, bio, 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, first_name, last_name, phone_number
|
id, username, bio, 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, first_name, last_name, phone_number
|
id, username, bio, 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, first_name, last_name, phone_number
|
id, username, bio, first_name, last_name, phone_number
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'u-fb91825f564a3cc110f11836fedea6f4',
|
'u-fb91825f564a3cc110f11836fedea6f4',
|
||||||
'solderneer',
|
'solderneer',
|
||||||
|
'',
|
||||||
'Sudharshan',
|
'Sudharshan',
|
||||||
'',
|
'',
|
||||||
'+65 8143 8417'
|
'+65 8143 8417'
|
||||||
|
|
1
types.go
1
types.go
|
@ -8,6 +8,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
|
Username string `json:"username"` // username
|
||||||
|
Bio string `json:"bio"` // bio
|
||||||
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