diff options
Diffstat (limited to 'litedb/handler.ml')
-rw-r--r-- | litedb/handler.ml | 113 |
1 files changed, 58 insertions, 55 deletions
diff --git a/litedb/handler.ml b/litedb/handler.ml index dd9251f..3408184 100644 --- a/litedb/handler.ml +++ b/litedb/handler.ml @@ -32,25 +32,24 @@ let get_posts (db_pool : pool) (_request : Request.t) = ;; (* Handler for GET /posts/:id - gets a single post by ID *) -let get_post (db_pool : pool) post_id (_request : Request.t) = +let get_post post_id (db_pool : pool) (_request : Request.t) = Caqti_eio.Pool.use (fun conn -> let post_or_error = Query.get_poast post_id conn in match post_or_error with | Ok (Some post) -> let json = - `Assoc [ - "id", `Int post.id; - "title", `String post.title; - "content", `String post.content; - "date", `String post.date; - "tags", `String post.tags; - "url", `String post.url - ] + `Assoc + [ "id", `Int post.id + ; "title", `String post.title + ; "content", `String post.content + ; "date", `String post.date + ; "tags", `String post.tags + ; "url", `String post.url + ] in Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | Ok None -> - Ok (Response.create `Not_found) + | Ok None -> Ok (Response.create `Not_found) | Error err -> Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); Ok (Response.create `Internal_server_error)) @@ -58,24 +57,23 @@ let get_post (db_pool : pool) post_id (_request : Request.t) = ;; (* Handler for GET /comments/:id - gets a single comment by ID *) -let get_comment (db_pool : pool) comment_id (_request : Request.t) = +let get_comment comment_id (db_pool : pool) (_request : Request.t) = Caqti_eio.Pool.use (fun conn -> let comment_or_error = Query.Query.comment ~id:comment_id conn in match comment_or_error with | Ok (Some comment) -> let json = - `Assoc [ - "id", `Int comment.id; - "content", `String comment.content; - "date", `String comment.date; - "tags", `String comment.tags; - "url", `String comment.url - ] + `Assoc + [ "id", `Int comment.id + ; "content", `String comment.content + ; "date", `String comment.date + ; "tags", `String comment.tags + ; "url", `String comment.url + ] in Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | Ok None -> - Ok (Response.create `Not_found) + | Ok None -> Ok (Response.create `Not_found) | Error err -> Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); Ok (Response.create `Internal_server_error)) @@ -83,25 +81,26 @@ let get_comment (db_pool : pool) comment_id (_request : Request.t) = ;; (* Handler for GET /users/:username/comments - gets comments by a user *) -let get_user_comments (db_pool : pool) username (_request : Request.t) = +let get_user_comments username (db_pool : pool) (_request : Request.t) = Caqti_eio.Pool.use (fun conn -> let comments_or_error = Query.Query.user_comments ~username conn in match comments_or_error with - | Ok (Some comment) -> + | Ok comments -> let json = - `Assoc [ - "id", `Int comment.id; - "content", `String comment.content; - "date", `String comment.date; - "tags", `String comment.tags; - "url", `String comment.url - ] + `List + (List.map + (fun (comment : Query.comment) -> + `Assoc + [ "id", `Int comment.id + ; "content", `String comment.content + ; "date", `String comment.date + ; "tags", `String comment.tags + ; "url", `String comment.url + ]) + comments) in Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | Ok None -> - (* Return empty array if no comments found *) - Ok (Response.of_string ~body:"[]" `OK) | Error err -> Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); Ok (Response.create `Internal_server_error)) @@ -109,24 +108,26 @@ let get_user_comments (db_pool : pool) username (_request : Request.t) = ;; (* Handler for GET /posts/:id/comments - gets comments for a post *) -let get_post_comments (db_pool : pool) post_id (_request : Request.t) = +let get_post_comments post_id (db_pool : pool) (_request : Request.t) = Caqti_eio.Pool.use (fun conn -> let comments_or_error = Query.Query.post_comments ~post_id conn in match comments_or_error with - | Ok (Some comment) -> + | Ok comments -> let json = - `Assoc [ - "id", `Int comment.id; - "content", `String comment.content; - "date", `String comment.date; - "tags", `String comment.tags; - "url", `String comment.url - ] + `List + (List.map + (fun (comment : Query.comment) -> + `Assoc + [ "id", `Int comment.id + ; "content", `String comment.content + ; "date", `String comment.date + ; "tags", `String comment.tags + ; "url", `String comment.url + ]) + comments) in Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | Ok None -> - Ok (Response.of_string ~body:"[]" `OK) | Error err -> Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); Ok (Response.create `Internal_server_error)) @@ -134,24 +135,26 @@ let get_post_comments (db_pool : pool) post_id (_request : Request.t) = ;; (* Handler for GET /comments/:id/children - gets child comments *) -let get_comment_children (db_pool : pool) parent_id (_request : Request.t) = +let get_comment_children parent_id (db_pool : pool) (_request : Request.t) = Caqti_eio.Pool.use (fun conn -> let comments_or_error = Query.Query.comment_children ~post_id:parent_id conn in match comments_or_error with - | Ok (Some comment) -> + | Ok comments -> let json = - `Assoc [ - "id", `Int comment.id; - "content", `String comment.content; - "date", `String comment.date; - "tags", `String comment.tags; - "url", `String comment.url - ] + `List + (List.map + (fun (comment : Query.comment) -> + `Assoc + [ "id", `Int comment.id + ; "content", `String comment.content + ; "date", `String comment.date + ; "tags", `String comment.tags + ; "url", `String comment.url + ]) + comments) in Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | Ok None -> - Ok (Response.of_string ~body:"[]" `OK) | Error err -> Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); Ok (Response.create `Internal_server_error)) |