-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.nim
103 lines (79 loc) · 2.15 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import
strutils,
json
import
allographer/query_builder,
prologue
import
views,
models
# Create instance
var app = newApp()
# Init schemas
discard initSchemas()
# Create routes
# List all
app.addRoute("/", proc(ctx: Context) {.async.} =
let rows = rdb().table("todo").getPlain()
resp htmlResponse(listView(rows=rows))
)
# Create
app.get("/create", proc(ctx: Context) {.async.} =
if ctx.getQueryParams("save").len != 0:
let newTask = ctx.getQueryParams("task").strip
let id = rdb()
.table("todo")
.insertID(%*{
"task": newTask,
"status": 0
})
resp redirect("/create/?update_status=success&id=" & $id)
else:
resp htmlResponse(createView(ctx.getQueryParams("update_status"), ctx.getQueryParams("id")))
)
# Read
app.get("/read/{id}", proc(ctx: Context) {.async.} =
let id = ctx.getPathParams("id", "")
let row = rdb()
.table("todo")
.where("id", "=", $id)
.firstPlain()
resp htmlResponse(readView(row[1]))
)
# Update
app.get("/update/{id}", proc(ctx: Context) {.async.} =
var id = ctx.getPathParams("id", "")
if ctx.getQueryParams("save").len != 0:
let
task = ctx.getQueryParams("task").strip
status = ctx.getQueryParams("status").strip
var statusId = 0
if status == "open":
statusId = 1
rdb()
.table("todo")
.where("id", "=", id)
.update(%*{
"task": task,
"status": statusId
})
resp redirect("/update/" & id & "?update_status=success")
else:
var row = rdb()
.table("todo")
.where("id", "=", id)
.firstPlain()
echo $row
resp htmlResponse(updateView(id.parseInt, row, ctx.getQueryParams("update_status")))
)
# Delete
app.get("/delete/{id}", proc(ctx: Context) {.async.} =
let id = ctx.getPathParams("id")
rdb()
.table("todo")
.where("id", "=", id)
.delete()
resp redirect("/")
)
# Run instance
app.run()