! Copyright (c) 2008 Slava Pestov ! See https://factorcode.org/license.txt for BSD license. USING: accessors kernel sequences namespaces db db.types db.tuples validators hashtables urls html.forms html.components html.templates.chloe http.server http.server.dispatchers furnace furnace.boilerplate furnace.auth furnace.actions furnace.redirection furnace.db furnace.auth.login webapps.utils ; IN: webapps.todo TUPLE: todo-list < dispatcher ; TUPLE: todo uid id priority summary description ; todo "TODO" { { "uid" "UID" { VARCHAR 256 } +not-null+ } { "id" "ID" +db-assigned-id+ } { "priority" "PRIORITY" INTEGER +not-null+ } { "summary" "SUMMARY" { VARCHAR 256 } +not-null+ } { "description" "DESCRIPTION" { VARCHAR 256 } } } define-persistent : ( id -- todo ) todo new swap >>id username >>uid ; : ( -- action ) [ validate-integer-id "id" value select-tuple from-object ] >>init { todo-list "view-todo" } >>template ; : validate-todo ( -- ) { { "summary" [ v-one-line ] } { "priority" [ v-integer 0 v-min-value 10 v-max-value ] } { "description" [ v-required ] } } validate-params ; : view-todo-url ( id -- url ) "$todo-list/view" >>path swap "id" set-query-param ; : ( -- action ) [ 0 "priority" set-value ] >>init { todo-list "new-todo" } >>template [ validate-todo ] >>validate [ f dup { "summary" "priority" "description" } to-object [ insert-tuple ] [ id>> view-todo-url ] bi ] >>submit ; : ( -- action ) [ validate-integer-id "id" value select-tuple from-object ] >>init { todo-list "edit-todo" } >>template [ validate-integer-id validate-todo ] >>validate [ f dup { "id" "summary" "priority" "description" } to-object [ update-tuple ] [ id>> view-todo-url ] bi ] >>submit ; : todo-list-url ( -- url ) URL" $todo-list/list" ; : ( -- action ) [ validate-integer-id ] >>validate [ "id" get delete-tuples todo-list-url ] >>submit ; : ( -- action ) [ f select-tuples "items" set-value ] >>init { todo-list "todo-list" } >>template ; : ( -- responder ) todo-list new-dispatcher "list" add-responder URL" /list" "" add-responder "view" add-responder "new" add-responder "edit" add-responder "delete" add-responder { todo-list "todo" } >>template "view your todo list" >>description ; USING: furnace.auth.features.registration furnace.auth.features.edit-profile furnace.auth.features.deactivate-user furnace.alloy ; : ( responder -- responder' ) "Todo list" allow-registration allow-edit-profile allow-deactivation ; : todo-db ( -- db ) "todo.db" ; : init-todo-db ( -- ) todo-db [ init-furnace-tables todo ensure-table ] with-db ; : ( -- responder ) init-todo-db todo-db ; : run-todo ( -- ) main-responder set-global todo-db start-expiring run-test-httpd ; MAIN: run-todo