From 7ab43cf9f290e3e802d3b04e352e5c7db4b14170 Mon Sep 17 00:00:00 2001 From: UnicodingUnicorn <7555ic@gmail.com> Date: Tue, 18 Jun 2019 18:53:04 +0800 Subject: [PATCH] Get user by username as per issue #9 --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++-- handlers.go | 27 +++++++++++++++++++++++++++ main.go | 1 + 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d21dfc..c3c87a4 100644 --- a/README.md +++ b/README.md @@ -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": "", + "username": "", "first_name": "", "last_name": "", "phone_number": "" @@ -102,6 +104,7 @@ List of users. [ { "id": "", + "username": "", "first_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": "", + "username": "", "first_name": "", "last_name": "", "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": "", + "username": "", + "first_name": "", + "last_name": "", + "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* ``` diff --git a/handlers.go b/handlers.go index 7cd105e..4b08087 100644 --- a/handlers.go +++ b/handlers.go @@ -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) diff --git a/main.go b/main.go index 9ecbd38..39d47c6 100644 --- a/main.go +++ b/main.go @@ -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))