Skip to content

Commit

Permalink
fetch collection api
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Dec 28, 2024
1 parent 37f700e commit 2161417
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
37 changes: 37 additions & 0 deletions src/api/handlers/collection/fetch_collection_api_handler.rs
Original file line number Diff line number Diff line change
@@ -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<Arc<AvoRedState>>,
Path(collection_id): Path<String>,
Extension(logged_in_user): Extension<LoggedInUser>,
) -> Result<Json<ApiResponse<CollectionModel>>> {
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))
}
1 change: 1 addition & 0 deletions src/api/handlers/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 7 additions & 3 deletions src/api/rest_api_routes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<AvoRedState>) -> Router {
Router::new()
Expand Down Expand Up @@ -148,7 +149,10 @@ fn admin_api_routes(state: Arc<AvoRedState>) -> 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))
Expand Down
50 changes: 25 additions & 25 deletions src/repositories/collection_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CollectionCollection> {
// let sql = "SELECT * FROM type::thing($table, $id);";
// let vars: BTreeMap<String, Value> = [
// ("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<CollectionCollection> = result_object?.try_into();
//
// model_model
// }

pub async fn find_by_id(
&self,
datastore: &Datastore,
database_session: &Session,
model_id: String,
) -> Result<CollectionModel> {
let sql = "SELECT * FROM type::thing($table, $id);";
let vars: BTreeMap<String, Value> = [
("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<CollectionModel> = result_object?.try_into();

model_model
}
//
//
// pub async fn update_model_identifier(
Expand Down
19 changes: 10 additions & 9 deletions src/services/collection_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct CollectionService {
collection_repository: CollectionRepository,
}


impl CollectionService {
pub fn new(collection_repository: CollectionRepository) -> Result<Self> {
Ok(CollectionService {
Expand Down Expand Up @@ -89,15 +90,15 @@ impl CollectionService {
})
}

// pub async fn find_by_id(
// &self,
// (datastore, database_session): &DB,
// id: String,
// ) -> Result<CollectionCollection> {
// 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<CollectionModel> {
self.collection_repository
.find_by_id(datastore, database_session, id)
.await
}

// pub async fn update_collection_identifier(
// &self,
Expand Down

0 comments on commit 2161417

Please sign in to comment.