-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* comments impl * optionals removed * dionis gigachad * limit up
- Loading branch information
Showing
18 changed files
with
375 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::request_content::CommentsRequestContent; | ||
use super::response_content_failure::CommentsResponseContentFailure; | ||
use super::response_content_failure::CommentsResponseContentFailure::*; | ||
use super::response_content_success::CommentsResponseContentSuccess; | ||
use crate::extensions::Resolve; | ||
use blog_server_services::traits::comment_service::CommentService; | ||
use blog_server_services::traits::post_service::PostService; | ||
use screw_api::request::ApiRequest; | ||
use screw_api::response::ApiResponse; | ||
use std::sync::Arc; | ||
|
||
async fn handler( | ||
post_slug: String, | ||
offset: Option<i64>, | ||
limit: Option<i64>, | ||
comment_service: Arc<Box<dyn CommentService>>, | ||
post_service: Arc<Box<dyn PostService>>, | ||
) -> Result<CommentsResponseContentSuccess, CommentsResponseContentFailure> { | ||
if post_slug.is_empty() { | ||
return Err(PostSlugEmpty); | ||
} | ||
|
||
let post = post_service | ||
.post_by_slug(&post_slug) | ||
.await | ||
.map_err(|e| DatabaseError { | ||
reason: e.to_string(), | ||
})? | ||
.ok_or(PostNotFound)?; | ||
|
||
let offset = offset.unwrap_or(0).max(0); | ||
let limit = limit.unwrap_or(200).max(0).min(200); | ||
|
||
let (comments_result, total_result) = tokio::join!( | ||
comment_service.comments_by_post_id(&post.id, &offset, &limit), | ||
comment_service.comments_count_by_post_id(&post.id), | ||
); | ||
|
||
let comments = comments_result | ||
.map_err(|e| DatabaseError { | ||
reason: e.to_string(), | ||
})? | ||
.into_iter() | ||
.map(|a| a.into()) | ||
.collect(); | ||
|
||
let total = total_result.map_err(|e| DatabaseError { | ||
reason: e.to_string(), | ||
})?; | ||
|
||
Ok(CommentsResponseContentSuccess { | ||
comments, | ||
total, | ||
offset, | ||
limit, | ||
}) | ||
} | ||
|
||
pub async fn http_handler<Extensions>( | ||
request: ApiRequest<CommentsRequestContent, Extensions>, | ||
) -> ApiResponse<CommentsResponseContentSuccess, CommentsResponseContentFailure> | ||
where | ||
Extensions: Resolve<Arc<Box<dyn CommentService>>> + Resolve<Arc<Box<dyn PostService>>>, | ||
{ | ||
ApiResponse::from( | ||
handler( | ||
request.content.post_slug, | ||
request.content.offset, | ||
request.content.limit, | ||
request.content.comment_service, | ||
request.content.post_service, | ||
) | ||
.await, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod handler; | ||
mod request_content; | ||
mod response_content_failure; | ||
mod response_content_success; | ||
|
||
pub use handler::http_handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::extensions::Resolve; | ||
use blog_server_services::traits::comment_service::*; | ||
use blog_server_services::traits::post_service::*; | ||
use screw_api::request::{ApiRequestContent, ApiRequestOriginContent}; | ||
use std::sync::Arc; | ||
|
||
pub struct CommentsRequestContent { | ||
pub(super) post_slug: String, | ||
pub(super) offset: Option<i64>, | ||
pub(super) limit: Option<i64>, | ||
pub(super) comment_service: Arc<Box<dyn CommentService>>, | ||
pub(super) post_service: Arc<Box<dyn PostService>>, | ||
} | ||
|
||
impl<Extensions> ApiRequestContent<Extensions> for CommentsRequestContent | ||
where | ||
Extensions: Resolve<Arc<Box<dyn CommentService>>> + Resolve<Arc<Box<dyn PostService>>>, | ||
{ | ||
type Data = (); | ||
|
||
fn create(origin_content: ApiRequestOriginContent<Self::Data, Extensions>) -> Self { | ||
Self { | ||
post_slug: origin_content | ||
.path | ||
.get("post_slug") | ||
.map(|n| n.to_owned()) | ||
.unwrap_or_default(), | ||
offset: origin_content | ||
.query | ||
.get("offset") | ||
.map(|v| v.parse().ok()) | ||
.flatten(), | ||
limit: origin_content | ||
.query | ||
.get("limit") | ||
.map(|v| v.parse().ok()) | ||
.flatten(), | ||
comment_service: origin_content.extensions.resolve(), | ||
post_service: origin_content.extensions.resolve(), | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
blog-server-api/src/endpoints/comments/response_content_failure.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use hyper::StatusCode; | ||
use screw_api::response::{ApiResponseContentBase, ApiResponseContentFailure}; | ||
|
||
pub enum CommentsResponseContentFailure { | ||
DatabaseError { reason: String }, | ||
PostSlugEmpty, | ||
PostNotFound, | ||
} | ||
|
||
impl ApiResponseContentBase for CommentsResponseContentFailure { | ||
fn status_code(&self) -> &'static StatusCode { | ||
match self { | ||
CommentsResponseContentFailure::DatabaseError { reason: _ } => { | ||
&StatusCode::INTERNAL_SERVER_ERROR | ||
} | ||
CommentsResponseContentFailure::PostSlugEmpty => &StatusCode::BAD_REQUEST, | ||
CommentsResponseContentFailure::PostNotFound => &StatusCode::NOT_FOUND, | ||
} | ||
} | ||
} | ||
|
||
impl ApiResponseContentFailure for CommentsResponseContentFailure { | ||
fn identifier(&self) -> &'static str { | ||
match self { | ||
CommentsResponseContentFailure::DatabaseError { reason: _ } => { | ||
"COMMENTS_DATABASE_ERROR" | ||
} | ||
CommentsResponseContentFailure::PostSlugEmpty => "COMMENTS_POST_SLUG_EMPTY", | ||
CommentsResponseContentFailure::PostNotFound => "COMMENTS_POST_NOT_FOUND", | ||
} | ||
} | ||
|
||
fn reason(&self) -> Option<String> { | ||
Some(match self { | ||
CommentsResponseContentFailure::DatabaseError { reason } => { | ||
if cfg!(debug_assertions) { | ||
format!("database error: {}", reason) | ||
} else { | ||
"internal database error".to_string() | ||
} | ||
} | ||
CommentsResponseContentFailure::PostSlugEmpty => { | ||
"comments root post slug is empty in request URL".to_string() | ||
} | ||
CommentsResponseContentFailure::PostNotFound => { | ||
"comments root post record not found in database".to_string() | ||
} | ||
}) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
blog-server-api/src/endpoints/comments/response_content_success.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::entities::Comment; | ||
use hyper::StatusCode; | ||
use screw_api::response::{ApiResponseContentBase, ApiResponseContentSuccess}; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct CommentsResponseContentSuccess { | ||
pub(super) comments: Vec<Comment>, | ||
pub(super) total: i64, | ||
pub(super) offset: i64, | ||
pub(super) limit: i64, | ||
} | ||
|
||
impl ApiResponseContentBase for CommentsResponseContentSuccess { | ||
fn status_code(&self) -> &'static StatusCode { | ||
&StatusCode::OK | ||
} | ||
} | ||
|
||
impl ApiResponseContentSuccess for CommentsResponseContentSuccess { | ||
type Data = Self; | ||
|
||
fn identifier(&self) -> &'static str { | ||
"COMMENTS_OK" | ||
} | ||
|
||
fn description(&self) -> Option<String> { | ||
Some("comments list returned".to_string()) | ||
} | ||
|
||
fn data(&self) -> &Self::Data { | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod author; | ||
pub mod author_me; | ||
pub mod authors; | ||
pub mod comments; | ||
pub mod login; | ||
pub mod post; | ||
pub mod posts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use super::*; | ||
use blog_server_services::traits::comment_service::Comment as ServiceComment; | ||
use serde::Serialize; | ||
|
||
#[derive(Clone, Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Comment { | ||
pub post_id: i64, | ||
pub created_at: i64, | ||
pub content: String, | ||
pub short_author: ShortAuthor, | ||
} | ||
|
||
impl Into<Comment> for ServiceComment { | ||
fn into(self) -> Comment { | ||
Comment { | ||
post_id: self.base.post_id, | ||
created_at: self.base.created_at, | ||
content: self.base.content, | ||
short_author: ShortAuthor { | ||
slug: self.author_slug, | ||
first_name: self.author_first_name, | ||
last_name: self.author_last_name, | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod author; | ||
mod comment; | ||
mod post; | ||
mod tag; | ||
|
||
pub use author::*; | ||
pub use comment::*; | ||
pub use post::*; | ||
pub use tag::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod rbatis_author_service; | ||
mod rbatis_comment_service; | ||
mod rbatis_post_service; | ||
|
||
pub use rbatis_author_service::create_rbatis_author_service; | ||
pub use rbatis_comment_service::create_rbatis_comment_service; | ||
pub use rbatis_post_service::create_rbatis_post_service; |
Oops, something went wrong.