/
userController.go
202 lines (173 loc) · 5.48 KB
/
userController.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package controllers
import (
"encoding/json"
"net/http"
"github.com/l3njo/dropnote-api/models"
u "github.com/l3njo/dropnote-api/utils"
"github.com/gorilla/mux"
uuid "github.com/satori/go.uuid"
)
// GetUser is the handler funcion for getting a user fom the database.
func GetUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(UserKey).(uuid.UUID)
user := models.GetUser(App.DB, id)
resp := u.Message(true, "success")
resp["data"] = user
u.Respond(w, resp)
}
// UpdateUser is the handler function for editing a user in the database
func UpdateUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(UserKey).(uuid.UUID)
user, temp := models.GetUser(App.DB, id), &models.User{}
err := json.NewDecoder(r.Body).Decode(&temp)
if err != nil {
u.Respond(w, u.Message(false, "There was an error in your request payload"))
return
}
if tempUser := models.GetUserByMail(App.DB, temp.Mail); tempUser != nil {
u.Respond(w, u.Message(false, "That email address is already in use"))
return
}
user.Name = temp.Name
user.Mail = temp.Mail
resp := make(map[string]interface{})
if data, err := models.UpdateUser(App.DB, user); err != nil {
resp = u.Message(false, err.Error())
} else {
resp = data
}
u.Respond(w, resp)
}
// UpdatePassword is the handler function for changing a user's password
func UpdatePassword(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(UserKey).(uuid.UUID)
user := models.GetUser(App.DB, id)
payload := struct {
Current string `json:"current"`
Updated string `json:"updated"`
}{}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
u.Respond(w, u.Message(false, "There was an error in your request payload"))
return
}
if payload.Current == "" || payload.Updated == "" {
u.Respond(w, u.Message(false, "One of the fields is empty"))
return
}
if _, err := models.Login(App.DB, user.Mail, payload.Current); err != nil {
u.Respond(w, u.Message(false, err.Error()))
return
}
user.Pass = payload.Updated
resp := u.Message(true, "success")
if err = models.UpdatePassword(App.DB, user); err != nil {
resp = u.Message(false, err.Error())
}
u.Respond(w, resp)
}
// DeleteUser is the handler function for removing a user from the database
func DeleteUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(UserKey).(uuid.UUID)
user := models.GetUser(App.DB, id)
err := models.DeleteUser(App.DB, user)
resp := u.Message(true, "success")
if err != nil {
resp = u.Message(false, "failed")
resp["error"] = err
}
u.Respond(w, resp)
}
// GetUserNotes is the handler funcion for getting notes created by current user
func GetUserNotes(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(UserKey).(uuid.UUID)
user := models.GetNotesFor(App.DB, id)
resp := u.Message(true, "success")
resp["data"] = user
u.Respond(w, resp)
}
// UpdateUserNote is the handler function for editing a note created by current user in the database
func UpdateUserNote(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
user := r.Context().Value(UserKey).(uuid.UUID)
id, err := uuid.FromString(params["id"])
if err != nil {
u.Respond(w, u.Message(false, "There was an error in your request"))
return
}
note := models.GetNote(App.DB, id)
if note == nil {
u.Respond(w, u.Message(false, "That note does not exist"))
return
}
if !uuid.Equal(note.Creator, user) {
u.Respond(w, u.Message(false, "You are not authorized to modify that record"))
return
}
if err = json.NewDecoder(r.Body).Decode(¬e); err != nil {
u.Respond(w, u.Message(false, "There was an error in your request payload"))
return
}
resp := make(map[string]interface{})
if data, err := models.UpdateNote(App.DB, note); err != nil {
resp = u.Message(false, "failed")
resp["error"] = err
} else {
resp = data
}
u.Respond(w, resp)
}
// ToggleUserNote is the handler function for togglling visibility for a note created by current user in the database
func ToggleUserNote(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
user := r.Context().Value(UserKey).(uuid.UUID)
id, err := uuid.FromString(params["id"])
if err != nil {
u.Respond(w, u.Message(false, "There was an error in your request"))
return
}
note := models.GetNote(App.DB, id)
if note == nil {
u.Respond(w, u.Message(false, "That note does not exist"))
return
}
if !uuid.Equal(note.Creator, user) {
u.Respond(w, u.Message(false, "You are not authorized to modify that record"))
return
}
note.Visible = !note.Visible
resp := make(map[string]interface{})
if data, err := models.ToggleNote(App.DB, note); err != nil {
resp = u.Message(false, "failed")
resp["error"] = err
} else {
resp = data
}
u.Respond(w, resp)
}
// DeleteUserNote is the handler function for removing a note created by current user from the database
func DeleteUserNote(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
user := r.Context().Value(UserKey).(uuid.UUID)
id, err := uuid.FromString(params["id"])
if err != nil {
u.Respond(w, u.Message(false, "There was an error in your request"))
return
}
note := models.GetNote(App.DB, id)
if note == nil {
u.Respond(w, u.Message(false, "That note does not exist"))
return
}
if !uuid.Equal(note.Creator, user) {
u.Respond(w, u.Message(false, "You are not authorized to delete that record"))
return
}
err = models.DeleteNote(App.DB, note)
resp := u.Message(true, "success")
if err != nil {
resp = u.Message(false, "failed")
resp["error"] = err
}
u.Respond(w, resp)
}