forked from planety/prologue-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.nim
83 lines (74 loc) · 2.39 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
from sqlite3 import last_insert_rowid
import
strutils,
db_sqlite,
prologue,
views
# Create instance
var app = newApp()
# Create routes
# - list all
app.addRoute("/", proc(ctx: Context) {.async.} =
let db = open("sqlite.db", "", "", "")
let rows = db.getAllRows(sql("""SELECT * FROM todo"""))
db.close()
resp htmlResponse(listView(rows=rows))
)
# - create
app.get("/create", proc(ctx: Context) {.async.} =
if ctx.getQueryParams("save").len != 0:
let
row = ctx.getQueryParams("task").strip
db = open("sqlite.db", "", "", "")
db.exec(sql"INSERT INTO todo (task,status) VALUES (?,?)", row, 1)
let
id = last_insert_rowid(db)
db.close()
resp redirect("/create/?status=success&id=" & $id)
else:
resp htmlResponse(createView(ctx.getQueryParams("status"), ctx.getQueryParams("id")))
)
# - read
app.get("/read/{item}", proc(ctx: Context) {.async.} =
let
db = open("sqlite.db", "", "", "")
item = ctx.getPathParams("item", "")
rows = db.getAllRows(sql"SELECT task FROM todo WHERE id LIKE ?", item)
db.close()
if rows.len == 0:
resp "This item number does not exist!"
else:
resp htmlResponse(readView($rows[0]))
)
# - update
app.get("/update/{id}", proc(ctx: Context) {.async.} =
if ctx.getQueryParams("save").len != 0:
let
edit = ctx.getQueryParams("task").strip
status = ctx.getQueryParams("status").strip
id = ctx.getPathParams("id", "")
var statusId = 0
if status == "open":
statusId = 1
let db= open("sqlite.db", "", "", "")
db.exec(sql"UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", edit, statusId, id)
db.close()
resp redirect("/update/" & id & "?status=success")
else:
let db = open("sqlite.db", "", "", "")
let id = ctx.getPathParams("id", "")
let data = db.getAllRows(sql"SELECT task FROM todo WHERE id LIKE ?", id)
db.close()
resp htmlResponse(updateView(id.parseInt, data[0], ctx.getQueryParams("status")))
)
# - delete
app.get("/delete/{id}", proc(ctx: Context) {.async.} =
let
id = ctx.getPathParams("id")
db= open("sqlite.db", "", "", "")
db.exec(sql"DELETE FROM todo WHERE id = ?", id)
db.close()
resp redirect("/")
)
# Run instance
app.run()