-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
45 lines (33 loc) · 893 Bytes
/
handlers.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
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
func GetUsers(w http.ResponseWriter,_ *http.Request) {
users := getUsers()
json.NewEncoder(w).Encode(&users)
}
func GetUser(w http.ResponseWriter,r *http.Request){
params := mux.Vars(r)
username := params["username"]
user := getUser(username)
json.NewEncoder(w).Encode(user)
}
func CreateUpdateUser(w http.ResponseWriter,r *http.Request){
var user User
json.NewDecoder(r.Body).Decode(&user)
if err := SetUser(user); err!= nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte("user added"))
}
func DeleteUser(w http.ResponseWriter,r *http.Request){
params := mux.Vars(r)
username := params["username"]
deleteUser(username)
w.WriteHeader(http.StatusOK)
w.Write([]byte("user deleted"))
}