-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_router.go
122 lines (100 loc) · 3.01 KB
/
users_router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"net/http"
"strconv"
)
type UsersRouter struct {
Subrouter *mux.Router
DB *sql.DB
}
func (ur *UsersRouter) Route(router *mux.Router) {
ur.Subrouter = router.PathPrefix("/users").Subrouter()
ur.Subrouter.HandleFunc("", ur.ListUsers).Methods("GET")
ur.Subrouter.HandleFunc("", ur.CreateUser).Methods("POST")
ur.Subrouter.HandleFunc("/{id:[0-9]+}", ur.GetUser).Methods("GET")
ur.Subrouter.HandleFunc("/{id:[0-9]+}", ur.UpdateUser).Methods("PUT")
ur.Subrouter.HandleFunc("/{id:[0-9]+}", ur.DeleteUser).Methods("DELETE")
}
func (ur *UsersRouter) ListUsers(w http.ResponseWriter, r *http.Request) {
// limit to 10 users for now, probably remove limit restriction later
users, err := getUsers(ur.DB, 0, 10)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
}
respondWithJSON(w, http.StatusOK, users)
}
func (ur *UsersRouter) DeleteUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
respondWithError(w, http.StatusBadRequest, errors.New("Invalid user ID"))
return
}
u := user{ID: id}
if err := u.deleteUser(ur.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "success"})
}
func (ur *UsersRouter) UpdateUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
respondWithError(w, http.StatusBadRequest, errors.New("Invalid user ID"))
return
}
var u user
decoder := json.NewDecoder(r.Body)
// maps values from json that match back to struct
if err := decoder.Decode(&u); err != nil {
respondWithError(w, http.StatusBadRequest, errors.New("Invalid request payload"))
return
}
defer r.Body.Close()
u.ID = id
if err := u.updateUser(ur.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusOK, u)
}
func (ur *UsersRouter) GetUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
respondWithError(w, http.StatusBadRequest, errors.New("Invalid user ID"))
return
}
u := user{ID: id}
if err := u.getUser(ur.DB); err != nil {
switch err {
case sql.ErrNoRows:
respondWithError(w, http.StatusNotFound, errors.New("User not found"))
default:
respondWithError(w, http.StatusInternalServerError, err)
}
return
}
respondWithJSON(w, http.StatusOK, u)
}
func (ur *UsersRouter) CreateUser(w http.ResponseWriter, r *http.Request) {
var u user
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&u); err != nil {
respondWithError(w, http.StatusBadRequest, errors.New("Invalid request payload"))
return
}
defer r.Body.Close()
if err := u.createUser(ur.DB); err != nil {
fmt.Println(err)
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusCreated, u)
}