|
1 | 1 | package controllers
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "demo/webservice/models" |
| 5 | + "encoding/json" |
4 | 6 | "net/http"
|
5 | 7 | "regexp"
|
| 8 | + "strconv" |
6 | 9 | )
|
7 | 10 |
|
8 | 11 | type userController struct {
|
9 | 12 | userIDPattern *regexp.Regexp
|
10 | 13 | }
|
11 | 14 |
|
12 | 15 | func (uc userController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
13 |
| - w.Write([]byte("Hello from the User Controller!")) |
| 16 | + if r.URL.Path == "/users" || r.URL.Path == "/users/" { |
| 17 | + switch r.Method { |
| 18 | + case http.MethodGet: |
| 19 | + uc.getAll(w) |
| 20 | + case http.MethodPost: |
| 21 | + uc.post(w, r) |
| 22 | + default: |
| 23 | + w.WriteHeader(http.StatusNotImplemented) |
| 24 | + } |
| 25 | + } else { |
| 26 | + matches := uc.userIDPattern.FindStringSubmatch(r.URL.Path) |
| 27 | + if len(matches) == 0 { |
| 28 | + w.WriteHeader(http.StatusBadRequest) |
| 29 | + w.Write([]byte("Malformed URL")) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + id, err := strconv.Atoi(matches[1]) |
| 34 | + if err != nil { |
| 35 | + w.WriteHeader(http.StatusBadRequest) |
| 36 | + w.Write([]byte("Malformed User ID")) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + switch r.Method { |
| 41 | + case http.MethodGet: |
| 42 | + uc.get(id, w) |
| 43 | + case http.MethodPut: |
| 44 | + uc.put(id, w, r) |
| 45 | + case http.MethodDelete: |
| 46 | + uc.delete(id, w) |
| 47 | + default: |
| 48 | + w.WriteHeader(http.StatusNotImplemented) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (uc *userController) getAll(w http.ResponseWriter) { |
| 54 | + encodeResponseAsJSON(models.GetUsers(), w) |
| 55 | +} |
| 56 | + |
| 57 | +func (uc *userController) get(id int, w http.ResponseWriter) { |
| 58 | + u, err := models.GetUserByID(id) |
| 59 | + if err != nil { |
| 60 | + w.WriteHeader(http.StatusInternalServerError) |
| 61 | + w.Write([]byte(err.Error())) |
| 62 | + return |
| 63 | + } |
| 64 | + encodeResponseAsJSON(u, w) |
| 65 | +} |
| 66 | + |
| 67 | +func (uc *userController) post(w http.ResponseWriter, r *http.Request) { |
| 68 | + u, err := uc.parseRequest(r) |
| 69 | + if err != nil { |
| 70 | + w.WriteHeader(http.StatusInternalServerError) |
| 71 | + w.Write([]byte("Could not parse User object")) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + u, err = models.AddUser(u) |
| 76 | + if err != nil { |
| 77 | + w.WriteHeader(http.StatusInternalServerError) |
| 78 | + w.Write([]byte(err.Error())) |
| 79 | + return |
| 80 | + } |
| 81 | + encodeResponseAsJSON(u, w) |
| 82 | +} |
| 83 | + |
| 84 | +func (uc *userController) put(id int, w http.ResponseWriter, r *http.Request) { |
| 85 | + u, err := uc.parseRequest(r) |
| 86 | + if err != nil { |
| 87 | + w.WriteHeader(http.StatusInternalServerError) |
| 88 | + w.Write([]byte("Could not parse User object")) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + if u.ID != id { |
| 93 | + w.WriteHeader(http.StatusBadRequest) |
| 94 | + w.Write([]byte("ID of submitted user must match ID in URL")) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + u, err = models.UpdateUser(u) |
| 99 | + if err != nil { |
| 100 | + w.WriteHeader(http.StatusInternalServerError) |
| 101 | + w.Write([]byte(err.Error())) |
| 102 | + return |
| 103 | + } |
| 104 | + encodeResponseAsJSON(u, w) |
| 105 | +} |
| 106 | + |
| 107 | +func (uc *userController) delete(id int, w http.ResponseWriter) { |
| 108 | + err := models.RemoveUserByID(id) |
| 109 | + if err != nil { |
| 110 | + w.WriteHeader(http.StatusInternalServerError) |
| 111 | + w.Write([]byte(err.Error())) |
| 112 | + return |
| 113 | + } |
| 114 | + w.WriteHeader(http.StatusOK) |
| 115 | +} |
| 116 | + |
| 117 | +func (uc *userController) parseRequest(r *http.Request) (models.User, error) { |
| 118 | + dec := json.NewDecoder(r.Body) |
| 119 | + var u models.User |
| 120 | + err := dec.Decode(&u) |
| 121 | + if err != nil { |
| 122 | + return models.User{}, err |
| 123 | + } |
| 124 | + return u, nil |
14 | 125 | }
|
15 | 126 |
|
16 | 127 | // Constructor Function - Go's way of creating constructor as a function
|
|
0 commit comments