summaryrefslogtreecommitdiff
path: root/src/api/posts.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-01-28 18:04:00 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-01-28 18:04:00 -0500
commitb58654fd70958d89b344a6f7acac204f67ae9879 (patch)
tree60a1960d0d265c9f661e633022164f33e099c81c /src/api/posts.rs
parentnew rust, clippy (diff)
downloadxssbook-b58654fd70958d89b344a6f7acac204f67ae9879.tar.gz
xssbook-b58654fd70958d89b344a6f7acac204f67ae9879.tar.bz2
xssbook-b58654fd70958d89b344a6f7acac204f67ae9879.zip
fmt
Diffstat (limited to 'src/api/posts.rs')
-rw-r--r--src/api/posts.rs74
1 files changed, 49 insertions, 25 deletions
diff --git a/src/api/posts.rs b/src/api/posts.rs
index e2e64b2..3a2e507 100644
--- a/src/api/posts.rs
+++ b/src/api/posts.rs
@@ -1,23 +1,37 @@
-use axum::{response::Response, Router, routing::{post, patch}};
+use axum::{
+ response::Response,
+ routing::{patch, post},
+ Router,
+};
use serde::Deserialize;
-use crate::types::{extract::{AuthorizedUser, Json, Check, CheckResult}, post::Post, http::ResponseCode};
-
+use crate::types::{
+ extract::{AuthorizedUser, Check, CheckResult, Json},
+ http::ResponseCode,
+ post::Post,
+};
#[derive(Deserialize)]
struct PostCreateRequest {
- content: String
+ content: String,
}
impl Check for PostCreateRequest {
fn check(&self) -> CheckResult {
- Self::assert_length(&self.content, 1, 500, "Comments must be between 1-500 characters long")?;
+ Self::assert_length(
+ &self.content,
+ 1,
+ 500,
+ "Comments must be between 1-500 characters long",
+ )?;
Ok(())
}
}
-async fn create(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostCreateRequest>) -> Response {
-
+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.text("Failed to create post")
};
@@ -31,7 +45,7 @@ async fn create(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostCreat
#[derive(Deserialize)]
struct PostPageRequest {
- page: u64
+ page: u64,
}
impl Check for PostPageRequest {
@@ -40,8 +54,10 @@ impl Check for PostPageRequest {
}
}
-async fn page(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<PostPageRequest>) -> Response {
-
+async fn page(
+ AuthorizedUser(_user): AuthorizedUser,
+ Json(body): Json<PostPageRequest>,
+) -> Response {
let Ok(posts) = Post::from_post_page(body.page) else {
return ResponseCode::InternalServerError.text("Failed to fetch posts")
};
@@ -55,7 +71,7 @@ async fn page(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<PostPageRe
#[derive(Deserialize)]
struct UsersPostsRequest {
- user_id: u64
+ user_id: u64,
}
impl Check for UsersPostsRequest {
@@ -64,8 +80,10 @@ impl Check for UsersPostsRequest {
}
}
-async fn user(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<UsersPostsRequest>) -> Response {
-
+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.text("Failed to fetch posts")
};
@@ -80,18 +98,25 @@ async fn user(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<UsersPosts
#[derive(Deserialize)]
struct PostCommentRequest {
content: String,
- post_id: u64
+ post_id: u64,
}
impl Check for PostCommentRequest {
fn check(&self) -> CheckResult {
- Self::assert_length(&self.content, 1, 255, "Comments must be between 1-255 characters long")?;
+ Self::assert_length(
+ &self.content,
+ 1,
+ 255,
+ "Comments must be between 1-255 characters long",
+ )?;
Ok(())
}
}
-async fn comment(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostCommentRequest>) -> Response {
-
+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.text("Failed to fetch posts")
};
@@ -106,7 +131,7 @@ async fn comment(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostComm
#[derive(Deserialize)]
struct PostLikeRequest {
state: bool,
- post_id: u64
+ post_id: u64,
}
impl Check for PostLikeRequest {
@@ -116,7 +141,6 @@ impl Check for PostLikeRequest {
}
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.text("Failed to fetch posts")
};
@@ -130,9 +154,9 @@ async fn like(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostLikeReq
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
+ .route("/create", post(create))
+ .route("/page", post(page))
+ .route("/user", post(user))
+ .route("/comment", patch(comment))
+ .route("/like", patch(like))
+}