This commit is contained in:
zapashcanon 2022-04-08 00:43:00 +02:00
parent 190d086206
commit ce7bb9d386
No known key found for this signature in database
GPG key ID: 8981C3C62D1D28F1
9 changed files with 442 additions and 500 deletions

View file

@ -1,5 +1,6 @@
open Db
open Syntax
open Caqti_request.Infix
open Caqti_type
type t =
{ name : string
@ -8,84 +9,28 @@ type t =
; thumbnail : string
}
module Q = struct
open Caqti_request.Infix
open Caqti_type
let create_info_table =
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS image_info (post_id TEXT, image_name TEXT, \
image_alt TEXT, FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON \
DELETE CASCADE)"
let create_content_table =
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS image_content (post_id TEXT, content TEXT, \
FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE CASCADE)"
let create_thumbnail_table =
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS image_thumbnail (post_id TEXT, content TEXT, \
FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE CASCADE)"
let upload_info =
(tup3 string string string ->. unit) "INSERT INTO image_info VALUES (?,?,?)"
let upload_content =
(tup2 string string ->. unit) "INSERT INTO image_content VALUES (?,?)"
let upload_thumbnail =
(tup2 string string ->. unit) "INSERT INTO image_thumbnail VALUES (?,?)"
let get_post_content =
(string ->? string) "SELECT content FROM image_content WHERE post_id=?"
let get_post_thumbnail =
(string ->? string) "SELECT content FROM image_thumbnail WHERE post_id=?"
let get_post_info =
(string ->? tup2 string string)
"SELECT image_name,image_alt FROM image_info WHERE post_id=?"
(*avatars*)
let create_user_content_table =
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS user_image_content (user_id TEXT, content \
TEXT, FOREIGN KEY(user_id) REFERENCES user(user_id) ON DELETE CASCADE)"
let create_user_thumbnail_table =
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS user_image_thumbnail (user_id TEXT, content \
TEXT, FOREIGN KEY(user_id) REFERENCES user(user_id) ON DELETE CASCADE)"
let upload_user_content =
(tup2 string string ->. unit) "INSERT INTO user_image_content VALUES (?,?)"
let upload_user_thumbnail =
(tup2 string string ->. unit)
"INSERT INTO user_image_thumbnail VALUES (?,?)"
let get_user_content =
(string ->? string) "SELECT content FROM user_image_content WHERE user_id=?"
let get_user_thumbnail =
(string ->? string)
"SELECT content FROM user_image_thumbnail WHERE user_id=?"
let delete_user_content =
(string ->. unit) "DELETE FROM user_image_content WHERE user_id=?"
let delete_user_thumbnail =
(string ->. unit) "DELETE FROM user_image_thumbnail WHERE user_id=?"
end
let () =
let tables =
[| Q.create_info_table
; Q.create_content_table
; Q.create_thumbnail_table
; Q.create_user_content_table
; Q.create_user_thumbnail_table
[| (unit ->. unit)
"CREATE TABLE IF NOT EXISTS image_info (post_id TEXT, image_name \
TEXT, image_alt TEXT, FOREIGN KEY(post_id) REFERENCES \
post_user(post_id) ON DELETE CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS image_content (post_id TEXT, content \
TEXT, FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS image_thumbnail (post_id TEXT, content \
TEXT, FOREIGN KEY(post_id) REFERENCES post_user(post_id) ON DELETE \
CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS user_image_content (user_id TEXT, content \
TEXT, FOREIGN KEY(user_id) REFERENCES user(user_id) ON DELETE \
CASCADE)"
; (unit ->. unit)
"CREATE TABLE IF NOT EXISTS user_image_thumbnail (user_id TEXT, \
content TEXT, FOREIGN KEY(user_id) REFERENCES user(user_id) ON \
DELETE CASCADE)"
|]
in
if
@ -93,38 +38,68 @@ let () =
(Array.map (fun query -> Db.exec query ()) tables)
then Dream.error (fun log -> log "can't create images tables")
let upload_info =
Db.exec
@@ (tup3 string string string ->. unit)
"INSERT INTO image_info VALUES (?,?,?)"
let upload_content =
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO image_content VALUES (?,?)"
let upload_thumbnail =
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO image_thumbnail VALUES (?,?)"
let get_content =
Db.find_opt
@@ (string ->? string) "SELECT content FROM image_content WHERE post_id=?"
let get_thumbnail =
Db.find_opt
@@ (string ->? string) "SELECT content FROM image_thumbnail WHERE post_id=?"
let get_info =
Db.find_opt
@@ (string ->? tup2 string string)
"SELECT image_name,image_alt FROM image_info WHERE post_id=?"
let upload_user_content =
Db.exec
@@ (tup2 string string ->. unit) "INSERT INTO user_image_content VALUES (?,?)"
let upload_user_thumbnail =
Db.exec
@@ (tup2 string string ->. unit)
"INSERT INTO user_image_thumbnail VALUES (?,?)"
let get_user_content =
Db.find_opt
@@ (string ->? string)
"SELECT content FROM user_image_content WHERE user_id=?"
let get_user_thumbnail =
Db.find_opt
@@ (string ->? string)
"SELECT content FROM user_image_thumbnail WHERE user_id=?"
let delete_user_content =
Db.exec @@ (string ->. unit) "DELETE FROM user_image_content WHERE user_id=?"
let delete_user_thumbnail =
Db.exec
@@ (string ->. unit) "DELETE FROM user_image_thumbnail WHERE user_id=?"
let upload image id =
let^ () = Db.exec Q.upload_info (id, image.name, image.alt) in
let^ () = Db.exec Q.upload_content (id, image.content) in
let^ () = Db.exec Q.upload_thumbnail (id, image.thumbnail) in
Ok ()
let get_content id =
let^ content = Db.find_opt Q.get_post_content id in
Ok content
let get_thumbnail id =
let^ thumbnail = Db.find_opt Q.get_post_thumbnail id in
Ok thumbnail
let get_info id =
let^ info = Db.find_opt Q.get_post_info id in
Ok info
let* () = upload_info (id, image.name, image.alt) in
let* () = upload_content (id, image.content) in
upload_thumbnail (id, image.thumbnail)
let upload_avatar image id =
let^ () = Db.exec Q.delete_user_content id in
let^ () = Db.exec Q.delete_user_thumbnail id in
let^ () = Db.exec Q.upload_user_content (id, image.content) in
let^ () = Db.exec Q.upload_user_thumbnail (id, image.thumbnail) in
Ok ()
let get_user_content id =
let^ content = Db.find_opt Q.get_user_content id in
Ok content
let get_user_thumbnail id =
let^ thumbnail = Db.find_opt Q.get_user_thumbnail id in
Ok thumbnail
let* () = delete_user_content id in
let* () = delete_user_thumbnail id in
let* () = upload_user_content (id, image.content) in
upload_user_thumbnail (id, image.thumbnail)
let make_thumbnail content =
let open Bos in