open Piaf type pool = ((module Rapper_helper.CONNECTION), Caqti_error.t) Caqti_eio.Pool.t (* This is the main handler function for the GET /posts endpoint. *) (* It takes a database connection pool and a Piaf request as input. *) 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)) db_pool ;; (* Handler for GET /posts/:id - gets a single post by ID *) 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)) db_pool ;; (* Handler for GET /comments/:id - gets a single comment by ID *) 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)) db_pool ;; (* Handler for GET /users/:username/comments - gets comments by a user *) 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)) db_pool ;; (* Handler for GET /posts/:id/comments - gets comments for a post *) 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)) db_pool ;; (* Handler for GET /comments/:id/children - gets child comments *) 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)) db_pool ;;