Browse Source

implement the favourites feature-set in the db package (#143).

unstable
Craig Oates 9 months ago
parent
commit
1f7abc9811
  1. 42
      src/db.lisp

42
src/db.lisp

@ -78,7 +78,10 @@
#:get-files-for-account
#:update-file
;; FAVOURITE
;; nothing implemented yet.
#:create-favourite
#:delete-favourite
#:get-favourite-recipes
#:is-favourite?
;; UNSORTED
#:delete-row-entry
#:entry-exists?))
@ -597,7 +600,42 @@ entries from the database for with the `ACCOUNT-ID'."
;; FAVOURITE
;; =============================================================================
;; Nothing implemented yet...
(defun create-favourite (account-id recipe-id)
"Adds an entry to the favourite table in the database."
(with-connection (db)
(mito:create-dao 'models:favourite
:account-id account-id
:recipe-id recipe-id)))
(defun delete-favourite (account-id recipe-id)
"Deletes the row in 'favourite' table with `ACCOUNT-ID' & `RECIPE-ID'."
(with-connection (db)
(mito:delete-by-values 'models:favourite
:account-id account-id
:recipe-id recipe-id)))
(defun get-favourite-recipes (account-id &key limit reverse)
"Returns a list of 'favourite' entries for `ACCOUNT-ID' from DB.
Use `LIMIT' to specify the maximum number of entries you would like returned
from the database."
(with-connection (db)
(let ((fav-ids
(mapcar (lambda (x) (append (models::favourite-recipe-id x)))
(mito:select-dao 'models:favourite
(sxql:where (:= :account-id account-id))))))
(when fav-ids
(mito:select-dao 'models:recipe
(sxql:where (:in :recipe-id fav-ids))
(when reverse (sxql:order-by (:desc :created-at)))
(when limit (sxql:limit limit)))))))
(defun is-favourite? (account-id recipe-id)
"Checks to see if 'favourite' entry matching `ACCOUNT-ID' & `RECIPE-ID' exists."
(with-connection (db)
(mito:find-dao 'models:favourite
:account-id account-id
:recipe-id recipe-id)))
#| UNSORTED
================================================================================

Loading…
Cancel
Save