-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ea1383
commit 9ee653f
Showing
6 changed files
with
99 additions
and
7 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod page_table_api_handler; | ||
pub mod request; | ||
pub mod store_page_api_handler; | ||
pub mod store_page_api_handler; | ||
pub mod update_page_api_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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod page_table_request; | ||
pub mod store_page_request; | ||
pub mod store_page_request; | ||
pub mod update_page_request; |
41 changes: 41 additions & 0 deletions
41
src/api/rest_api/handlers/page/request/update_page_request.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::error::Result; | ||
use serde::Deserialize; | ||
use validator::{Validate, ValidationErrors, ValidationErrorsKind}; | ||
|
||
#[derive(Deserialize, Debug, Clone, Validate, Default)] | ||
pub struct UpdatePageRequest { | ||
#[validate(length(min = 1, message = "The name is a required field."))] | ||
pub name: String, | ||
|
||
#[validate(length(min = 1, message = "The identifier is a required field."))] | ||
pub identifier: String, | ||
} | ||
|
||
impl UpdatePageRequest { | ||
pub fn validate_errors(&self) -> Result<ValidationErrors> { | ||
let validation_error_list = match self.validate() { | ||
Ok(_) => ValidationErrors::new(), | ||
Err(errors) => errors, | ||
}; | ||
|
||
for (_field_name, error) in validation_error_list.errors() { | ||
match &error { | ||
ValidationErrorsKind::Field(field_errors) => { | ||
for _field_error in field_errors { | ||
// IDea here is to add it into some kind of Error Response | ||
// so we can return JSON struct with status code | ||
|
||
// let message = match &field_error.message { | ||
// Some(message) => message, | ||
// None => continue, | ||
// }; | ||
} | ||
} | ||
ValidationErrorsKind::Struct(_) => continue, | ||
ValidationErrorsKind::List(_) => continue, | ||
} | ||
} | ||
|
||
Ok(validation_error_list) | ||
} | ||
} |
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,47 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::models::page_model::{PageModel, UpdatablePageModel}; | ||
use crate::{ | ||
api::rest_api::handlers::page::request::update_page_request::UpdatePageRequest, | ||
avored_state::AvoRedState, error::Result | ||
}; | ||
|
||
use axum::{extract::{Path as AxumPath, State}, Json, response::IntoResponse}; | ||
use serde::Serialize; | ||
|
||
pub async fn update_page_api_handler( | ||
AxumPath(page_id): AxumPath<String>, | ||
state: State<Arc<AvoRedState>>, | ||
Json(payload): Json<UpdatePageRequest>, | ||
) -> Result<impl IntoResponse> { | ||
println!("->> {:<12} - update_page_api_handler", "HANDLER"); | ||
|
||
let _validation_error_list = payload.validate_errors()?; | ||
|
||
// println!("Validation error list: {:?}", validation_error_list); | ||
|
||
let updateable_page_model = UpdatablePageModel { | ||
id: page_id, | ||
name: payload.name, | ||
identifier: payload.identifier, | ||
content: "updated test content".to_string(), // @todo remove this one | ||
logged_in_username: "[email protected]".to_string(), | ||
}; | ||
let updated_page_model = state | ||
.page_service | ||
.update_page(&state.db, updateable_page_model) | ||
.await?; | ||
let response = UpdatablePageResponse { | ||
status: true, | ||
page_model: updated_page_model | ||
}; | ||
|
||
Ok(Json(response)) | ||
} | ||
|
||
|
||
#[derive(Serialize, Debug)] | ||
pub struct UpdatablePageResponse { | ||
pub status: bool, | ||
pub page_model: PageModel | ||
} |
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