open caqti_type; use Q module for discuss

This commit is contained in:
Swrup 2022-04-04 21:38:09 +02:00
parent 613d901bca
commit 0632a713c7
5 changed files with 115 additions and 162 deletions

View file

@ -1,5 +1,4 @@
open Db
open Caqti_request.Infix
(** Creating the table of all messages.
@ -11,44 +10,51 @@ open Caqti_request.Infix
- some text (msg)
TODO: add date ? *)
let () =
module Q = struct
open Caqti_request.Infix
open Caqti_type
let create_msg_table =
(Caqti_type.unit ->. Caqti_type.unit)
(unit ->. unit)
"CREATE TABLE IF NOT EXISTS msg ( msg_id TEXT, from_id TEXT, to_id TEXT, \
msg TEXT, PRIMARY KEY(msg_id), FOREIGN KEY(from_id) REFERENCES \
user(user_id) ON DELETE CASCADE, FOREIGN KEY(to_id) REFERENCES \
user(user_id) ON DELETE CASCADE)"
in
match Db.exec create_msg_table () with
let find_comrades =
(tup2 string string ->* tup2 string string)
"SELECT from_id, to_id FROM msg WHERE from_id=? OR to_id=?"
let find_messages =
(tup2 (tup2 string string) (tup2 string string) ->* tup2 string string)
"SELECT from_id, msg FROM msg WHERE (from_id=? AND to_id=?) OR \
(from_id=? AND to_id=?)"
let insert_msg =
(tup3 string string string ->. unit)
"INSERT INTO msg VALUES (NULL, ?, ?, ?)"
end
let () =
match Db.exec Q.create_msg_table () with
| Ok () -> ()
| Error _e -> Dream.error (fun log -> log "can't create msg table")
(** let's find who the user is talking to so we can know if they're dangerous *)
let find_comrades =
let find_comrades =
(Caqti_type.(tup2 string string) ->* Caqti_type.(tup2 string string))
"SELECT from_id, to_id FROM msg WHERE from_id=? OR to_id=?"
let find_comrades user_id =
let open Syntax in
let^ comrades = Db.collect_list Q.find_comrades (user_id, user_id) in
let comrades =
List.map (fun (l, r) -> if l = user_id then r else l) comrades
in
fun user_id ->
let open Syntax in
let^ comrades = Db.collect_list find_comrades (user_id, user_id) in
let comrades =
List.map (fun (l, r) -> if l = user_id then r else l) comrades
in
Ok (List.sort_uniq String.compare comrades)
Ok (List.sort_uniq String.compare comrades)
(** find all messages between two товарищи *)
let find_messages =
let find_messages =
( Caqti_type.(tup2 (tup2 string string) (tup2 string string))
->* Caqti_type.(tup2 string string) )
"SELECT from_id, msg FROM msg WHERE (from_id=? AND to_id=?) OR \
(from_id=? AND to_id=?)"
in
fun k1 k2 ->
let open Syntax in
let^ comrades = Db.collect_list find_messages ((k1, k2), (k2, k1)) in
Ok comrades
let find_messages k1 k2 =
let open Syntax in
let^ comrades = Db.collect_list Q.find_messages ((k1, k2), (k2, k1)) in
Ok comrades
(** display the list of discussions *)
let render request =
@ -133,15 +139,10 @@ let render_one request =
Dream.respond ~status:`See_Other ~headers:[ ("Location", redirect_url) ] ""
| Some user_id -> pp_discussion (request, user_id, comrade_id)
let insert_msg =
let insert_msg =
(Caqti_type.(tup3 string string string) ->. Caqti_type.unit)
"INSERT INTO msg VALUES (NULL, ?, ?, ?)"
in
fun from_id to_id msg ->
let open Syntax in
let^ () = Db.exec insert_msg (from_id, to_id, msg) in
Ok ()
let insert_msg from_id to_id msg =
let open Syntax in
let^ () = Db.exec Q.insert_msg (from_id, to_id, msg) in
Ok ()
(** handle posts *)
let post request =