src/view/admin/post-list.lisp

DOWNLOAD
(defpackage murja.view.admin.post-list
  (:use :cl :binding-arrows :spinneret
   :easy-routes
   :murja.settings :cl-hash-util
   :murja.view.components.blogpost)
  (:import-from :murja.view.components.tabs :deftab)
  (:import-from :murja.model.post :admin-get-all-titles :title :month :year :id :tags :hidden :unlisted)
  (:export :blog/postadmin))

(in-package :murja.view.admin.post-list)

(defun admin-post-row (title-to-print)
  (with-slots (title month year id tags hidden unlisted) title-to-print
    (with-html
      (:div.title-flex-container
       (:span.post-admin-title (format nil "~a - ~a ~d" title month year))
       (:a :href (format nil "/blog/post/editor/~d" id) "Edit")
       (:div.post-admin-title
	(:h* "Tags")
	(:ul
	 (dolist (tag tags)
	   (:li
	    (:a :href (format nil "/blog/tags/~a" tag) tag)))))))))

(defun tagcloud ()
  (let* ((all-tags
	   (postmodern:query "select distinct (replace((elements->0)::text, '\"', ''))
from blog.post as orig_post,
     jsonb_array_elements(tags) as elements;" :column))
	 ;; there is probably a way to do this with a single query, but I couldn't codegolf it 
	 (counted
	   (map 'list (lambda (tag)
			(cons tag (caar (postmodern:query "SELECT count(*) from blog.post where tags ? $1" tag))))
		all-tags)))
    ;; (("lol" . 2) ("hehe" . 2) ("ilpo" . 1) ("säätää" . 1) ("tag1" . 1) ("tag2" . 1))

    
    
    (with-html
      (:section
       (:h* ("Tags in the system (~d)" (length counted)))
       (:ul
	(dolist (c counted)
	  (destructuring-bind (tag . count) c
	    (:li (:a :href (format nil "/blog/tags/~a" tag)
		     (format nil "~a (~d)" tag count))))))))))
     

(deftab blog/postadmin (:url "/blog/postadmin"
			:title "Manage posts"
			:require-login t
			:needed-abilities ("create-post" "delete-post" "edit-post"))
  (setf murja.view.components.root:*inject-to-sidebar* #'tagcloud)
  (let ((titles (admin-get-all-titles)))
    (:div.vertical-flex-container
     (dolist (title titles)
       (admin-post-row title)))))