summary refs log tree commit diff
path: root/src/gemma.lisp
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-12-20T18·36+0100
committerVincent Ambo <tazjin@gmail.com>2017-12-20T19·15+0100
commitcf0395e37ca2fd31bc14e1395185f59064ac854e (patch)
treede9f55dfb84af2eb5a5e867d7ffa0f5993885e2c /src/gemma.lisp
parentce036d7d15361ad4ad6630d0528473529d3df46a (diff)
feat(lisp): Add Hunchentoot handler for task completion
Diffstat (limited to 'src/gemma.lisp')
-rw-r--r--src/gemma.lisp23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/gemma.lisp b/src/gemma.lisp
index 0aa873db459e..39feb5f6081c 100644
--- a/src/gemma.lisp
+++ b/src/gemma.lisp
@@ -74,11 +74,19 @@ maximum interval."
 ;; Define web API
 ;;
 
+(defun response-for (task)
+  "Create a response object to be JSON encoded for TASK."
+  `((:name . ,(name-of task))
+    (:description . ,(description-of task))
+    (:remaining . ,(days-remaining task))))
+
 (defun start-gemma ()
   ;; Set up web server
   (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242))
 
   ;; ... and register all handlers.
+
+  ;; Task listing handler
   (hunchentoot:define-easy-handler
    (get-tasks :uri "/tasks") ()
 
@@ -86,11 +94,16 @@ maximum interval."
    (setf (hunchentoot:header-out "Access-Control-Allow-Origin") "*")
    (json:encode-json-to-string
     ;; Construct a frontend-friendly representation of the tasks.
-    (mapcar
-     (lambda (task) `((:name . ,(name-of task))
-                      (:description . ,(description-of task))
-                      (:remaining . ,(days-remaining task))))
-     (sort-tasks (list-tasks))))))
+    (mapcar #'response-for (sort-tasks (list-tasks)))))
+
+  ;; Task completion handler
+  (hunchentoot:define-easy-handler
+   (complete-task-handler :uri "/complete") (task)
+   (setf (hunchentoot:content-type*) "application/json")
+   (let* ((key (intern (json:camel-case-to-lisp task) "GEMMA")))
+     (format t "Marking task ~A as completed" key)
+     (complete-task key)
+     (json:encode-json-to-string (response-for (get-task key))))))
 
 ;; (not-so) example tasks