From cf0395e37ca2fd31bc14e1395185f59064ac854e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 20 Dec 2017 19:36:31 +0100 Subject: feat(lisp): Add Hunchentoot handler for task completion --- src/gemma.lisp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/gemma.lisp') 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 -- cgit 1.4.1