1 ! Copyright (c) 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel sequences namespaces
4 db db.types db.tuples validators hashtables urls
9 http.server.dispatchers
19 TUPLE: todo-list < dispatcher ;
21 TUPLE: todo uid id priority summary description ;
25 { "uid" "UID" { VARCHAR 256 } +not-null+ }
26 { "id" "ID" +db-assigned-id+ }
27 { "priority" "PRIORITY" INTEGER +not-null+ }
28 { "summary" "SUMMARY" { VARCHAR 256 } +not-null+ }
29 { "description" "DESCRIPTION" { VARCHAR 256 } }
32 : <todo> ( id -- todo )
37 : <view-action> ( -- action )
41 "id" value <todo> select-tuple from-object
44 { todo-list "view-todo" } >>template ;
46 : validate-todo ( -- )
48 { "summary" [ v-one-line ] }
49 { "priority" [ v-integer 0 v-min-value 10 v-max-value ] }
50 { "description" [ v-required ] }
53 : view-todo-url ( id -- url )
54 <url> "$todo-list/view" >>path swap "id" set-query-param ;
56 : <new-action> ( -- action )
58 [ 0 "priority" set-value ] >>init
60 { todo-list "new-todo" } >>template
62 [ validate-todo ] >>validate
66 dup { "summary" "priority" "description" } to-object
67 [ insert-tuple ] [ id>> view-todo-url <redirect> ] bi
70 : <edit-action> ( -- action )
74 "id" value <todo> select-tuple from-object
77 { todo-list "edit-todo" } >>template
86 dup { "id" "summary" "priority" "description" } to-object
87 [ update-tuple ] [ id>> view-todo-url <redirect> ] bi
90 : todo-list-url ( -- url )
91 URL" $todo-list/list" ;
93 : <delete-action> ( -- action )
95 [ validate-integer-id ] >>validate
98 "id" get <todo> delete-tuples
99 todo-list-url <redirect>
102 : <list-action> ( -- action )
104 [ f <todo> select-tuples "items" set-value ] >>init
105 { todo-list "todo-list" } >>template ;
107 : <todo-list> ( -- responder )
108 todo-list new-dispatcher
109 <list-action> "" add-responder
110 <view-action> "view" add-responder
111 <new-action> "new" add-responder
112 <edit-action> "edit" add-responder
113 <delete-action> "delete" add-responder
115 { todo-list "todo" } >>template
117 "view your todo list" >>description ;