diff options
author | Grant Shoshin Shangreaux <shoshin@cicadas.surf> | 2022-12-28 14:02:25 -0600 |
---|---|---|
committer | Grant Shoshin Shangreaux <shoshin@cicadas.surf> | 2022-12-28 14:02:25 -0600 |
commit | 72bcf7e66269af588d9daa9064de23d257cbd053 (patch) | |
tree | d0da6e22ddf93d2546fa415a44594f7be0f5b379 | |
parent | 2c75a754fdddb3e46e5eed95abacbcfc70334fe7 (diff) |
Add: serialization for Heroes and dnd.api package
-rw-r--r-- | api.lisp | 16 | ||||
-rw-r--r-- | init.lisp | 12 | ||||
-rw-r--r-- | model.lisp | 6 | ||||
-rw-r--r-- | package.lisp | 14 | ||||
-rw-r--r-- | routes.lisp | 12 | ||||
-rw-r--r-- | serialization.lisp | 25 | ||||
-rw-r--r-- | utilities.lisp | 2 |
7 files changed, 50 insertions, 37 deletions
@@ -1,8 +1,8 @@ ;;;; api.lisp -- http routes for dnd -(in-package :dnd) +(in-package :dnd.api) -(lzb:provision-app (api) +(lzb:provision-app () :title "Dungeons & Deadlines" :version "0.1.0" :prefix "/api" @@ -13,13 +13,11 @@ (declare (ignore ignore)) t) - - -(defendpoint* :get "/hall-of-legends" ((filter-by identity)) +(defendpoint api :get "/heroes" () (:auth t) "Get a list of heros sorted by renown" (json:to-json - (list :heroes - (sort (copy-seq (db:store-objects-with-class 'hero)) - #'> - :key #'renown)))) + (list :|heroes| + (sort (copy-seq (db:store-objects-with-class 'hero)) + #'> + :key #'renown)))) @@ -1,6 +1,6 @@ ;;;; init.lisp -(in-package #:dnd) +(in-package #:dnd.api) (defun init-db (&optional config) (if config @@ -9,3 +9,13 @@ 'db:mp-store :directory (merge-pathnames "dnd-store/" (user-homedir-pathname)) :subsystems (list (make-instance 'db:store-object-subsystem))))) + +(defvar *dnd-arena* nil + "The instance of the HTTP server.") + +(defun conjure-arena () + (setf *dnd-arena* (lzb:create-server)) + (lzb:install-app *dnd-arena* 'api) + (lzb:start-server *dnd-arena*)) + + @@ -1,8 +1,6 @@ ;;;; model.lisp -- bknr.datastore class definitions for dnd - - -(in-package :dnd) +(in-package :dnd.api) (deftype title () `(member :noob)) @@ -20,7 +18,7 @@ (experience hero)) (defclass has-uid () - ((nuid :reader uid :initform (nuid))) + ((uid :reader uid :initform (nuid))) (:metaclass db:persistent-class)) (defclass can-equip () diff --git a/package.lisp b/package.lisp index 2c7bff8..ff06a36 100644 --- a/package.lisp +++ b/package.lisp @@ -8,8 +8,20 @@ (#:re #:cl-ppcre) (#:json #:jonathan)) (:import-from #:lazybones + #:defendpoint #:defendpoint*) (:import-from #:derrida #:with-plist)) - +(defpackage #:dnd.api + (:use #:cl) + (:local-nicknames (#:db #:bknr.datastore) + (#:idx #:bknr.indices) + (#:lzb #:lazybones) + (#:re #:cl-ppcre) + (#:json #:jonathan)) + (:import-from #:lazybones + #:defendpoint + #:defendpoint*) + (:import-from #:derrida + #:with-plist)) diff --git a/routes.lisp b/routes.lisp deleted file mode 100644 index 56b6191..0000000 --- a/routes.lisp +++ /dev/null @@ -1,12 +0,0 @@ -;;;; routes.lisp -- http routes for dnd - -(in-package :dnd) - -(lzb:provision-app (api) - :title "Dungeons & Deadlines" - :version "0.1.0" - :prefix "/api" - :content-type "application/json") - - - diff --git a/serialization.lisp b/serialization.lisp index 6579b92..d1689c1 100644 --- a/serialization.lisp +++ b/serialization.lisp @@ -1,13 +1,20 @@ ;;;; serialization.lisp +(in-package :dnd.api) +(defmacro with-json-object (&body pairs) + (list* + 'json:with-object + (loop for (key val . more) on pairs by #'cddr + collect `(json:write-key-value ,key ,val)))) -(in-package :dnd) - -(defun hall-of-fame-hero-view (hero) - (json:to-json - (list :name (hero-name hero) - :uid (uid hero) - :renown (renown hero) - :title (hero-title hero) - :class (hero-class hero)))) +(defmethod json:%to-json ((hero hero)) + (with-slots (name experience chronicle uid equipment-table) hero + (with-json-object + :|name| name + :|experience| experience + :|chronicle| chronicle + :|uid| uid + :|renown| (renown hero) + :|title| (hero-title hero) + :|class| (hero-class hero)))) diff --git a/utilities.lisp b/utilities.lisp index b6cf16a..987ddce 100644 --- a/utilities.lisp +++ b/utilities.lisp @@ -1,6 +1,6 @@ ;;;; utilities -- nuff said -(in-package :dnd) +(in-package :dnd.api) (let ((host (uiop:hostname)) |