Skip to content

Commit

Permalink
update page api
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Dec 23, 2023
1 parent 2ea1383 commit 9ee653f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/api/rest_api/handlers/page/mod.rs
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;
3 changes: 2 additions & 1 deletion src/api/rest_api/handlers/page/request/mod.rs
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 src/api/rest_api/handlers/page/request/update_page_request.rs
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)
}
}
6 changes: 3 additions & 3 deletions src/api/rest_api/handlers/page/store_page_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use crate::{
avored_state::AvoRedState, error::Result
};
use axum::{extract::State, Json};
use serde::{Deserialize, Serialize};
use serde::Serialize;
use crate::api::rest_api::handlers::page::request::store_page_request::StorePageRequest;


pub async fn store_page_api_handler(
state: State<Arc<AvoRedState>>,
Json(payload): Json<StorePageRequest>,
) -> Result<Json<CreatedPageResponse>> {
let validation_error_list = payload.validate_errors()?;
let _validation_error_list = payload.validate_errors()?;

println!("Validation error list: {:?}", validation_error_list);
// println!("Validation error list: {:?}", validation_error_list);

let creatable_page = CreatablePageModel {
name: payload.name,
Expand Down
47 changes: 47 additions & 0 deletions src/api/rest_api/handlers/page/update_page_api_handler.rs
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
}
6 changes: 4 additions & 2 deletions src/api/rest_api/rest_api_routes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use axum::{routing::get, Router, middleware};
use axum::routing::post;
use axum::routing::{post, put};
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE};
use axum::http::header::HeaderValue;
use crate::avored_state::AvoRedState;
Expand All @@ -12,7 +12,8 @@ use crate::api::rest_api::handlers::{
admin_user::admin_user_login_api_handler::admin_user_login_api_handler,
component_all_api_handler::component_all_api_handler,
health_check_api_handler::health_check_api_handler,
page::store_page_api_handler::store_page_api_handler
page::store_page_api_handler::store_page_api_handler,
page::update_page_api_handler::update_page_api_handler
};

pub fn rest_api_routes(state: Arc<AvoRedState>) -> Router {
Expand All @@ -35,6 +36,7 @@ pub fn rest_api_routes(state: Arc<AvoRedState>) -> Router {
Router::new()
.route("/api/page", get(page_table_api_handler))
.route("/api/page", post(store_page_api_handler))
.route("/api/page/:page_id", put(update_page_api_handler))
.route("/api/component-all", get(component_all_api_handler))
.route_layer(middleware::from_fn_with_state(
state.clone(),
Expand Down

0 comments on commit 9ee653f

Please sign in to comment.