From 718e6daeb90b89c5adeb6705a38f4de2e84387bc Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Mon, 10 Jan 2022 23:44:24 +0000 Subject: [PATCH] initial Chapter 14 commit (code is broken). I have lost control of the code base and motivation to clean it up at this moment in time. I am going to leave the project in its current state and move on to another project to take what I have learned and apply it in a more focuses manner. I might come back to this repo. at some point in the future but, if I do not, consider this project broken and do not try to use it in any way. --- src/model.lisp | 101 +++++++++++++++++++++++++++-------- src/web.lisp | 20 +++++-- templates/entries/index.html | 4 +- 3 files changed, 98 insertions(+), 27 deletions(-) diff --git a/src/model.lisp b/src/model.lisp index 027c792..c9a9c6e 100644 --- a/src/model.lisp +++ b/src/model.lisp @@ -24,6 +24,17 @@ #:sex)) (in-package #:rails-to-caveman.model) +(defclass vote() + ((entry + :col-type entry + :initarg :entry + :accessor entry-of) + (user + :col-type user + :initarg :user + :accessor user-of)) + (:metaclass mito:dao-table-class)) + (defclass article () ((title :initarg :title :col-type (:varchar 80) @@ -98,6 +109,26 @@ :inflate #'cl-pass:hash )) (:metaclass mito:dao-table-class)) +(defmethod mito:delete-dao :before ((user user)) + (mito:delete-by-values 'vote + :user-id(mito:object-id user)) + (mito:delete-by-values 'entry + :user-id (mito:object-id user))) + +(defmethod mito:delete-dao :before ((o entry)) + (mito:delete-by-values 'vote + :entry-id (mito:object-id o)) + (mito:delete-by-values 'entry-image + :entry-id (mito:object-id o))) + +(defun votablep (entry user) + (and entry (not (mito:object= user (author-of entry))) + (null (mito:select-dao 'vote + (sxql:where + (:and + (:= :user-id (mito:object-id user)) + (:= :entry-id (mito:object-id entry)))) + (sxql:limit 1))))) (defun ids () "Produces a list of all the Id's in the database. Part of Chapter 4 @@ -538,8 +569,21 @@ reference for future projects. This is a learning project after all. ;; 'user' class definition highlighting this. :administrator (zerop 0) :password "asagao!" - :filename (when (zerop x) "profile.png") - :content-type (when (zerop x) "image/png"))) + :filename "profile.png" ; (when (zerop x) "profile.png") + :content-type "image/png") ; (when (zerop x) "image/png")) + ;; PROBLEM IS IN HERE.... + (with-open-file (s (merge-pathnames #P"profile.png" + rails-to-caveman.config::*application-root*) + :element-type '(unsigned-byte 8)) + (let ((vector (make-array + (file-length s) + :element-type '(unsigned-byte 8)))) + (read-sequence vector s) + (rails-to-caveman.storage::write + (make-instance 'flex::vector-input-stream :vector vector) x "account" + (rails-to-caveman.storage::make-file :name "profile.png" :content-type "image/png")) + ;; PROBLEM IS IN HERE.... + ))) ;; "#.(" is an ARRAY LITERAL. (Noted in 'Loving Common Lisp' book). (let ((body #.(with-output-to-string (*standard-output*) (format t "Morning glory wins.~2%") @@ -555,27 +599,40 @@ reference for future projects. This is a learning project after all. :date-expired (local-time:timestamp- now-time (- 2 x) :day) :member-only (zerop 0))) (dolist (name '("Jiro" "Taro" "Hana")) + ;; replace here... (let ((user (mito:find-dao 'user :name name))) (when user (dotimes (x 10) - (mito:create-dao 'entry - :user user - :title (format nil "Title~D" x) - :body body - :date-posted (local-time:timestamp- now-time (- 10 x) :day) - :status (nth (rem x 3) '("draft" "member-only" "public"))))))) + ;; (mito:create-dao 'entry + ;; :user user + ;; :title (format nil "Title~D" x) + ;; :body body + ;; :date-posted (local-time:timestamp- now-time (- 10 x) :day) + ;; :status (nth (rem x 3) '("draft" "member-only" "public"))))))) + (let ((entry (mito:create-dao 'entry + :user user + :title (format nil "Title~D" x) + :body body + :date-posted (local-time:timestamp- now-time (- 10 x) :day) + :status (nth (rem x 3)'("draft" "member-only" "public"))))) + (when (find x '(7 8)) + (dolist (name '("John" "Mike" "Sophy")) + (let ((voter (mito:find-dao 'user :name name))) + (mito:create-dao 'vote :user voter :entry entry)))))))))) ))) + ;; PROBLEM IS IN HERE.... - (with-open-file (s (merge-pathnames #P"profile.png" - rails-to-caveman.config::*application-root*) - :element-type '(unsigned-byte 8)) - (let ((vector (make-array - (file-length s) - :element-type '(unsigned-byte 8)))) - (read-sequence vector s) - (rails-to-caveman.storage::write - (make-instance 'flex::vector-input-stream :vector vector) 1 "account" - (rails-to-caveman.storage::make-file :name "profile.png" :content-type "image/png")) - ;; PROBLEM IS IN HERE.... - )))))) + ;; (with-open-file (s (merge-pathnames #P"profile.png" + ;; rails-to-caveman.config::*application-root*) + ;; :element-type '(unsigned-byte 8)) + ;; (let ((vector (make-array + ;; (file-length s) + ;; :element-type '(unsigned-byte 8)))) + ;; (read-sequence vector s) + ;; (rails-to-caveman.storage::write + ;; (make-instance 'flex::vector-input-stream :vector vector) 1 "account" + ;; (rails-to-caveman.storage::make-file :name "profile.png" :content-type "image/png")) + ;; ;; PROBLEM IS IN HERE.... + ;; )))))) +;; )))) #| (defun seeds() (with-open-file (s (merge-pathnames "profile.png" @@ -600,6 +657,6 @@ reference for future projects. This is a learning project after all. (defun rebuild () "Drops the current database table, recreates it and populates it using seeded data." (with-connection (db) - (mapc #'mito:ensure-table-exists '(user article entry)) - (mapc #'mito:recreate-table '(user article entry))) + (mapc #'mito:ensure-table-exists '(user article entry entry-image vote)) + (mapc #'mito:recreate-table '(user article entry entry-image vote))) (seeds)) diff --git a/src/web.lisp b/src/web.lisp index 8d1c091..2789f0d 100644 --- a/src/web.lisp +++ b/src/web.lisp @@ -416,7 +416,12 @@ nil "/users/~D"(mito:object-id user)))))))) :news ,(articles-make 5) :blogs ,(entries :limit 5) :articles ,(articles-make 5) - :entries ,(entries :author author) + :entry ,(entries :author author) + :entries ,(loop :for entry :in (entries :limit 5) + :for votes = (mito:count-dao 'rails-to-caveman.model::vote + :entry-id (mito:object-id author)) + :collect (cons author (and (< 0 votes) votes))) + ;; (entries :author author) ,@(roles) :token ,(token) ;; :member ,(rails-to-caveman.model::author-of entry)))))) @@ -435,7 +440,10 @@ nil "/users/~D"(mito:object-id user)))))))) :user ,(current-user) :entries ,entries :blogs ,entries - :news ,(articles 5) + :news ,(articles-make 5) + :articles ,(articles-make 5) + :images ,(mito:retrieve-dao 'rails-to-caveman.model::entry-image + :entry-id (mito:object-id entry)) :member ,(rails-to-caveman.model::author-of entry))))))) (defroute create-entry ("/entries" :method :post) ; :post was :put @@ -532,9 +540,12 @@ nil "/users/~D"(mito:object-id user)))))))) :entry ,(make-instance 'rails-to-caveman.model::entry :date-posted (local-time:now)) + :entries ,(make-instance 'rails-to-caveman.model::entry + :date-posted + (local-time:now)) :user ,(current-user) :blogs ,(entries :limit 5) - :news ,(articles 5))))) + :news ,(articles-make 5))))) (defroute "/entries/:id/edit" (&key id) (if (null (ignore-errors (parse-integer id))) @@ -1205,6 +1216,7 @@ nil "/users/~D"(mito:object-id user)))))))) (defroute index-entry-image "/entries/:id/images" (&key id) (with-authenticity-check (:logged-in) + (with-connection (db) (ensure-let ((entry (mito:find-dao 'rails-to-caveman.model::entry :id id))) (let ((images (mito:retrieve-dao 'rails-to-caveman.model::entry-image @@ -1219,7 +1231,7 @@ nil "/users/~D"(mito:object-id user)))))))) :token ,(token) :news ,(articles-make 5) :articles ,(articles-make 5) - :blogs ,(entries :limit 5) )))))) + :blogs ,(entries :limit 5) ))))))) (defroute "/about" () ;; about.html should be in the /templates directory. diff --git a/templates/entries/index.html b/templates/entries/index.html index a717ab8..d86e84b 100644 --- a/templates/entries/index.html +++ b/templates/entries/index.html @@ -28,7 +28,9 @@

{{entry.title}}

{{ entry.body | truncatechars: 80 }}

More - +{% if votes %} +★{{votes}} + {% endif %} {% include "entries/footer.html" %} {% endfor %} {% else %}