feat: wire up /add handler with form model
This commit is contained in:
@@ -57,6 +57,23 @@ Returns the rendered string."
|
|||||||
(setq s (replace-regexp-in-string "\"" """ s))
|
(setq s (replace-regexp-in-string "\"" """ s))
|
||||||
s)))
|
s)))
|
||||||
|
|
||||||
|
|
||||||
|
(defun spine-add-form-model (books)
|
||||||
|
"Build the view model ht for templates/add.mustache from BOOKS."
|
||||||
|
(let ((authors nil)
|
||||||
|
(categories nil))
|
||||||
|
(dolist (book books)
|
||||||
|
(let ((author (plist-get book :author)))
|
||||||
|
(when (and author (> (length author) 0)
|
||||||
|
(not (member author authors)))
|
||||||
|
(push author authors)))
|
||||||
|
(dolist (tag (or (plist-get book :tags) nil))
|
||||||
|
(unless (member tag categories)
|
||||||
|
(push tag categories))))
|
||||||
|
(ht ("app_title" "spine")
|
||||||
|
("existing_authors" (sort authors #'string<))
|
||||||
|
("existing_categories" (sort categories #'string<)))))
|
||||||
|
|
||||||
(defun spine-index-model (books &optional selected-id)
|
(defun spine-index-model (books &optional selected-id)
|
||||||
"Build the view model ht for templates/index.mustache from BOOKS.
|
"Build the view model ht for templates/index.mustache from BOOKS.
|
||||||
SELECTED-ID expands the matching book's detail section."
|
SELECTED-ID expands the matching book's detail section."
|
||||||
@@ -295,6 +312,28 @@ Set by test harnesses that only need the model functions.")
|
|||||||
(spine-index-model books (cadr (assoc "id" query)))))
|
(spine-index-model books (cadr (assoc "id" query)))))
|
||||||
(insert (spine-render-empty-state)))))
|
(insert (spine-render-empty-state)))))
|
||||||
|
|
||||||
|
(defservlet add text/html (path query request)
|
||||||
|
(let ((method (cadr (car request))))
|
||||||
|
(if (equal method "POST")
|
||||||
|
(progn
|
||||||
|
(spine-add-book
|
||||||
|
:title (cdr (assoc "title" query))
|
||||||
|
:author (cdr (assoc "author" query))
|
||||||
|
:category (cdr (assoc "category" query))
|
||||||
|
:format (cdr (assoc "format" query))
|
||||||
|
:isbn (cdr (assoc "isbn" query))
|
||||||
|
:cover (cdr (assoc "cover" query))
|
||||||
|
:rec_by (cdr (assoc "rec_by" query))
|
||||||
|
:rec_note (cdr (assoc "rec_note" query))
|
||||||
|
:date_added (cdr (assoc "date_added" query))
|
||||||
|
:initial_note (cdr (assoc "initial_note" query)))
|
||||||
|
(httpd-redirect t "/index" 303))
|
||||||
|
(let* ((books (spine-books))
|
||||||
|
(model (if books
|
||||||
|
(spine-add-form-model books)
|
||||||
|
(ht ("app_title" "spine")))))
|
||||||
|
(insert (spine-render "add.mustache" model))))))
|
||||||
|
|
||||||
(defun httpd/ (proc uri-path query request)
|
(defun httpd/ (proc uri-path query request)
|
||||||
"Redirect root to /index."
|
"Redirect root to /index."
|
||||||
(httpd-redirect proc "/index" 302))
|
(httpd-redirect proc "/index" 302))
|
||||||
|
|||||||
Reference in New Issue
Block a user