From da8a20a7e1e95e9a543220c6172689c2fc52b4e2 Mon Sep 17 00:00:00 2001 From: James Brechtel Date: Sun, 21 Jun 2026 08:18:48 -0400 Subject: [PATCH] feat: wire up /add handler with form model --- spine.el | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spine.el b/spine.el index 35718f3..f6b0694 100644 --- a/spine.el +++ b/spine.el @@ -57,6 +57,23 @@ Returns the rendered string." (setq s (replace-regexp-in-string "\"" """ 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) "Build the view model ht for templates/index.mustache from BOOKS. 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))))) (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) "Redirect root to /index." (httpd-redirect proc "/index" 302))