diff options
Diffstat (limited to 'lib/handler.ml')
-rw-r--r-- | lib/handler.ml | 232 |
1 files changed, 117 insertions, 115 deletions
diff --git a/lib/handler.ml b/lib/handler.ml index 3408184..16ada90 100644 --- a/lib/handler.ml +++ b/lib/handler.ml @@ -1,3 +1,4 @@ +open Shared open Piaf type pool = ((module Rapper_helper.CONNECTION), Caqti_error.t) Caqti_eio.Pool.t @@ -8,26 +9,27 @@ let get_posts (db_pool : pool) (_request : Request.t) = (* Use a connection from the pool. Caqti_eio.Pool.use handles acquiring and releasing the connection. *) Caqti_eio.Pool.use (fun conn -> - (* Call the get_poasts function from the Query module to fetch posts from the database. *) - let posts_or_error = Query.get_poasts () conn in - (* Pattern match on the result of the database query. *) - match posts_or_error with - (* If the query is successful, the result is a list of posts. *) - | Ok posts -> - (* Map the list of post tuples to a Yojson list. *) - let json = - `List - ((* For each post tuple, create a JSON object. *) - List.map - (fun (post : Query.post_summary) -> `Assoc [ "title", `String post.title ]) - posts) - in - (* Return a 200 OK response with the JSON body. *) - Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - (* If the query fails, log the error and return a 500 Internal Server Error response. *) - | Error err -> - Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); - Ok (Response.create `Internal_server_error)) + (* Call the get_poasts function from the Query module to fetch posts from the database. *) + let posts_or_error = Query.get_poasts () conn in + (* Pattern match on the result of the database query. *) + match posts_or_error with + (* If the query is successful, the result is a list of posts. *) + | Ok posts -> + (* Map the list of post tuples to a Yojson list. *) + let json = + `List + ((* For each post tuple, create a JSON object. *) + List.map + (fun (post : Query.post_summary) -> + `Assoc [ "title", `String post.title ]) + posts) + in + (* Return a 200 OK response with the JSON body. *) + Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) + (* If the query fails, log the error and return a 500 Internal Server Error response. *) + | Error err -> + Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); + Ok (Response.create `Internal_server_error)) db_pool ;; @@ -35,24 +37,24 @@ let get_posts (db_pool : pool) (_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 - ] - in - Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | 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)) + 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 + ] + in + Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) + | 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)) db_pool ;; @@ -60,23 +62,23 @@ let get_post post_id (db_pool : pool) (_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 - ] - in - Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) - | 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)) + 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 + ] + in + Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK) + | 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)) db_pool ;; @@ -84,26 +86,26 @@ let get_comment comment_id (db_pool : pool) (_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 comments -> - let json = - `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) - | Error err -> - Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); - Ok (Response.create `Internal_server_error)) + let comments_or_error = Query.Query.user_comments ~username conn in + match comments_or_error with + | Ok comments -> + let json = + `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) + | Error err -> + Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); + Ok (Response.create `Internal_server_error)) db_pool ;; @@ -111,26 +113,26 @@ let get_user_comments username (db_pool : pool) (_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 comments -> - let json = - `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) - | Error err -> - Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); - Ok (Response.create `Internal_server_error)) + let comments_or_error = Query.Query.post_comments ~post_id conn in + match comments_or_error with + | Ok comments -> + let json = + `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) + | Error err -> + Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); + Ok (Response.create `Internal_server_error)) db_pool ;; @@ -138,25 +140,25 @@ let get_post_comments post_id (db_pool : pool) (_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 comments -> - let json = - `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) - | Error err -> - Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); - Ok (Response.create `Internal_server_error)) + let comments_or_error = Query.Query.comment_children ~post_id:parent_id conn in + match comments_or_error with + | Ok comments -> + let json = + `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) + | Error err -> + Logs.err (fun m -> m "Database error: %a" Caqti_error.pp err); + Ok (Response.create `Internal_server_error)) db_pool ;; |