summaryrefslogtreecommitdiff
path: root/src/api/posts.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/posts.rs')
-rw-r--r--src/api/posts.rs102
1 files changed, 102 insertions, 0 deletions
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<PostCreateRequest>) -> 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<PostPageRequest>) -> 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<UsersPostsRequest>) -> 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<PostCommentRequest>) -> 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<PostLikeRequest>) -> 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