-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
154 lines (135 loc) · 3.02 KB
/
handler.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-chi/chi"
)
var (
mux chi.Router
)
func initHandler() {
mux = chi.NewMux()
mux.Route("/user", func(r chi.Router) {
r.Get("/", getUser)
r.Post("/", addUser)
r.Patch("/", updateUser)
r.Delete("/", deleteUser)
})
}
type errResp struct {
Error string `json:"error"`
}
func printErr(w http.ResponseWriter, err error, status int) {
w.WriteHeader(status)
var resp errResp
resp.Error = err.Error()
json.NewEncoder(w).Encode(&resp)
}
func printSuccess(w http.ResponseWriter, payload interface{}, status int) {
w.WriteHeader(status)
json.NewEncoder(w).Encode(payload)
}
func getUser(w http.ResponseWriter, r *http.Request) {
var err error
var user User
err = json.NewDecoder(r.Body).Decode(&user)
if err != nil {
printErr(w, err, http.StatusBadRequest)
return
}
session := engine.Prepare()
defer session.Close()
var isExist bool
isExist, err = session.Get(&user)
if err != nil {
printErr(w, err, http.StatusInternalServerError)
return
}
if !isExist {
printErr(w, fmt.Errorf("Error user not found, user struct: %+v", user), http.StatusNotFound)
return
}
printSuccess(w, user, http.StatusOK)
}
func addUser(w http.ResponseWriter, r *http.Request) {
var err error
var user User
err = json.NewDecoder(r.Body).Decode(&user)
if err != nil {
printErr(w, err, http.StatusBadRequest)
return
}
session := engine.Prepare()
defer session.Close()
_, err = engine.InsertOne(&user)
if err != nil {
printErr(w, err, http.StatusInternalServerError)
return
}
printSuccess(w, user, http.StatusCreated)
}
func updateUser(w http.ResponseWriter, r *http.Request) {
var user User
var (
err error
)
err = json.NewDecoder(r.Body).Decode(&user)
if err != nil {
printErr(w, err, http.StatusBadRequest)
return
}
session := engine.Prepare()
defer session.Close()
id := user.ID
user.ID = 0
_, err = session.ID(id).Update(&user)
if err != nil {
printErr(w, err, http.StatusInternalServerError)
return
}
var (
updatedUser User
isExist bool
)
updatedUser.ID = id
isExist, err = session.Get(&updatedUser)
if err != nil {
printErr(w, err, http.StatusInternalServerError)
return
}
if !isExist {
printErr(w, fmt.Errorf("Error user not found, user struct: %+v", updatedUser), http.StatusNotFound)
return
}
printSuccess(w, updatedUser, http.StatusOK)
}
func deleteUser(w http.ResponseWriter, r *http.Request) {
var user User
var (
err error
)
err = json.NewDecoder(r.Body).Decode(&user)
if err != nil {
printErr(w, err, http.StatusBadRequest)
return
}
session := engine.Prepare()
defer session.Close()
var isExist bool
isExist, err = session.Get(&user)
if err != nil {
printErr(w, err, http.StatusInternalServerError)
return
}
if !isExist {
printErr(w, fmt.Errorf("Error user not found, user struct: %+v", user), http.StatusNotFound)
return
}
_, err = session.ID(user.ID).Delete(&User{})
if err != nil {
printErr(w, err, http.StatusInternalServerError)
return
}
printSuccess(w, user, http.StatusOK)
}