Skip to content

Commit

Permalink
Merge pull request #234 from avored/233-move-the-page-identifier-vali…
Browse files Browse the repository at this point in the history
…dation-logic-to-impl-validation

move the logic impl validation
  • Loading branch information
indpurvesh authored Aug 29, 2024
2 parents 19ee4e6 + 9d6d9c7 commit 938f2f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ jobs:
override: true
- name: Rust Check
run: |
cp .env.example .env
cargo check
18 changes: 16 additions & 2 deletions src/api/handlers/page/request/store_page_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rust_i18n::t;
use serde::Deserialize;

use crate::avored_state::AvoRedState;
use crate::models::validation_error::{ErrorMessage, Validate};

#[derive(Deserialize, Debug, Clone, Default)]
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct CreatablePageComponentElementDataOptionRequest {


impl StorePageRequest {
pub fn validate(&self) -> crate::error::Result<Vec<ErrorMessage>> {
pub async fn validate(&self, state: &AvoRedState) -> crate::error::Result<Vec<ErrorMessage>> {
let mut errors: Vec<ErrorMessage> = vec![];

if !self.name.required()? {
Expand All @@ -56,6 +56,20 @@ impl StorePageRequest {
errors.push(error_message);
}

let page_count = state
.page_service
.count_of_identifier(&state.db, self.identifier.clone())
.await?;

if page_count.total > 0 {
let error_message = ErrorMessage {
key: String::from("identifier"),
message: t!("validation_count", attribute = t!("identifier")).to_string()
};

errors.push(error_message);
}

Ok(errors)
}
}
36 changes: 8 additions & 28 deletions src/api/handlers/page/store_page_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use crate::error::Error;
use crate::models::page_model::{CreatableComponentContentModel, CreatableComponentElementContentModel, CreatablePageModel, PageModel, CreatablePageComponentElementDataModel};
use crate::models::validation_error::{ErrorMessage, ErrorResponse};
use crate::models::validation_error::ErrorResponse;
use crate::{
avored_state::AvoRedState, error::Result
};
Expand All @@ -16,7 +16,7 @@ pub async fn store_page_api_handler(
state: State<Arc<AvoRedState>>,
Json(payload): Json<StorePageRequest>,
) -> Result<Json<CreatedPageResponse>> {
let error_messages = payload.validate()?;
let error_messages = payload.validate(&state).await?;

let has_permission_bool = state
.admin_user_service
Expand Down Expand Up @@ -79,34 +79,14 @@ pub async fn store_page_api_handler(
let created_page_model = state
.page_service
.create_page(&state.db, creatable_page, logged_in_user)
.await;
.await?;

match created_page_model {
Ok(val) => {
let response = CreatedPageResponse {
status: true,
page_model: val
};
let response = CreatedPageResponse {
status: true,
page_model: created_page_model
};

Ok(Json(response))
},
Err(e) => match e {
Error::Generic(ref msg) if msg == "Duplicate error" => {
let mut errors: Vec<ErrorMessage> = vec![];
let error_message = ErrorMessage {
key: String::from("identifier"),
message: "identifier should be unique".to_string()
};
errors.push(error_message);
let error_response = ErrorResponse {
status: false,
errors
};
Err(Error::BadRequestError(error_response))
},
_ => Err(Error::Generic("Internal Server Error".to_string())),
}
}
Ok(Json(response))
}

#[derive(Serialize, Debug)]
Expand Down

0 comments on commit 938f2f5

Please sign in to comment.