From 5a2f5bf9a12b270e15757df48afeb1c1db5b34b3 Mon Sep 17 00:00:00 2001 From: Mateus Cruz Date: Mon, 5 Feb 2024 03:31:35 -0300 Subject: add db queries --- lib/operation.ml | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/operation.ml (limited to 'lib/operation.ml') diff --git a/lib/operation.ml b/lib/operation.ml new file mode 100644 index 0000000..1fb1dd5 --- /dev/null +++ b/lib/operation.ml @@ -0,0 +1,49 @@ +module TransactionType = struct + type t = + | Credit + | Debit + + let t = + let encode = function + | Credit -> "credit" + | Debit -> "debit" + in + let decode = function + | "credit" -> Ok Credit + | "debit" -> Ok Debit + | _ -> Error "Invalid transaction type" + in + Caqti_type.(enum ~encode ~decode "transaction_type") + ;; +end + +type t = + | Transaction of + { transaction_type : TransactionType.t + ; value : int + ; description : string + } + | Balance of { client_id : int } + +type transaction = + { id : int + ; client_id : int + ; value : int + ; transaction_type : TransactionType.t + ; description : string + ; created_at : Ptime.t + } + +let decoder = + let open Utils.Decoder in + let open Syntax in + let transaction_type_decoder = + literal "c" *> return TransactionType.Credit + <|> literal "d" *> return TransactionType.Debit + in + (fun value transaction_type description -> + Transaction { value; description; transaction_type }) + <$> ("valor" <: int) + <*> ("tipo" <: transaction_type_decoder) + <*> ("descricao" <: string) +;; -- cgit v1.2.3