From aec4fdecc10be35cde5dc42308960f10bc452187 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Wed, 15 Feb 2023 00:01:44 -0500 Subject: make database calls 1 conn --- src/api/posts.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/api/posts.rs') diff --git a/src/api/posts.rs b/src/api/posts.rs index 57b2ca8..bd7e665 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -9,7 +9,7 @@ use crate::{ public::docs::{EndpointDocumentation, EndpointMethod}, types::{ comment::Comment, - extract::{AuthorizedUser, Check, CheckResult, Json}, + extract::{AuthorizedUser, Check, CheckResult, Database, Json}, http::ResponseCode, like::Like, post::Post, @@ -55,9 +55,10 @@ impl Check for PostCreateRequest { async fn create( AuthorizedUser(user): AuthorizedUser, + Database(db): Database, Json(body): Json, ) -> Response { - let Ok(post) = Post::new(user.user_id, body.content) else { + let Ok(post) = Post::new(&db, user.user_id, body.content) else { return ResponseCode::InternalServerError.text("Failed to create post") }; @@ -101,9 +102,10 @@ impl Check for PostPageRequest { async fn page( AuthorizedUser(user): AuthorizedUser, + Database(db): Database, Json(body): Json, ) -> Response { - let Ok(posts) = Post::from_post_page(user.user_id, body.page) else { + let Ok(posts) = Post::from_post_page(&db, user.user_id, body.page) else { return ResponseCode::InternalServerError.text("Failed to fetch posts") }; @@ -149,9 +151,10 @@ impl Check for CommentsPageRequest { async fn comments( AuthorizedUser(_user): AuthorizedUser, + Database(db): Database, Json(body): Json, ) -> Response { - let Ok(comments) = Comment::from_comment_page(body.page, body.post_id) else { + let Ok(comments) = Comment::from_comment_page(&db, body.page, body.post_id) else { return ResponseCode::InternalServerError.text("Failed to fetch comments") }; @@ -197,9 +200,10 @@ impl Check for UsersPostsRequest { async fn user( AuthorizedUser(user): AuthorizedUser, + Database(db): Database, Json(body): Json, ) -> Response { - let Ok(posts) = Post::from_user_post_page(user.user_id, body.user_id, body.page) else { + let Ok(posts) = Post::from_user_post_page(&db, user.user_id, body.user_id, body.page) else { return ResponseCode::InternalServerError.text("Failed to fetch posts") }; @@ -251,9 +255,10 @@ impl Check for PostCommentRequest { async fn comment( AuthorizedUser(user): AuthorizedUser, + Database(db): Database, Json(body): Json, ) -> Response { - if let Err(err) = Comment::new(user.user_id, body.post_id, &body.content) { + if let Err(err) = Comment::new(&db, user.user_id, body.post_id, &body.content) { return err; } @@ -293,12 +298,16 @@ impl Check for PostLikeRequest { } } -async fn like(AuthorizedUser(user): AuthorizedUser, Json(body): Json) -> Response { +async fn like( + AuthorizedUser(user): AuthorizedUser, + Database(db): Database, + Json(body): Json, +) -> Response { if body.state { - if let Err(err) = Like::add_liked(user.user_id, body.post_id) { + if let Err(err) = Like::add_liked(&db, user.user_id, body.post_id) { return err; } - } else if let Err(err) = Like::remove_liked(user.user_id, body.post_id) { + } else if let Err(err) = Like::remove_liked(&db, user.user_id, body.post_id) { return err; } -- cgit v1.2.3-freya