-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzed.nim
80 lines (61 loc) · 2.25 KB
/
zed.nim
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
import jester, viewbase, store, json, httpcore, times, norm/sqlite
from nativesockets import Port
import webconfig
var bindAddr = "localhost"
if not BIND_LOCAL_ONLY:
bindAddr = "0.0.0.0"
settings:
port = nativeSockets.Port(webconfig.PORT)
bindAddr = bindAddr
staticDir = "./static"
routes:
get "/":
resp pageBase("""
<div id="zed"></div>
<script src="/marked.min.js"></script>
<script src="/notes.js"></script>
<script src="/zed.js"></script>
""")
get "/cal":
resp pageBase("""
<div id="cal"></div>
<script src="/marked.min.js"></script>
<script src="/notes.js"></script>
<script src="/zcal.js"></script>
""")
get "/notes":
resp(Http200, $(%*searchNotes("")), "application/json")
post "/notes/create":
let noteJson = parseJson(request.body)
assert noteJson.hasKey("content"), "Note requires content!"
assert noteJson.hasKey("tagline"), "Note requires tagline!"
let content = noteJson{"content"}.getStr("")
let tagline = noteJson{"tagline"}.getStr("")
withDb:
resp $(Note(
content: content,
created: now().utc,
tagline: tagline
).insertID())
post "/notes/update":
let noteJson = parseJson(request.body)
assert noteJson.hasKey("content"), "Note requires content!"
assert noteJson.hasKey("tagline"), "Note requires tagline!"
assert noteJson.hasKey("id"), "Note requires ID to be updated!"
let id = noteJson{"id"}.getInt(-1)
assert id != -1, "Cannot update a note with an ID of -1"
withDb:
var note = Note.getOne(id)
discard NoteHistory(
noteId: id,
content: note.content,
tagline: note.tagline,
created: note.created,
updated: note.updated,
deleted: note.deleted
).insertID()
note.content = noteJson{"content"}.getStr("")
note.tagline = noteJson{"tagline"}.getStr("")
note.updated = some(now().utc)
note.update()
resp Http200, "Successfully saved!"