diff --git a/src/api/handlers/collection/fetch_collection_api_handler.rs b/src/api/handlers/collection/fetch_collection_api_handler.rs new file mode 100644 index 0000000..2cf20e2 --- /dev/null +++ b/src/api/handlers/collection/fetch_collection_api_handler.rs @@ -0,0 +1,37 @@ +use crate::api::handlers::model::fetch_model_api_handler::FetchModelResponse; +use crate::avored_state::AvoRedState; +use crate::error::{Error, Result}; +use crate::models::collection_model::CollectionModel; +use crate::models::token_claim_model::LoggedInUser; +use crate::responses::ApiResponse; +use axum::extract::{Path, State}; +use axum::{Extension, Json}; +use std::sync::Arc; + +pub async fn fetch_collection_api_handler( + state: State>, + Path(collection_id): Path, + Extension(logged_in_user): Extension, +) -> Result>> { + println!("->> {:<12} - fetch_model_api_handler", "HANDLER"); + + let has_permission_bool = state + .admin_user_service + .has_permission(logged_in_user, String::from("get_model")) + .await?; + if !has_permission_bool { + return Err(Error::Forbidden); + } + + let model_model = state + .collection_service + .find_by_id(&state.db, collection_id) + .await?; + + let response = ApiResponse { + status: true, + data: model_model, + }; + + Ok(Json(response)) +} diff --git a/src/api/handlers/collection/mod.rs b/src/api/handlers/collection/mod.rs index e16d00c..ee04a1f 100644 --- a/src/api/handlers/collection/mod.rs +++ b/src/api/handlers/collection/mod.rs @@ -1,4 +1,5 @@ pub mod collection_table_api_handler; pub mod store_collection_api_handler; pub mod update_collection_api_handler; +pub mod fetch_collection_api_handler; pub mod request; diff --git a/src/api/rest_api_routes.rs b/src/api/rest_api_routes.rs index 4408938..09278ac 100644 --- a/src/api/rest_api_routes.rs +++ b/src/api/rest_api_routes.rs @@ -1,6 +1,8 @@ use crate::api::handlers::cms::all_pages_cms_api_handler::all_pages_cms_api_handler; use crate::api::handlers::cms::sent_contact_us_email_handler::sent_contact_us_email_handler; use crate::api::handlers::collection::collection_table_api_handler::collection_table_api_handler; +use crate::api::handlers::collection::store_collection_api_handler::store_collection_api_handler; +use crate::api::handlers::collection::update_collection_api_handler::update_collection_api_handler; use crate::api::handlers::graphql::graphql_api_handler::graphql_api_handler; use crate::api::handlers::misc::delete_demo_data_api_handler::delete_demo_data_api_handler; use crate::api::handlers::misc::install_demo_data_api_handler::install_demo_data_api_handler; @@ -63,8 +65,7 @@ use axum::{middleware, routing::get, Extension, Router}; use juniper::{EmptyMutation, EmptySubscription}; use std::sync::Arc; use tower_http::cors::CorsLayer; -use crate::api::handlers::collection::store_collection_api_handler::store_collection_api_handler; -use crate::api::handlers::collection::update_collection_api_handler::update_collection_api_handler; +use crate::api::handlers::collection::fetch_collection_api_handler::fetch_collection_api_handler; pub fn rest_api_routes(state: Arc) -> Router { Router::new() @@ -148,7 +149,10 @@ fn admin_api_routes(state: Arc) -> Router { ) .route("/api/collection", get(collection_table_api_handler)) .route("/api/collection", post(store_collection_api_handler)) - .route("/api/collection/:collection_id", put(update_collection_api_handler)) + .route( + "/api/collection/:collection_id", + get(fetch_collection_api_handler), + ) .route("/api/model", get(model_table_api_handler)) .route("/api/model", post(store_model_api_handler)) .route("/api/model/:model_id", put(update_model_api_handler)) diff --git a/src/repositories/collection_repository.rs b/src/repositories/collection_repository.rs index 4053eb5..0206ced 100644 --- a/src/repositories/collection_repository.rs +++ b/src/repositories/collection_repository.rs @@ -71,31 +71,31 @@ impl CollectionRepository { } Ok(paginate_models) } - // - // pub async fn find_by_id( - // &self, - // datastore: &Datastore, - // database_session: &Session, - // model_id: String, - // ) -> Result { - // let sql = "SELECT * FROM type::thing($table, $id);"; - // let vars: BTreeMap = [ - // ("id".into(), model_id.into()), - // ("table".into(), "models".into()), - // ] - // .into(); - // - // let responses = datastore.execute(sql, database_session, Some(vars)).await?; - // - // let result_object_option = into_iter_objects(responses)?.next(); - // let result_object = match result_object_option { - // Some(object) => object, - // None => Err(Error::Generic("no record found".to_string())), - // }; - // let model_model: Result = result_object?.try_into(); - // - // model_model - // } + + pub async fn find_by_id( + &self, + datastore: &Datastore, + database_session: &Session, + model_id: String, + ) -> Result { + let sql = "SELECT * FROM type::thing($table, $id);"; + let vars: BTreeMap = [ + ("id".into(), model_id.into()), + ("table".into(), "collections".into()), + ] + .into(); + + let responses = datastore.execute(sql, database_session, Some(vars)).await?; + + let result_object_option = into_iter_objects(responses)?.next(); + let result_object = match result_object_option { + Some(object) => object, + None => Err(Error::Generic("no record found".to_string())), + }; + let model_model: Result = result_object?.try_into(); + + model_model + } // // // pub async fn update_model_identifier( diff --git a/src/services/collection_service.rs b/src/services/collection_service.rs index a4f49cc..0f3d80b 100644 --- a/src/services/collection_service.rs +++ b/src/services/collection_service.rs @@ -11,6 +11,7 @@ pub struct CollectionService { collection_repository: CollectionRepository, } + impl CollectionService { pub fn new(collection_repository: CollectionRepository) -> Result { Ok(CollectionService { @@ -89,15 +90,15 @@ impl CollectionService { }) } - // pub async fn find_by_id( - // &self, - // (datastore, database_session): &DB, - // id: String, - // ) -> Result { - // self.collection_repository - // .find_by_id(datastore, database_session, id) - // .await - // } + pub async fn find_by_id( + &self, + (datastore, database_session): &DB, + id: String, + ) -> Result { + self.collection_repository + .find_by_id(datastore, database_session, id) + .await + } // pub async fn update_collection_identifier( // &self,