diff options
Diffstat (limited to 'lib/query.ml')
-rw-r--r-- | lib/query.ml | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/query.ml b/lib/query.ml new file mode 100644 index 0000000..0fb6f7e --- /dev/null +++ b/lib/query.ml @@ -0,0 +1,37 @@ +type pool = ((module Rapper_helper.CONNECTION), Caqti_error.t) Caqti_eio.Pool.t + +let transaction_query = + [%rapper + execute + {sql| + INSERT INTO transactions (client_id, value, type, description) + VALUES (%int{client_id}, %int{value}, %Operation.TransactionType{transaction_type}, %string{description}) + |sql}] +;; + +let client_query = + let open Client in + [%rapper + get_opt + {sql| + SELECT @int{id}, @int{mov_limit} FROM clients WHERE id = %int{id} + |sql} + record_out] +;; + +let execute_operation ~client_id ~(op : Operation.t) pool = + match op with + | Transaction data -> + Caqti_eio.Pool.use + (fun conn -> + transaction_query + ~client_id + ~value:data.value + ~transaction_type:data.transaction_type + ~description:data.description + conn) + pool + | _ -> failwith "TODO" +;; + +let find_client id pool = Caqti_eio.Pool.use (fun conn -> client_query ~id conn) pool |