data.lisp (4844B)
1 (in-package :miniblog.data) 2 3 (defun format-dates (content) 4 (let ((created-at (getf content :created-at)) 5 (last-updated-at (getf content :last-updated-at))) 6 (setf (getf content :created-at-rfc-822) (miniblog.format:rfc-822-format created-at)) 7 (setf (getf content :created-at-short) (miniblog.format:short-date-format created-at)) 8 (setf (getf content :created-at-long) (miniblog.format:long-date-format created-at)) 9 (setf (getf content :last-updated-at-rfc-822) (miniblog.format:rfc-822-format last-updated-at)) 10 (setf (getf content :last-updated-at-short) (miniblog.format:short-date-format last-updated-at)) 11 (setf (getf content :last-updated-at-long) (miniblog.format:long-date-format last-updated-at)) 12 content)) 13 14 (defgeneric xform (entry) 15 (:documentation "Transform an entry of some type into an idiomatic p-list")) 16 17 (defmethod xform ((entry blog-entries)) 18 "Transform a blog-entries object into an idiomatic property list" 19 (format-dates 20 (list :id (mito.dao.mixin:object-id entry) 21 :created-at (mito.dao.mixin:object-created-at entry) 22 :last-updated-at (mito.dao.mixin:object-updated-at entry) 23 :title (entry-title entry) 24 :content (entry-content entry) 25 :created-by (entry-username entry) 26 :last-updated-by (entry-last-updated-by entry)))) 27 28 (defmethod xform ((entry blog-pages)) 29 "Transform a blog-pages object into an idiomatic p-list" 30 (format-dates 31 (list :id (mito.dao.mixin:object-id entry) 32 :created-at (mito.dao.mixin:object-created-at entry) 33 :last-updated-at (mito.dao.mixin:object-updated-at entry) 34 :name (page-name entry) 35 :parent (page-parent entry) 36 :title (page-title entry) 37 :content (page-content entry) 38 :created-by (page-username entry) 39 :last-updated-by (page-last-updated-by entry)))) 40 41 (defun add-entry (title content &key (username "nobody")) 42 (miniblog.db:add-entry title content :username username :transform #'xform)) 43 44 (defun get-entry (id) 45 "Get entry by id, or nil if the requested id isn't found" 46 (miniblog.db:get-entry id :transform #'xform)) 47 48 (defmacro with-entry-id (entry id &rest body) 49 "Takes a varname to hold the entry list and a post id 50 and executes the forms in body with the entry bound 51 to the specified entry variable" 52 `(let ((,entry (miniblog.data:get-entry ,id))) 53 (if ,entry 54 (progn ,@body) 55 (format t "Post ID ~d not found~%" ,id)))) 56 57 (defun get-entries (&key year month max-entries) 58 "Get entries from the database, optionally limited to a date 59 range or count" 60 (miniblog.db:get-entries :year year :month month :max-entries max-entries :transform #'xform)) 61 62 (defun update-entry (id title content &key (username "nobody")) 63 "Update entry by id. Returns the updated entry or nil if the id doesn't exist." 64 (miniblog.db:update-entry id title content :username username :transform #'xform)) 65 66 (defun delete-entry (id) 67 "Delete the specified entry from the database. No-op if the id is invalid." 68 (miniblog.db:delete-entry id)) 69 70 (defun add-page (name title content &key (parent 0) (username "nobody")) 71 "Add a new page to the database" 72 (miniblog.db:add-page name title content :parent parent :username username :transform #'xform)) 73 74 (defun get-page (id) 75 "Get page by id, or NIL if the requested id isn't found" 76 (miniblog.db:get-page id :transform #'xform)) 77 78 (defmacro with-page-id (page id &rest body) 79 "Takes a varname to hold the entry list and a page id 80 and executes the forms in body with the entry bound 81 to the specified entry variable" 82 `(let ((,page (miniblog.data:get-page ,id))) 83 (if ,page 84 (progn ,@body) 85 (format t "Page ID ~d not found~%" ,id)))) 86 87 (defun get-pages (&optional (root-id 0)) 88 "Get all pages in a tree, or optionally a subtree starting from a given id. Returns the subtree as the first return value and the (unconditionally) full hashtable as the second." 89 (miniblog.db:get-pages :root-id root-id :transform #'xform)) 90 91 (defun update-page (id name title content &key (username "nobody")) 92 "Update page by id. Returns the updated page or nil if the id doesn't exist." 93 (miniblog.db:update-page id name title content :username username :transform #'xform)) 94 95 (defun move-page (id parent) 96 "Move a page under a new parent page (or to the root if parent is 0 or NIL)" 97 (miniblog.db:move-page id parent :transform #'xform)) 98 99 (defun delete-page (id &key (children :move-to-parent)) 100 "Delete a page. The parameter CHILDREN can be any of :MOVE-TO-PARENT (the default) in which all immediate children will be attached to the deleted page's parent, :MOVE-TO-ROOT in which all immediate children will be made top-level pages, or :DELETE in which case the entire subtree will be pruned." 101 (miniblog.db:delete-page id :children children))