summaryrefslogtreecommitdiff
path: root/routes.lisp
blob: b1d4ff7ed3769b9857c6b0991b6d698d9170074d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
;;;; routes.lisp -- http routes for dnd

(in-package :dnd)

(lzb:provision-app ()
  :title "Dungeons & Deadlines"
  :version "0.1.0"
  :content-type "text/html")

(defparameter +session-cookie-name+ "dnd-session")

(defendpoint* :get "/a-hero-is-born" () ()
  (a-hero-is-born))

(defendpoint* :post "/a-hero-is-born" () ()
  (with-plist ((name :name)) (lzb:request-body)
    (birth-from-the-goddess-loins name)
    (setf (lzb:response-header :location) "/tavern-door"
	  (lzb:response-code) "303")))

(defendpoint* :get "/tavern-door" () ()
  (doorkeeper))

(defendpoint* :post "/tavern-door" () ()
  (with-plist ((name :name)) (lzb:request-body)
    (a:if-let ((hero (hero-known-as name)))
      (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"))))

(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)))