-
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.
Merge pull request #12 from tikitko/feat/createPostApi
feat: create new post route init
- Loading branch information
Showing
21 changed files
with
290 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use super::request_content::CreatePostRequestContent; | ||
use super::response_content_failure::CreatePostContentFailure; | ||
use super::response_content_failure::CreatePostContentFailure::*; | ||
use super::response_content_success::CreatePostContentSuccess; | ||
|
||
pub async fn http_handler( | ||
(CreatePostRequestContent { | ||
new_post_data, | ||
post_service, | ||
auth_author_future, | ||
},): (CreatePostRequestContent,), | ||
) -> Result<CreatePostContentSuccess, CreatePostContentFailure> { | ||
let author = auth_author_future.await.map_err(|e| Unauthorized { | ||
reason: e.to_string(), | ||
})?; | ||
|
||
let base_post = new_post_data | ||
.map_err(|e| ValidationError { | ||
reason: e.to_string(), | ||
})? | ||
.into(author.id); | ||
|
||
let inserted_id = post_service | ||
.create_post(&base_post) | ||
.await | ||
.map_err(|e| DatabaseError { | ||
reason: e.to_string(), | ||
})?; | ||
|
||
let created_post = post_service | ||
.post_by_id(&inserted_id) | ||
.await | ||
.map_err(|e| DatabaseError { | ||
reason: e.to_string(), | ||
})? | ||
.ok_or(InsertFailed)?; | ||
|
||
Ok(created_post.into()) | ||
} |
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; |
32 changes: 32 additions & 0 deletions
32
blog-server-api/src/endpoints/create_post/request_content.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,32 @@ | ||
use crate::{entities::CreatePost, extensions::Resolve, utils::auth}; | ||
use blog_server_services::traits::{ | ||
author_service::{Author, AuthorService}, | ||
post_service::PostService, | ||
}; | ||
use screw_api::request::{ApiRequestContent, ApiRequestOriginContent}; | ||
use screw_components::{dyn_fn::DFuture, dyn_result::DResult}; | ||
use std::sync::Arc; | ||
|
||
pub struct CreatePostRequestContent { | ||
pub(super) new_post_data: DResult<CreatePost>, | ||
pub(super) post_service: Arc<Box<dyn PostService>>, | ||
pub(super) auth_author_future: DFuture<Result<Author, auth::Error>>, | ||
} | ||
|
||
impl<Extensions> ApiRequestContent<Extensions> for CreatePostRequestContent | ||
where | ||
Extensions: Resolve<Arc<Box<dyn PostService>>> + Resolve<Arc<Box<dyn AuthorService>>>, | ||
{ | ||
type Data = CreatePost; | ||
|
||
fn create(origin_content: ApiRequestOriginContent<Self::Data, Extensions>) -> Self { | ||
Self { | ||
new_post_data: origin_content.data_result, | ||
post_service: origin_content.extensions.resolve(), | ||
auth_author_future: Box::pin(auth::author( | ||
origin_content.http_parts, | ||
origin_content.extensions.resolve(), | ||
)), | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
blog-server-api/src/endpoints/create_post/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,64 @@ | ||
use hyper::StatusCode; | ||
use screw_api::response::{ApiResponseContentBase, ApiResponseContentFailure}; | ||
|
||
pub enum CreatePostContentFailure { | ||
DatabaseError { reason: String }, | ||
ValidationError { reason: String }, | ||
AlreadyExists, | ||
InsertFailed, | ||
Unauthorized { reason: String }, | ||
} | ||
|
||
impl ApiResponseContentBase for CreatePostContentFailure { | ||
fn status_code(&self) -> &'static StatusCode { | ||
match self { | ||
CreatePostContentFailure::DatabaseError { reason: _ } => { | ||
&StatusCode::INTERNAL_SERVER_ERROR | ||
} | ||
CreatePostContentFailure::AlreadyExists => &StatusCode::BAD_REQUEST, | ||
CreatePostContentFailure::ValidationError { reason: _ } => &StatusCode::BAD_REQUEST, | ||
CreatePostContentFailure::Unauthorized { reason: _ } => &StatusCode::UNAUTHORIZED, | ||
CreatePostContentFailure::InsertFailed => &StatusCode::INTERNAL_SERVER_ERROR, | ||
} | ||
} | ||
} | ||
|
||
impl ApiResponseContentFailure for CreatePostContentFailure { | ||
fn identifier(&self) -> &'static str { | ||
match self { | ||
CreatePostContentFailure::DatabaseError { reason: _ } => "CREATE_POST_DATABASE_ERROR", | ||
CreatePostContentFailure::ValidationError { reason: _ } => { | ||
"CREATE_POST_VALIDATION_ERROR" | ||
} | ||
CreatePostContentFailure::AlreadyExists => "CREATE_POST_ALREASY_EXISTS", | ||
CreatePostContentFailure::InsertFailed => "CREATE_POST_COULD_NOT_FIND_CREATED_POST", | ||
CreatePostContentFailure::Unauthorized { reason: _ } => "CREATE_POST_UNAUTHORIZED", | ||
} | ||
} | ||
|
||
fn reason(&self) -> Option<String> { | ||
Some(match self { | ||
CreatePostContentFailure::DatabaseError { reason } => { | ||
if cfg!(debug_assertions) { | ||
format!("database error: {}", reason) | ||
} else { | ||
"internal database error".to_string() | ||
} | ||
} | ||
CreatePostContentFailure::Unauthorized { reason } => { | ||
if cfg!(debug_assertions) { | ||
format!("unauthorized error: {}", reason) | ||
} else { | ||
"unauthorized error".to_string() | ||
} | ||
} | ||
CreatePostContentFailure::ValidationError { reason } => { | ||
format!("validation error: {}", reason) | ||
} | ||
CreatePostContentFailure::AlreadyExists => { | ||
String::from("post with specified ID already exists") | ||
} | ||
CreatePostContentFailure::InsertFailed => String::from("error while creating new post"), | ||
}) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
blog-server-api/src/endpoints/create_post/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,41 @@ | ||
use crate::entities::Post; | ||
use blog_server_services::traits::post_service::Post as ServicePost; | ||
use hyper::StatusCode; | ||
use screw_api::response::{ApiResponseContentBase, ApiResponseContentSuccess}; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize, Debug, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct CreatePostContentSuccess { | ||
created_post: Post, | ||
} | ||
|
||
impl Into<CreatePostContentSuccess> for ServicePost { | ||
fn into(self) -> CreatePostContentSuccess { | ||
CreatePostContentSuccess { | ||
created_post: (self.into()), | ||
} | ||
} | ||
} | ||
|
||
impl ApiResponseContentBase for CreatePostContentSuccess { | ||
fn status_code(&self) -> &'static StatusCode { | ||
&StatusCode::OK | ||
} | ||
} | ||
|
||
impl ApiResponseContentSuccess for CreatePostContentSuccess { | ||
type Data = Self; | ||
|
||
fn identifier(&self) -> &'static str { | ||
"POST_CREATED" | ||
} | ||
|
||
fn description(&self) -> Option<String> { | ||
Some(String::from("post record created")) | ||
} | ||
|
||
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
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,35 @@ | ||
use blog_server_services::traits::post_service::BasePost; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::utils::time_utils; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct CreatePost { | ||
pub title: String, | ||
pub slug: String, | ||
pub summary: String, | ||
pub published: u8, | ||
pub content: Option<String>, | ||
pub tags: Vec<ShortTag>, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ShortTag { | ||
pub title: String, | ||
} | ||
|
||
impl CreatePost { | ||
pub fn into(self, author_id: u64) -> BasePost { | ||
BasePost { | ||
author_id, | ||
created_at: time_utils::now_as_secs(), | ||
title: self.title, | ||
slug: self.slug, | ||
summary: self.summary, | ||
published: self.published, | ||
content: self.content, | ||
} | ||
} | ||
} |
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,9 +1,11 @@ | ||
mod author; | ||
mod comment; | ||
mod create_post; | ||
mod post; | ||
mod tag; | ||
|
||
pub use author::*; | ||
pub use comment::*; | ||
pub use create_post::*; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
use serde::Serialize; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize)] | ||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Tag { | ||
pub id: u64, | ||
pub title: String, | ||
pub slug: String, | ||
} |
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,3 +1,4 @@ | ||
pub mod auth; | ||
pub mod jwt; | ||
pub mod password; | ||
pub mod time_utils; |
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,8 @@ | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
pub fn now_as_secs() -> u64 { | ||
SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap_or_default() | ||
.as_secs() | ||
} |
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
Oops, something went wrong.