From 25c1854c8306d4550c0ced886cbd36cb43099129 Mon Sep 17 00:00:00 2001 From: Grant Shoshin Shangreaux Date: Sat, 21 Jan 2023 09:08:08 -0600 Subject: Add: initial draft of doorkeeper session management --- pages.lisp | 19 +++++++++++-------- routes.lisp | 34 ++++++++++++++++------------------ transactions.lisp | 13 +++++++++++++ 3 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 transactions.lisp diff --git a/pages.lisp b/pages.lisp index 7bbf48b..72291e5 100644 --- a/pages.lisp +++ b/pages.lisp @@ -8,12 +8,12 @@ (:title ,title)) (:body ,@body)))) -(defun a-hero-is-born () - (with-page (:title "A Hero Is Born!") +(defun godess-shrine () + (with-page (:title "A Sacred Shrine") (:header - (:h1 "A Hero Is Materializing From the Void...")) - (:form :method "POST" :action "/a-hero-is-born" - (:label :for "NAME" "Thy Hero's Appelation") + (:h1 "Pray and become a hero...")) + (:form :method "POST" :action "/godess-shrine" + (:label :for "NAME" "Enter the epithet thy hero shall be called:") (:input :name "NAME") (:button :type "submit")))) @@ -21,10 +21,13 @@ (with-page (:title "Tavern Door") (:h1 "Wot's yer name 'ero?") (:form :method "POST" :action "/tavern-door" - (:label :for "NAME" "Thy Hero's Appelation") + (:label :for "NAME" "Thy Hero's Appelation:") (:input :name "NAME") - (:button :type "submit")))) + (:button :type "submit")) + (:h2 "Eh? Ye need to birth a new hero?") + (:a :href "/godess-shrine" "Follow me..."))) (defun tavern (hero) (with-page (:title "A Bustling Tavern") - (:h1 "Aye! Welcome " (hero-name hero)))) + (:h1 "Aye! Welcome " (hero-name hero)) + ())) diff --git a/routes.lisp b/routes.lisp index b1d4ff7..b0b81ac 100644 --- a/routes.lisp +++ b/routes.lisp @@ -9,14 +9,22 @@ (defparameter +session-cookie-name+ "dnd-session") -(defendpoint* :get "/a-hero-is-born" () () - (a-hero-is-born)) +(defun redirect-to (location) + (setf (lzb:response-header :location) location + (lzb:response-code) "303")) -(defendpoint* :post "/a-hero-is-born" () () +(defmacro with-session (&body body) + `(a:if-let ((sesh (session-with-id (lzb:request-cookie +session-cookie-name+)))) + ,@body + (redirect-to "/tavern-door"))) + +(defendpoint* :get "/godess-shrine" () () + (godess-shrine)) + +(defendpoint* :post "/godess-shrine" () () (with-plist ((name :name)) (lzb:request-body) (birth-from-the-goddess-loins name) - (setf (lzb:response-header :location) "/tavern-door" - (lzb:response-code) "303"))) + (redirect-to "/tavern-door"))) (defendpoint* :get "/tavern-door" () () (doorkeeper)) @@ -27,18 +35,8 @@ (a:when-let ((sesh (new-sesh hero))) (lzb:set-response-cookie +session-cookie-name+ (session-id sesh) :path "/" :domain "localhost") - (setf (lzb:response-header :location) "/tavern" - (lzb:response-code) "303")) - (setf (lzb:response-header :location) "/tavern-door" - (lzb:response-code) "303")))) + (redirect-to "/tavern")) + (redirect-to "/tavern-door")))) (defendpoint* :get "/tavern" () () - (let ((hero (session-hero (session-with-id (lzb:request-cookie +session-cookie-name+))))) - (tavern hero))) - -(defun birth-from-the-goddess-loins (name) - (db:with-transaction () - (make-instance 'hero :name name))) - -(defun new-sesh (hero) - (db:with-transaction () (make-instance 'session :hero hero))) + (with-session (tavern (session-hero sesh)))) diff --git a/transactions.lisp b/transactions.lisp new file mode 100644 index 0000000..6a50fc1 --- /dev/null +++ b/transactions.lisp @@ -0,0 +1,13 @@ +;;;; transactions.lisp -- data store transactions for dnd + +(in-package :dnd) + +(defun birth-from-the-goddess-loins (name) + (db:with-transaction () + (make-instance 'hero :name name))) + +(defun new-sesh (hero) + (db:with-transaction () (make-instance 'session :hero hero))) + +(defun destroy-sesh (session) + (db:delete-object session)) -- cgit v1.2.3