use caqti infix
This commit is contained in:
parent
d57c359b1f
commit
6b5bfcd144
5 changed files with 102 additions and 121 deletions
|
|
@ -1,3 +1,6 @@
|
|||
open Db
|
||||
open Caqti_request.Infix
|
||||
|
||||
(** Creating the table of all messages.
|
||||
|
||||
Each message is made of :
|
||||
|
|
@ -10,27 +13,25 @@
|
|||
TODO: add date ? *)
|
||||
let () =
|
||||
let create_msg_table =
|
||||
Caqti_request.exec Caqti_type.unit
|
||||
(Caqti_type.unit ->. Caqti_type.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.Db.exec create_msg_table () with
|
||||
match Db.exec 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_request.collect
|
||||
Caqti_type.(tup2 string string)
|
||||
Caqti_type.(tup2 string string)
|
||||
(Caqti_type.(tup2 string string) ->* Caqti_type.(tup2 string string))
|
||||
"SELECT from_id, to_id FROM msg WHERE from_id=? OR to_id=?"
|
||||
in
|
||||
fun user_id ->
|
||||
let open Syntax in
|
||||
let^ comrades = Db.Db.collect_list find_comrades (user_id, user_id) 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
|
||||
|
|
@ -39,15 +40,14 @@ let find_comrades =
|
|||
(** find all messages between two товарищи *)
|
||||
let find_messages =
|
||||
let find_messages =
|
||||
Caqti_request.collect
|
||||
Caqti_type.(tup2 (tup2 string string) (tup2 string string))
|
||||
Caqti_type.(tup2 string string)
|
||||
( 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.Db.collect_list find_messages ((k1, k2), (k2, k1)) in
|
||||
let^ comrades = Db.collect_list find_messages ((k1, k2), (k2, k1)) in
|
||||
Ok comrades
|
||||
|
||||
(** display the list of discussions *)
|
||||
|
|
@ -135,13 +135,12 @@ let render_one request =
|
|||
|
||||
let insert_msg =
|
||||
let insert_msg =
|
||||
Caqti_request.exec
|
||||
Caqti_type.(tup3 string string string)
|
||||
(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.Db.exec insert_msg (from_id, to_id, msg) in
|
||||
let^ () = Db.exec insert_msg (from_id, to_id, msg) in
|
||||
Ok ()
|
||||
|
||||
(** handle posts *)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue