-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontacts-app.lisp
113 lines (98 loc) · 3.92 KB
/
contacts-app.lisp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(defpackage :msgraph.demo.contacts
(:use :cl :msgraph :odata/lang :easy-routes :cl-who :arrows :access)
(:export :start-app :stop-app))
(in-package :msgraph.demo.contacts)
(defvar *html*)
(defparameter +appuser+ "77d37ed0-173e-474e-a477-371f4bbdd1a2")
(defmacro with-html-page (&body body)
`(who:with-html-output-to-string (*html*)
(:html
(:head
(:title "Contacts")
(:link :rel "stylesheet" :href "https://unpkg.com/[email protected]/build/pure-min.css" :integrity "sha384-oAOxQR6DkCoMliIh8yFnu25d7Eq/PHS21PClpwjOTeU2jRSq11vu66rf90/cZr47" :crossorigin"anonymous"))
(:body
,@body))))
(defun get-contacts (user)
(-> msgraph::+msgraph+
(collection "users")
(id user)
(collection "contacts")
(fetch :collection)))
(defun get-contact (user id)
(-> msgraph::+msgraph+
(collection "users")
(id user)
(collection "contacts")
(id id)
(fetch)))
(defun create-contact (user contact)
(-> msgraph::+msgraph+
(collection "users")
(id user)
(collection "contacts")
(post contact)))
(defroute home ("/" :acceptor-name msgraph-contacts)
()
(with-html-page
(show-contacts-list +appuser+)
(:a :href (genurl 'create-contact-page :acceptor-name 'msgraph-contacts)
(str "New contact"))))
(defroute show-contact ("/contacts/:id" :acceptor-name msgraph-contacts)
(&path (id 'string))
(access:with-dot ()
(let ((contact (get-contact +appuser+ id)))
(with-html-page
(:form :class "pure-form pure-form-stacked"
(:fieldset
(:label "Name")
(:label (str contact.given-name))
(:label "Surname") (:label (str contact.surname))
(:label "Email") (:label (str contact.email-addresses.first.address))
(:label "Phone")
(:label (str contact.business-phones.first))))
))))
(defroute create-contact-page ("/contacts/new" :acceptor-name msgraph-contacts)
()
(with-html-page
(:form :class "pure-form pure-form-aligned"
:method "POST"
(:legend "Create contact")
(:fieldset
(:div :class "pure-control-group"
(:label "Name") (:input :name "name"))
(:div :class "pure-control-group"
(:label "Surname") (:input :name "surname"))
(:div :class "pure-control-group"
(:label "Email") (:input :name "email"))
(:div :class "pure-control-group"
(:label "Phone") (:input :name "phone")))
(:input :type "submit" :value "Create"))))
(defroute save-contact ("/contacts/new" :method :post :acceptor-name msgraph-contacts)
(&post name surname email phone)
(create-contact +appuser+
`((:given-name . ,name)
(:surname . ,surname)
(:email-addresses
((:address . ,email)
(:name . ,(concatenate 'string name " " surname))))
(:business-phones . (,phone))))
(redirect (genurl 'home :acceptor-name 'msgraph-contacts)))
(defun show-contacts-list (user)
(access:with-dot ()
(who:with-html-output (*html*)
(:ul
(loop
for contact in (get-contacts user)
do
(who:htm (:li (:a :href (genurl 'show-contact :id (access contact :id) :acceptor-name 'msgraph-contacts)
(who:str contact.given-name)
(who:str " ")
(who:str contact.surname)))))))))
(defparameter *acceptor* nil)
(defun start-app (&key (port 0))
;; When port is zero, the acceptor is bound to a random free port
(setf *acceptor* (hunchentoot:start (make-instance 'easy-routes-acceptor
:port port
:name 'msgraph-contacts))))
(defun stop-app ()
(hunchentoot:stop *acceptor*))