4
1
Fork 0

Endpoint to edit users. Fixes #11

pull/24/head
Daniel Lim 2019-06-21 11:14:07 +08:00
parent cfc9ea0672
commit 31b305a9c6
3 changed files with 87 additions and 2 deletions

View File

@ -31,6 +31,7 @@ Unless otherwise noted, bodies and responses are with `Content-Type: application
| [Get Users by Phone](#Get-Users-by-Phone) |
| [Get User by ID](#Get-User-by-ID) |
| [Get User by Username(#Get-User-by-Username) ]
| [Update User](#Update-User) |
| [Create Conversation](#Create-Conversation) |
| [Delete Conversation](#Delete-Conversation) |
| [Update Conversation](#Update-Conversation) |
@ -57,6 +58,7 @@ Create a new user.
| ---- | ---- | ----------- | -------- |
| username | String | Username of the added user. Must be unique. | ✓ |
| bio | String | Bio of the added user | ✓ |
| profile_pic | String | URL of added user's profile picture | ✓ |
| first_name | String | First 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 |
@ -70,6 +72,7 @@ Created user object.
"id": "<id>",
"username": "<username>",
"bio": "<bio>",
"profile_pic: "<profile_pic>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
@ -109,6 +112,7 @@ List of users.
"id": "<id>",
"username": "<username>",
"bio": "<bio>",
"profile_pic": "<profile_pic>",
"first_name": "<first_name>",
"last_name": "<last_name>"
},
@ -149,6 +153,7 @@ User object.
"id": "<id>",
"username": "<username>",
"bio": "<bio>",
"profile_pic": "<profile_pic>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
@ -187,6 +192,7 @@ User object.
"id": "<id>",
"username": "<username>",
"bio": "<bio>",
"profile_pic": "<profile_pic>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
@ -202,6 +208,37 @@ User object.
---
### Update User
```
PATCH /user
```
Update an existing user. User ID is taken from header supplied by `backend-auth`. If one does not wish to update a field, leave it the value acquire from Get-User.
#### Body
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| username | String | Updated username. | X |
| bio | String | Updated bio. | X |
| profile_pic | String | Updated URL of profile picture. | X |
| first_name | String | Updated first name. | X |
| last_name | String | Updated last name | X |
#### Success (200 OK)
Empty body.
#### Errors
| Code | Description |
| ---- | ----------- |
| 400 | Error parsing body/User with username already exists. |
| 500 | Error occurred updating database. |
---
### Create Conversation*
```
@ -421,6 +458,9 @@ List of user objects in conversation.
[
{
"id": "<id>",
"username": "<username>",
"bio": "<bio>",
"profile_pic": "<profile_pic>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
@ -483,6 +523,7 @@ List of user objects in user's contacts.
"id": "<id>",
"username": "<username>",
"bio": "<bio>",
"profile_pic": "<profile_pic",
"first_name": "<first_name>",
"last_name": "<last_name>"
},

View File

@ -153,6 +153,50 @@ func (h *Handler) GetUserByUsername(w http.ResponseWriter, r *http.Request, p ht
json.NewEncoder(w).Encode(user)
}
func (h *Handler) UpdateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := r.Context().Value("user").(string)
user := User{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&user)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Check for duplicate username
var _id string
err = h.db.QueryRow(`
SELECT id FROM "user" WHERE "user".id <> $1 AND "user".username = $2
`, userID, user.Username).Scan(&_id)
if err == nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else if err != sql.ErrNoRows {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Update
_, err = h.db.Exec(`
UPDATE "user"
SET
username = $2,
bio = $3,
profile_pic = $4,
first_name = $5,
last_name = $6
WHERE id = $1
`, userID, user.Username, user.Bio, user.ProfilePic, user.FirstName, user.LastName)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
w.WriteHeader(200);
}
func (h *Handler) CreateConversation(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := r.Context().Value("user").(string)
@ -335,7 +379,7 @@ func (h *Handler) DeleteConversation(w http.ResponseWriter, r *http.Request, p h
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
return
}
// Check

View File

@ -43,7 +43,7 @@ func main() {
router.GET("/user", h.GetUsersByPhone)
router.GET("/user/id/:user", h.GetUser)
router.GET("/user/username/:username", h.GetUserByUsername)
//router.PATCH("/user/:user", h.UpdateUser)
router.PATCH("/user", h.UpdateUser)
// Conversations
router.POST("/user/conversation", AuthMiddleware(h.CreateConversation))
router.GET("/user/conversation", AuthMiddleware(h.GetConversations)) // USER MEMBER CONVERSATION