4
1
Fork 0

Get user by username as per issue #9

pull/24/head
UnicodingUnicorn 2019-06-18 18:53:04 +08:00
parent 1ec0f43517
commit 7ab43cf9f2
3 changed files with 71 additions and 2 deletions

View File

@ -29,7 +29,8 @@ Unless otherwise noted, bodies and responses are with `Content-Type: application
| -------- |
| [Create User](#Create-User) |
| [Get Users by Phone](#Get-Users-by-Phone) |
| [Get User](#Get-User) |
| [Get User by ID](#Get-User-by-ID) |
| [Get User by Username(#Get-User-by-Username) ]
| [Create Conversation](#Create-Conversation) |
| [Delete Conversation](#Delete-Conversation) |
| [Update Conversation](#Update-Conversation) |
@ -65,6 +66,7 @@ Created user object.
```json
{
"id": "<id>",
"username": "<username>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
@ -102,6 +104,7 @@ List of users.
[
{
"id": "<id>",
"username": "<username>",
"first_name": "<first_name>",
"last_name": "<last_name>"
},
@ -119,7 +122,7 @@ List of users.
---
### Get User
### Get User by ID
```
GET /user/id/:user
@ -140,6 +143,7 @@ User object.
```json
{
"id": "<id>",
"username": "<username>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
@ -155,6 +159,43 @@ User object.
---
### Get User by Username
```
GET /user/username/:username
```
Get a specific user by username.
#### URL Params
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| username | String | User's username. | ✓ |
#### Success Response (200 OK)
User object.
```json
{
"id": "<id>",
"username": "<username>",
"first_name": "<first_name>",
"last_name": "<last_name>",
"phone_number": "<phone_number>"
}
```
#### Errors
| Code | Description |
| ---- | ----------- |
| 404 | User with supplied username could not be found in database |
| 500 | Error occurred retrieving entries from database. |
---
### Create Conversation*
```

View File

@ -126,6 +126,33 @@ func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.P
json.NewEncoder(w).Encode(user)
}
func (h *Handler) GetUserByUsername(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
username := p.ByName("userername")
// Response object
user := User{}
// Select
err := h.db.QueryRow(`
SELECT id, username, first_name, last_name, phone_number FROM "user" WHERE username = $1
`, username).Scan(&user.ID, &user.Username, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch {
case err == sql.ErrNoRows:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
case err != nil:
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Respond
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
func (h *Handler) CreateConversation(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := r.Context().Value("user").(string)

View File

@ -42,6 +42,7 @@ func main() {
router.POST("/user", h.CreateUser)
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)
// Conversations
router.POST("/user/conversation", AuthMiddleware(h.CreateConversation))