This repository was archived by the owner on Apr 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.osl
More file actions
223 lines (169 loc) · 4.89 KB
/
handlers.osl
File metadata and controls
223 lines (169 loc) · 4.89 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
def handleHealth(*gin.Context c) (
c.JSON(200, { ok: true })
)
def handleLogout(*gin.Context c) (
c.SetCookie("auth_token", "", -1, "/", "", false, true)
c.Redirect(302, "/")
)
def handleListNotes(*gin.Context c) (
string token = c.MustGet("auth")
c.JSON(200, { notes: listNotes(token) })
)
def handleListNotesWithMetadata(*gin.Context c) (
string token = c.MustGet("auth")
array notes = listNotesWithMetadata(token)
notes.sortBy("edited")
c.JSON(200, { notes: notes })
)
def handleGetAllNotesWithContent(*gin.Context c) (
string token = c.MustGet("auth")
array notes = listNotes(token)
array notesWithContent = []
for i notes.len (
string content = readNote(token, notes[i].toStr())
array tags = readTags(token, notes[i].toStr())
notesWithContent.append({
title: notes[i],
content: content,
tags: tags
})
)
c.JSON(200, { notes: notesWithContent })
)
def handleCreateNote(*gin.Context c) (
string token = c.MustGet("auth")
string title = c.Query("title")
string content = c.Query("content")
string tagsStr = c.Query("tags")
if title == "" (
c.JSON(400, { ok: false, error: "missing title" })
c.Abort()
return
)
if content == "" (
c.JSON(400, { ok: false, error: "missing content" })
c.Abort()
return
)
bool ok = writeNote(token, title, content)
if !ok (
c.JSON(400, { ok: false, error: "failed to write note" })
c.Abort()
return
)
if tagsStr != "" (
array tags = tagsStr.split(",")
for i tags.len (
tags[i] = tags[i].toStr().strip()
)
writeTags(token, title, tags)
)
c.JSON(200, { ok: true })
)
def handleDeleteNote(*gin.Context c) (
string token = c.MustGet("auth")
string title = c.Query("title")
if title == "" (
c.JSON(400, { ok: false, error: "missing title" })
c.Abort()
return
)
deleteNote(token, title)
deleteTags(token, title)
c.JSON(200, { ok: true })
)
def handleRenameNote(*gin.Context c) (
string token = c.MustGet("auth")
string oldTitle = c.Query("oldTitle")
string newTitle = c.Query("newTitle")
if oldTitle == "" (
c.JSON(400, { ok: false, error: "missing old title" })
c.Abort()
return
)
if newTitle == "" (
c.JSON(400, { ok: false, error: "missing new title" })
c.Abort()
return
)
bool ok = renameNote(token, oldTitle, newTitle)
if !ok (
c.JSON(400, { ok: false, error: "invalid title - only letters, numbers, spaces and underscores allowed" })
c.Abort()
return
)
c.JSON(200, { ok: true })
)
def handleReadNote(*gin.Context c) (
string token = c.MustGet("auth")
string title = c.Query("title")
if title == "" (
c.JSON(400, { ok: false, error: "missing title" })
c.Abort()
return
)
if !noteExists(token, title) (
c.JSON(404, { ok: false, error: "note not found" })
c.Abort()
return
)
string content = readNote(token, title)
if content == "" (
c.JSON(400, { ok: false, error: "invalid title - only letters, numbers, spaces and underscores allowed" })
c.Abort()
return
)
array tags = readTags(token, title)
c.JSON(200, { content: content, tags: tags })
)
def handleUpdateTags(*gin.Context c) (
string token = c.MustGet("auth")
string title = c.Query("title")
string tagsStr = c.Query("tags")
if title == "" (
c.JSON(400, { ok: false, error: "missing title" })
c.Abort()
return
)
if !noteExists(token, title) (
c.JSON(404, { ok: false, error: "note not found" })
c.Abort()
return
)
array tags = []
if tagsStr != "" (
tags = tagsStr.split(",")
for i tags.len (
tags[i] = tags[i].toStr().trim()
)
bool ok = writeTags(token, title, tags)
if ok (
c.JSON(200, { ok: true })
) else (
c.JSON(500, { ok: false, error: "failed to update tags" })
)
) else (
// No tags provided, don't update existing tags
c.JSON(200, { ok: true })
)
)
def handleGetAllTags(*gin.Context c) (
string token = c.MustGet("auth")
array tags = getAllTags(token)
c.JSON(200, { tags: tags })
)
def handleFilterNotesByTags(*gin.Context c) (
string token = c.MustGet("auth")
string tagsStr = c.Query("tags")
if tagsStr == "" (
c.JSON(400, { ok: false, error: "missing tags" })
c.Abort()
return
)
array filterTags = tagsStr.split(",")
for i filterTags.len (
filterTags[i] = filterTags[i].toStr().trim()
)
array notes = filterNotesByTags(token, filterTags)
c.JSON(200, { notes: notes })
)