-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.nim
69 lines (54 loc) · 1.51 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
import std/sequtils, strutils, algorithm
import os, jester, nimja
type Todo = ref object
id: int
description: string
done: bool
var
id = 4
todos = @[
Todo(id: 1, description: "clean the room", done: false),
Todo(id: 2, description: "call your mom", done: false),
Todo(id: 3, description: "go to bed early", done: false),
]
proc addTodo(todo: Todo) =
todos.add(todo)
proc deleteTodo(id: int): void =
todos = filter(todos, proc(item: Todo): bool = item.id != id)
proc editTodo(id: int, content: string) =
for item in todos:
if item.id == id:
item.description = content
proc toggleDone(id: int) =
for item in todos:
if item.id == id:
item.done = not item.done
proc renderIndex(todos: seq[Todo]): string =
compileTemplateFile(getScriptDir() / "template/index.nimja")
proc renderInput(todo: Todo): string =
compileTemplateFile(getScriptDir() / "template/partials/_input.nimja")
routes:
get "/":
resp renderIndex(todos)
get "/edit/@id":
var id = parseInt(@"id")
var todo = filter(todos, proc(item: Todo): bool = item.id == id)[0]
resp renderInput(todo)
patch "/@id":
var id = parseInt(@"id")
editTodo(id, @"edit")
redirect("/")
patch "/done/@id":
var id = parseInt(@"id")
toggleDone(id)
redirect("/")
post "/add":
if @"todo".len > 0:
addTodo(Todo(id: id, description: @"todo", done: false))
id += 1
redirect("/")
delete "/@id":
var id = parseInt(@"id")
deleteTodo(id)
redirect("/")
runForever()