Skip to content

Commit

Permalink
Add endpoint for GET payment by id
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Mar 24, 2024
1 parent 0e93bd9 commit 7868746
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
18 changes: 17 additions & 1 deletion api/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ let post_payments request =
|> Yojson.Safe.to_string
|> Dream.json ~status:`Created

let get_payment request =
let payment_id = Dream.param request "id" in

let%lwt payment = Dream.sql request (Payment.find payment_id) in

match payment with
| Some payment ->
let response =
(fun (id, owner, amount, created_at) -> {id; owner; amount; created_at})
payment
in

yojson_of_payment_creation response |> Yojson.Safe.to_string |> Dream.json
| None -> Dream.json ~status:`Not_Found ""

let () =
let postgres_url =
match Sys.getenv_opt "DATABASE_URL" with
Expand All @@ -40,5 +55,6 @@ let () =
@@ Dream.router
[
Dream.get "/" (fun _ -> Dream.html "hello world");
Dream.post "/payments/" post_payments;
Dream.scope "/payments" []
[Dream.post "/" post_payments; Dream.get "/:id/" get_payment];
]
12 changes: 12 additions & 0 deletions api/models/payment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ let create =
Db.find query (payment.owner, payment.amount, payment.description)
in
Caqti_lwt.or_fail result

let find =
let query =
let open Caqti_request.Infix in
(T.string ->? T.(tup4 string string float string))
{|
SELECT id, owner, amount, created_at FROM payment WHERE id = $1
|}
in
fun (id : string) (module Db : DB) ->
let%lwt result = Db.find_opt query id in
Caqti_lwt.or_fail result
14 changes: 0 additions & 14 deletions api/models/payment.mli

This file was deleted.

0 comments on commit 7868746

Please sign in to comment.