A website for producing interactive charts without writing a single line of code. Built with Common Lisp and Python. https://charts.craigoates.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

127 lines
4.8 KiB

(defpackage #:storage-management
(:use #:cl
#:file
#:mito
#:local-time
#:storage)
(:import-from #:hot-line.db
#:connection-settings
#:db
#:with-connection)
(:export #:add-file-to-db
#:delete-file-from-db
#:edit-file-in-db
#:get-all-files-from-db
#:get-file-count
#:get-files-from-db
#:get-file-from-db
#:get-paginated-files))
(in-package #:storage-management)
(defun add-file-to-db (filename owner content-type slug)
"Adds an entry to the 'file' table in the database.
`CONTENT-TYPE' is more like a file extension (png, jpg, pdf, md Etc.) than
anything else. I've added to so I can differentiate the different types of files
in the website's GUI -- when managing the files in the Admin./backend parts.
NOTE: This only stores the meta-data of the uploaded file. You must use the
'storage' package to deal with the data-file associated with the database
entry."
(with-connection (db)
(mito:create-dao 'file
:filename filename
:owner owner
:content-type content-type
:slug slug)))
(defun delete-file-from-db (&key (id nil) (filename nil) (slug nil))
"Deletes the specified entry in the 'file' database.
It does not delete the actual data-file. You will need to use the 'storage'
package to do that. The database entry is the meta-data used by the
Admin./backend of the website to manage the functionality for the /storage
section."
(with-connection (db)
(cond ((and (not filename) (not slug))
(mito:delete-dao
(mito:find-dao 'file:file :id id)))
((and (not id) (not slug))
(mito:delete-dao
(mito:find-dao 'file:file :filename filename)))
((and (not id) (not filename))
(mito:delete-dao
(mito:find-dao 'file:file :slug slug))))))
(defun edit-file-in-db (id filename content-type slug)
"Updates the specified entry in the 'file' table in the database.
The entry is identified via the `ID'. That is the only argument which will not
change."
(with-connection (db)
(let ((file-to-update
(mito:find-dao 'file:file :id id)))
(setf (file::filename-of file-to-update) filename
(file::content-type-of file-to-update) content-type
(file::slug-of file-to-update) slug)
(mito:save-dao file-to-update))))
(defun get-file-count (&optional (owner nil))
"Returns the total number of entries in the 'storage' database table."
(with-connection (db)
(if (null owner)
(mito:count-dao 'file:file)
(mito:count-dao 'file:file :owner owner))))
(defun get-all-files-from-db (username order)
"Retrieves all the entries in the 'file' database for `USERNAME'.
It does not retrieve the data-files associated with the entries in the
database. You will need to use the 'storage' package alongside this to do
that. This function/package only deals with the meta-data for the files stored
in the /storage directory.
NOTE: `USERNAME' is used here (column name in the DB) but it refers to 'owner'
in the actual DB table I have used `USERNAME' here because it easier to think of
it that way from a programming point-of-view From the database's point-of-view,
'owner' can refer to more than just `USERNAME' I've just settled on that from a
programming perspective."
(with-connection (db)
(mito:select-dao 'file:file
(sxql:where (:= :owner username))
(sxql:order-by order))))
(defun get-files-from-db (username order amount &key reverse)
"Returns all the entries in the 'file' table in the database.
These files are the meta-data of the files stored in the /storage
directory. They are not the actual file. You would use this when you want to
manage the files in the /storage directory (admin./backend of website)."
(with-connection (db)
(mito:select-dao 'file:file
(sxql:where (:= :owner username))
(if reverse
(sxql:order-by (:desc order))
(sxql:order-by order))
(sxql:limit amount))))
(defun get-file-from-db (&key (id nil) (filename nil) (slug nil))
"Retrieves the specified entry from the 'file' table in the database.
It only returns the meta-data for the file stored in the /storage directory. Use
the 'storage' package alongside this one to work with the data-file and the
meta-data of the data-file."
(with-connection (db)
(cond ((and (not filename) (not slug))
(mito:find-dao 'file:file :id id))
((and (not id) (not slug))
(mito:find-dao 'file:file :filename filename))
((and (not id) (not filename))
(mito:find-dao 'file:file :slug slug))
(t nil))))
(defun get-paginated-files (username page page-amount order)
"Retrieves a list of rows from the 'file' database with an offset.
The offset is determined by watch page the viewer is on and `PAGE' and
`PAGE-AMOUNT'."
(with-connection (db)
(mito:select-dao 'file:file
(sxql:order-by order)
(sxql:where (:= :owner username))
(sxql:offset (* (- page 1) page-amount))
(sxql:limit page-amount))))