diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-15 00:01:44 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-15 00:01:44 -0500 |
commit | aec4fdecc10be35cde5dc42308960f10bc452187 (patch) | |
tree | 67233229c6839c78d1bd3db0147467da30843f44 /src/api/posts.rs | |
parent | bug fixes (diff) | |
download | xssbook-aec4fdecc10be35cde5dc42308960f10bc452187.tar.gz xssbook-aec4fdecc10be35cde5dc42308960f10bc452187.tar.bz2 xssbook-aec4fdecc10be35cde5dc42308960f10bc452187.zip |
make database calls 1 conn
Diffstat (limited to 'src/api/posts.rs')
-rw-r--r-- | src/api/posts.rs | 27 |
1 files changed, 18 insertions, 9 deletions
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<PostCreateRequest>, ) -> 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<PostPageRequest>, ) -> 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<CommentsPageRequest>, ) -> 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<UsersPostsRequest>, ) -> 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<PostCommentRequest>, ) -> 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<PostLikeRequest>) -> Response { +async fn like( + AuthorizedUser(user): AuthorizedUser, + Database(db): Database, + Json(body): Json<PostLikeRequest>, +) -> 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; } |