(in-package #:cl-user) (defpackage #:nera-db (:nicknames #:nera) (:use #:cl #:app-constants #:hermetic #:ritherdon-archive.db #:utils #:user #:site-settings) (:export #:init-db #:update-user #:get-user #:get-user-id #:delete-user #:create-user #:get-site-settings #:migrate-all #:get-all-users #:update-enable-sign-on-settings #:set-home-page)) (in-package #:nera-db) (defparameter *tables* '(user site-settings) "List of the DB tables that need to be checked for migrations and DB setup.") (defun init-db (request) "Creates the database and creates Admin. in `USER' table." (destructuring-bind (&key username display-name password allow-sign-up &allow-other-keys) (utils:request-params request) (with-connection (db) ;; Add to the list to add more tables. (mapcar #'mito:ensure-table-exists *tables*) (mito:create-dao 'user :username username :display-name display-name :password (hermetic::hash password) :administrator +true+) (mito:create-dao 'site-settings :enable-sign-up (utils:checkbox-to-bool allow-sign-up))))) (defun ensure-tables-exist () "Creates missing tables from the database." (with-connection (db) (mapcar #'mito:ensure-table-exists *tables*))) (defun migrate-all () "Migrate the tables after we changed the class definition." (with-connection (db) (ensure-tables-exist) (mapcar #'mito:migrate-table *tables*))) (defun create-user (username display-name password administrator) "Add a new `USER' to the database." (with-connection (db) (mito:create-dao 'user :username username :display-name display-name :administrator administrator :password (hermetic::hash password)))) (defun delete-user (username) "Deletes `USER' from the database." (with-connection (db) (mito:delete-by-values 'user:user :username username))) (defun update-user (username &key display-name new-password) "Updates `USER' in database." (with-connection (db) (let ((user-to-update (mito:find-dao 'user:user :username username))) (if (not (utils:string-is-nil-or-empty? display-name)) (setf (user::display-name-of user-to-update) display-name)) (if (not (utils:string-is-nil-or-empty? new-password)) (setf (user::password-of user-to-update) (hermetic::hash new-password))) (mito:save-dao user-to-update)))) (defun get-user-id (username) "Returns the Id. number of the specified `USERNAME' in the database." (with-connection (db) (mito:object-id (mito:find-dao 'user :username username)))) (defun get-user (username) "Returns a `USER' profile from the database." (with-connection (db) (mito:find-dao 'user :username username))) (defun get-all-users () "Returns a list of all `USER' entries in the database." (with-connection (db) (mito:select-dao 'user (sxql:order-by (:asc :display-name))))) (defun get-site-settings () "Gets the settings for the website from the database." (with-connection (db) (mito:find-dao 'site-settings))) (defun update-enable-sign-on-settings (value) "Updates the 'Enable Sign Up' setting in the database with `VALUE'." (with-connection (db) (let ((settings-to-update (mito:find-dao 'site-settings))) (setf (site-settings::enable-sign-up-p settings-to-update) (utils:checkbox-to-bool value)) (mito:save-dao settings-to-update)))) (defun set-home-page (value) "Sets the page (in /storage/pages) to be displayed on the sites home page." (with-connection (db) (let ((settings-to-update (mito:find-dao 'site-settings))) (setf (site-settings::home-page-of settings-to-update) value) (mito:save-dao settings-to-update))))