From 88209d88236c3d865a9f5174a0dced31920859bf Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Thu, 26 Jan 2023 17:29:16 -0500 Subject: i did things --- src/api/posts.rs | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/api/posts.rs (limited to 'src/api/posts.rs') diff --git a/src/api/posts.rs b/src/api/posts.rs new file mode 100644 index 0000000..405dfa6 --- /dev/null +++ b/src/api/posts.rs @@ -0,0 +1,102 @@ +use axum::{response::Response, Router, routing::{post, patch}}; +use serde::Deserialize; + +use crate::types::{extract::{AuthorizedUser, Json}, post::Post, response::ResponseCode}; + + +#[derive(Deserialize)] +struct PostCreateRequest { + content: String +} + +async fn create(AuthorizedUser(user): AuthorizedUser, Json(body): Json) -> Response { + + let Ok(_post) = Post::new(user.user_id, body.content) else { + return ResponseCode::InternalServerError.msg("Failed to create post") + }; + + ResponseCode::Created.msg("Successfully created new post") +} + +#[derive(Deserialize)] +struct PostPageRequest { + page: u64 +} + +async fn page(AuthorizedUser(_user): AuthorizedUser, Json(body): Json) -> Response { + + let Ok(posts) = Post::from_post_page(body.page) else { + return ResponseCode::InternalServerError.msg("Failed to fetch posts") + }; + + let Ok(json) = serde_json::to_string(&posts) else { + return ResponseCode::InternalServerError.msg("Failed to fetch posts") + }; + + ResponseCode::Success.json(&json) +} + +#[derive(Deserialize)] +struct UsersPostsRequest { + user_id: u64 +} + +async fn user(AuthorizedUser(_user): AuthorizedUser, Json(body): Json) -> Response { + + let Ok(posts) = Post::from_user_id(body.user_id) else { + return ResponseCode::InternalServerError.msg("Failed to fetch posts") + }; + + let Ok(json) = serde_json::to_string(&posts) else { + return ResponseCode::InternalServerError.msg("Failed to fetch posts") + }; + + ResponseCode::Success.json(&json) +} + +#[derive(Deserialize)] +struct PostCommentRequest { + content: String, + post_id: u64 +} + +async fn comment(AuthorizedUser(user): AuthorizedUser, Json(body): Json) -> Response { + + let Ok(mut post) = Post::from_post_id(body.post_id) else { + return ResponseCode::InternalServerError.msg("Failed to fetch posts") + }; + + if let Err(err) = post.comment(user.user_id, body.content) { + return err; + } + + ResponseCode::Success.msg("Successfully commented on post") +} + +#[derive(Deserialize)] +struct PostLikeRequest { + state: bool, + post_id: u64 +} + +async fn like(AuthorizedUser(user): AuthorizedUser, Json(body): Json) -> Response { + + let Ok(mut post) = Post::from_post_id(body.post_id) else { + return ResponseCode::InternalServerError.msg("Failed to fetch posts") + }; + + if let Err(err) = post.like(user.user_id, body.state) { + return err; + } + + ResponseCode::Success.msg("Successfully changed like status on post") +} + +pub fn router() -> Router { + Router::new() + .route("/create", post(create)) + .route("/page", post(page)) + .route("/user", post(user)) + .route("/comment", patch(comment)) + .route("/like", patch(like)) +} \ No newline at end of file -- cgit v1.2.3-freya