factor/extra/webapps/todo/todo.factor

127 lines
3.1 KiB
Factor
Raw Normal View History

2008-04-15 07:35:06 -04:00
! Copyright (c) 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
2008-05-26 01:47:27 -04:00
USING: accessors kernel sequences namespaces
db db.types db.tuples validators hashtables urls
2008-05-26 01:47:27 -04:00
html.components
html.templates.chloe
2008-06-02 16:00:03 -04:00
http.server
http.server.dispatchers
furnace
furnace.sessions
furnace.boilerplate
furnace.auth
furnace.actions
furnace.db
2008-06-02 16:00:03 -04:00
furnace.auth.login ;
2008-04-15 08:09:01 -04:00
IN: webapps.todo
2008-04-15 07:35:06 -04:00
2008-06-02 16:00:03 -04:00
TUPLE: todo-list < dispatcher ;
2008-04-15 07:35:06 -04:00
TUPLE: todo uid id priority summary description ;
todo "TODO"
{
{ "uid" "UID" { VARCHAR 256 } +not-null+ }
2008-04-28 18:38:12 -04:00
{ "id" "ID" +db-assigned-id+ }
2008-04-15 07:35:06 -04:00
{ "priority" "PRIORITY" INTEGER +not-null+ }
{ "summary" "SUMMARY" { VARCHAR 256 } +not-null+ }
{ "description" "DESCRIPTION" { VARCHAR 256 } }
} define-persistent
: init-todo-table todo ensure-table ;
: <todo> ( id -- todo )
todo new
swap >>id
uid >>uid ;
2008-05-26 01:47:27 -04:00
: <view-action> ( -- action )
<page-action>
[
validate-integer-id
"id" value <todo> select-tuple from-object
2008-05-26 01:47:27 -04:00
] >>init
2008-06-02 16:00:03 -04:00
{ todo-list "view-todo" } >>template ;
2008-05-26 01:47:27 -04:00
: validate-todo ( -- )
{
{ "summary" [ v-one-line ] }
{ "priority" [ v-integer 0 v-min-value 10 v-max-value ] }
{ "description" [ v-required ] }
} validate-params ;
: <new-action> ( -- action )
<page-action>
[ 0 "priority" set-value ] >>init
2008-06-02 16:00:03 -04:00
{ todo-list "new-todo" } >>template
2008-05-26 01:47:27 -04:00
[ validate-todo ] >>validate
[
f <todo>
dup { "summary" "priority" "description" } deposit-slots
2008-05-26 01:47:27 -04:00
[ insert-tuple ]
[
<url>
"$todo-list/view" >>path
swap id>> "id" set-query-param
<redirect>
]
2008-05-26 01:47:27 -04:00
bi
] >>submit ;
: <edit-action> ( -- action )
<page-action>
[
validate-integer-id
"id" value <todo> select-tuple from-object
2008-05-26 01:47:27 -04:00
] >>init
2008-06-02 16:00:03 -04:00
{ todo-list "edit-todo" } >>template
2008-05-26 01:47:27 -04:00
[
validate-integer-id
validate-todo
] >>validate
[
f <todo>
dup { "id" "summary" "priority" "description" } deposit-slots
[ update-tuple ]
[
<url>
"$todo-list/view" >>path
swap id>> "id" set-query-param
<redirect>
]
2008-05-26 01:47:27 -04:00
bi
] >>submit ;
: <delete-action> ( -- action )
<action>
[ validate-integer-id ] >>validate
[
"id" get <todo> delete-tuples
URL" $todo-list/list" <redirect>
2008-05-26 01:47:27 -04:00
] >>submit ;
: <list-action> ( -- action )
<page-action>
[ f <todo> select-tuples "items" set-value ] >>init
2008-06-02 16:00:03 -04:00
{ todo-list "todo-list" } >>template ;
2008-04-15 07:35:06 -04:00
2008-05-26 01:47:27 -04:00
: <todo-list> ( -- responder )
todo-list new-dispatcher
<list-action> "list" add-main-responder
<view-action> "view" add-responder
<new-action> "new" add-responder
<edit-action> "edit" add-responder
<delete-action> "delete" add-responder
<boilerplate>
2008-06-02 16:00:03 -04:00
{ todo-list "todo" } >>template
<protected>
"view your todo list" >>description ;