-
-
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
e876c57
commit eeaba8d
Showing
10 changed files
with
287 additions
and
50 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,2 +1,4 @@ | ||
pub mod collection_table_api_handler; | ||
pub mod store_collection_api_handler; | ||
pub mod update_collection_api_handler; | ||
pub mod request; |
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 +1,3 @@ | ||
pub mod collection_table_request; | ||
pub mod store_collection_request; | ||
pub mod update_collection_request; |
35 changes: 35 additions & 0 deletions
35
src/api/handlers/collection/request/store_collection_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,35 @@ | ||
use rust_i18n::t; | ||
use serde::Deserialize; | ||
use crate::models::validation_error::{ErrorMessage, Validate}; | ||
|
||
#[derive(Deserialize, Debug, Clone, Default)] | ||
pub struct StoreCollectionRequest { | ||
pub name: String, | ||
pub identifier: String, | ||
} | ||
|
||
impl StoreCollectionRequest { | ||
pub fn validate(&self) -> crate::error::Result<Vec<ErrorMessage>> { | ||
let mut errors: Vec<ErrorMessage> = vec![]; | ||
|
||
if !self.name.required()? { | ||
let error_message = ErrorMessage { | ||
key: String::from("name"), | ||
message: t!("validation_required", attribute = t!("name")).to_string(), | ||
}; | ||
|
||
errors.push(error_message); | ||
} | ||
|
||
if !self.identifier.required()? { | ||
let error_message = ErrorMessage { | ||
key: String::from("identifier"), | ||
message: format!("Identifier is a required field {}", t!("identifier")), | ||
}; | ||
|
||
errors.push(error_message); | ||
} | ||
|
||
Ok(errors) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/api/handlers/collection/request/update_collection_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,26 @@ | ||
use rust_i18n::t; | ||
use serde::Deserialize; | ||
|
||
use crate::models::validation_error::{ErrorMessage, Validate}; | ||
|
||
#[derive(Deserialize, Debug, Clone, Default)] | ||
pub struct UpdateCollectionRequest { | ||
pub name: String, | ||
} | ||
|
||
impl UpdateCollectionRequest { | ||
pub fn validate(&self) -> crate::error::Result<Vec<ErrorMessage>> { | ||
let mut errors: Vec<ErrorMessage> = vec![]; | ||
|
||
if !self.name.required()? { | ||
let error_message = ErrorMessage { | ||
key: String::from("name"), | ||
message: t!("validation_required", attribute = t!("name")).to_string(), | ||
}; | ||
|
||
errors.push(error_message); | ||
} | ||
|
||
Ok(errors) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/api/handlers/collection/store_collection_api_handler.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,54 @@ | ||
use std::sync::Arc; | ||
use axum::{Extension, Json}; | ||
use axum::extract::State; | ||
use crate::api::handlers::collection::request::store_collection_request::StoreCollectionRequest; | ||
use crate::avored_state::AvoRedState; | ||
use crate::error::{Error, Result}; | ||
use crate::models::collection_model::{CollectionModel, CreatableCollection}; | ||
use crate::models::token_claim_model::LoggedInUser; | ||
use crate::models::validation_error::ErrorResponse; | ||
use crate::responses::ApiResponse; | ||
|
||
pub async fn store_collection_api_handler ( | ||
state: State<Arc<AvoRedState>>, | ||
Extension(logged_in_user): Extension<LoggedInUser>, | ||
Json(payload): Json<StoreCollectionRequest>, | ||
) -> Result<Json<ApiResponse<CollectionModel>>> { | ||
println!("->> {:<12} - store_collection_api_handler", "HANDLER"); | ||
|
||
let has_permission_bool = state | ||
.admin_user_service | ||
.has_permission(logged_in_user.clone(), String::from("collection_create")) | ||
.await?; | ||
if !has_permission_bool { | ||
return Err(Error::Forbidden); | ||
} | ||
|
||
let error_messages = payload.validate()?; | ||
|
||
if !error_messages.is_empty() { | ||
let error_response = ErrorResponse { | ||
status: false, | ||
errors: error_messages, | ||
}; | ||
|
||
return Err(Error::BadRequest(error_response)); | ||
} | ||
|
||
let creatable_model = CreatableCollection { | ||
name: payload.name, | ||
identifier: payload.identifier, | ||
logged_in_username: logged_in_user.email, | ||
}; | ||
|
||
let created_model = state | ||
.collection_service | ||
.create_collection(&state.db, creatable_model) | ||
.await?; | ||
let response = ApiResponse { | ||
status: true, | ||
data: created_model, | ||
}; | ||
|
||
Ok(Json(response)) | ||
} |
55 changes: 55 additions & 0 deletions
55
src/api/handlers/collection/update_collection_api_handler.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,55 @@ | ||
use std::sync::Arc; | ||
use axum::{Extension, Json}; | ||
use axum::extract::{Path, State}; | ||
use crate::api::handlers::collection::request::update_collection_request::UpdateCollectionRequest; | ||
use crate::avored_state::AvoRedState; | ||
use crate::error::{Error, Result}; | ||
use crate::models::collection_model::{CollectionModel, UpdatableCollection}; | ||
use crate::models::token_claim_model::LoggedInUser; | ||
use crate::models::validation_error::ErrorResponse; | ||
use crate::responses::ApiResponse; | ||
|
||
pub async fn update_collection_api_handler ( | ||
state: State<Arc<AvoRedState>>, | ||
Path(collection_id): Path<String>, | ||
Extension(logged_in_user): Extension<LoggedInUser>, | ||
Json(payload): Json<UpdateCollectionRequest>, | ||
) -> Result<Json<ApiResponse<CollectionModel>>> { | ||
println!("->> {:<12} - update_collection_api_handler", "HANDLER"); | ||
|
||
let has_permission_bool = state | ||
.admin_user_service | ||
.has_permission(logged_in_user.clone(), String::from("collection_update")) | ||
.await?; | ||
if !has_permission_bool { | ||
return Err(Error::Forbidden); | ||
} | ||
|
||
let error_messages = payload.validate()?; | ||
|
||
if !error_messages.is_empty() { | ||
let error_response = ErrorResponse { | ||
status: false, | ||
errors: error_messages, | ||
}; | ||
|
||
return Err(Error::BadRequest(error_response)); | ||
} | ||
|
||
let creatable_model = UpdatableCollection { | ||
name: payload.name, | ||
id: collection_id, | ||
logged_in_username: logged_in_user.email, | ||
}; | ||
|
||
let updated_model = state | ||
.collection_service | ||
.update_collection(&state.db, creatable_model) | ||
.await?; | ||
let response = ApiResponse { | ||
status: true, | ||
data: updated_model, | ||
}; | ||
|
||
Ok(Json(response)) | ||
} |
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
Oops, something went wrong.