/
routes_all_notes.go
210 lines (177 loc) · 4.95 KB
/
routes_all_notes.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
203
204
205
206
207
208
209
210
package api
import (
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
//
// Note that although this is an _all_ file, the calls are from within /admin or /participant so only logged in AND
// it's assumed that they are logged in
//
// routeAllGetMyNotes gets all of the current user's notes
func routeAllGetMyNotes(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromHTTPContext(r)
if err != nil {
sendAPIError(w, api_error_auth_missing, err, map[string]string{})
return
}
noteType := r.URL.Query().Get("type")
filter := &NoteSelectOptions{
NoteType: noteType,
}
// TODO: add in the project/module/block filtering
notes, err := GetAllNotesForUser(user.ID, noteType, filter)
if err != nil {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
sendAPIJSONData(w, http.StatusOK, notes)
}
// routeAllCreateNote creates a new note for a user
func routeAllCreateNote(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromHTTPContext(r)
if err != nil {
sendAPIError(w, api_error_auth_missing, err, map[string]string{})
return
}
input := &Note{}
render.Bind(r, input)
input.UserID = user.ID
if input.Title == "" || input.Body == "" {
sendAPIError(w, api_error_notes_save, errors.New("title and body required"), map[string]interface{}{
"input": input,
})
return
}
// if the user is an admin, set it to journal
if user.SystemRole == UserSystemRoleAdmin {
input.ProjectID = 0
input.ModuleID = 0
input.BlockID = 0
input.Visibility = NoteVisibilityPrivate
input.NoteType = NoteTypeJournal
} else {
if input.ProjectID != 0 {
if !IsUserInProject(user.ID, input.ProjectID) {
input.ProjectID = 0
input.ModuleID = 0
input.BlockID = 0
input.NoteType = NoteTypeJournal
}
if input.ModuleID != 0 && !IsModuleInProject(input.ProjectID, input.ModuleID) {
input.ModuleID = 0
input.BlockID = 0
}
if input.BlockID != 0 && !IsBlockInModule(input.ModuleID, input.BlockID) {
input.BlockID = 0
}
}
}
err = CreateNote(input)
if err != nil {
sendAPIError(w, api_error_notes_save, err, map[string]interface{}{
"input": input,
})
return
}
sendAPIJSONData(w, http.StatusCreated, input)
}
// routeAllGetMyNoteByID gets a single note for a user by its id
func routeAllGetMyNoteByID(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromHTTPContext(r)
if err != nil {
sendAPIError(w, api_error_auth_missing, err, map[string]string{})
return
}
noteID, noteIDErr := strconv.ParseInt(chi.URLParam(r, "noteID"), 10, 64)
if noteIDErr != nil {
sendAPIError(w, api_error_invalid_path, err, map[string]string{})
return
}
note, err := GetNote(noteID)
if err != nil {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
if note.UserID != user.ID {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
sendAPIJSONData(w, http.StatusOK, note)
}
// routeAllUpdateNoteByID update a single note for a user by its id
func routeAllUpdateNoteByID(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromHTTPContext(r)
if err != nil {
sendAPIError(w, api_error_auth_missing, err, map[string]string{})
return
}
noteID, noteIDErr := strconv.ParseInt(chi.URLParam(r, "noteID"), 10, 64)
if noteIDErr != nil {
sendAPIError(w, api_error_invalid_path, err, map[string]string{})
return
}
note, err := GetNote(noteID)
if err != nil {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
if note.UserID != user.ID {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
input := &Note{}
render.Bind(r, input)
if input.Title != "" {
note.Title = input.Title
}
if input.Body != "" {
note.Body = input.Body
}
if input.Visibility != "" {
note.Visibility = input.Visibility
}
if input.NoteType != "" {
note.NoteType = input.NoteType
}
err = UpdateNote(note)
if err != nil {
sendAPIError(w, api_error_notes_save, err, map[string]interface{}{
"input": input,
})
return
}
sendAPIJSONData(w, http.StatusOK, note)
}
// routeAllDeleteMyNoteByID deletes a single note
func routeAllDeleteMyNoteByID(w http.ResponseWriter, r *http.Request) {
user, err := getUserFromHTTPContext(r)
if err != nil {
sendAPIError(w, api_error_auth_missing, err, map[string]string{})
return
}
noteID, noteIDErr := strconv.ParseInt(chi.URLParam(r, "noteID"), 10, 64)
if noteIDErr != nil {
sendAPIError(w, api_error_invalid_path, err, map[string]string{})
return
}
note, err := GetNote(noteID)
if err != nil {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
if note.UserID != user.ID {
sendAPIError(w, api_error_notes_not_found, err, map[string]string{})
return
}
err = DeleteNoteByID(noteID)
if err != nil {
sendAPIError(w, api_error_notes_delete, err, map[string]string{})
return
}
sendAPIJSONData(w, http.StatusOK, map[string]bool{
"deleted": true,
})
}