From 9c3fd1be5df242c139f29eb7cc7117b37ac43c0f Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 10 Jan 2025 21:54:40 +0530 Subject: [PATCH] feat: bootstrap ordering web adapter --- src/ordering/adapters/input/mod.rs | 1 + src/ordering/adapters/input/web/errors.rs | 61 +++++++++++++++++++++++ src/ordering/adapters/input/web/mod.rs | 30 +++++++++++ src/ordering/adapters/input/web/routes.rs | 20 ++++++++ src/ordering/adapters/mod.rs | 2 +- src/ordering/adapters/types.rs | 2 +- 6 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/ordering/adapters/input/web/errors.rs create mode 100644 src/ordering/adapters/input/web/mod.rs create mode 100644 src/ordering/adapters/input/web/routes.rs diff --git a/src/ordering/adapters/input/mod.rs b/src/ordering/adapters/input/mod.rs index 56f60de..810dc4e 100644 --- a/src/ordering/adapters/input/mod.rs +++ b/src/ordering/adapters/input/mod.rs @@ -1,3 +1,4 @@ // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later +pub mod web; diff --git a/src/ordering/adapters/input/web/errors.rs b/src/ordering/adapters/input/web/errors.rs new file mode 100644 index 0000000..86b2a64 --- /dev/null +++ b/src/ordering/adapters/input/web/errors.rs @@ -0,0 +1,61 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use actix_web::http::StatusCode; +use actix_web::{HttpResponse, ResponseError}; +use derive_more::Display; +use serde::{Deserialize, Serialize}; + +use crate::inventory::application::services::errors::*; + +#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)] +struct ErrorResponse { + error: String, +} + +impl From for ErrorResponse { + fn from(value: WebError) -> Self { + ErrorResponse { + error: serde_json::to_string(&value).unwrap_or_else(|_| { + log::error!("Unable to serialize error"); + "Unable to serialize error".into() + }), + } + } +} + +#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +pub enum WebError { + InternalError, + BadRequest, + +} + +impl From for WebError { + fn from(v: InventoryError) -> Self { + match v { + InventoryError::InternalError => Self::InternalError, + } + } +} + +impl ResponseError for WebError { + fn status_code(&self) -> StatusCode { + match self { + Self::InternalError => StatusCode::INTERNAL_SERVER_ERROR, + Self::BadRequest => StatusCode::BAD_REQUEST, + + } + } + + fn error_response(&self) -> actix_web::HttpResponse { + let e: ErrorResponse = self.clone().into(); + match self { + Self::InternalError => HttpResponse::InternalServerError().json(e), + Self::BadRequest => HttpResponse::BadRequest().json(e), + } + } +} + +pub type WebJsonRepsonse = Result; diff --git a/src/ordering/adapters/input/web/mod.rs b/src/ordering/adapters/input/web/mod.rs new file mode 100644 index 0000000..fc34430 --- /dev/null +++ b/src/ordering/adapters/input/web/mod.rs @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::sync::Arc; + +use actix_web::web; + +use crate::inventory::adapters::types; + +mod employee; +mod errors; +mod owner; +mod routes; + +pub use errors::WebJsonRepsonse; + +pub use routes::RoutesRepository; + +pub fn load_ctx() -> impl FnOnce(&mut web::ServiceConfig) { + let routes = types::WebInventoryRoutesRepository::new(Arc::new(RoutesRepository::default())); + + let f = move |cfg: &mut web::ServiceConfig| { + cfg.app_data(routes); + cfg.configure(owner::services); + cfg.configure(employee::services); + }; + + Box::new(f) +} diff --git a/src/ordering/adapters/input/web/routes.rs b/src/ordering/adapters/input/web/routes.rs new file mode 100644 index 0000000..566dfb5 --- /dev/null +++ b/src/ordering/adapters/input/web/routes.rs @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use url::Url; +use uuid::Uuid; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub struct RoutesRepository { +} + +impl Default for RoutesRepository { + fn default() -> Self { + Self { + } + } +} + +impl RoutesRepository { +} diff --git a/src/ordering/adapters/mod.rs b/src/ordering/adapters/mod.rs index c6bf8cf..d301400 100644 --- a/src/ordering/adapters/mod.rs +++ b/src/ordering/adapters/mod.rs @@ -68,7 +68,7 @@ pub fn load_adapters(pool: PgPool, settings: Settings) -> impl FnOnce(&mut web:: )); let f = move |cfg: &mut web::ServiceConfig| { - //cfg.configure(input::web::load_ctx()); + cfg.configure(input::web::load_ctx()); cfg.app_data(Data::new(category_cqrs_query.clone())); cfg.app_data(Data::new(product_cqrs_query.clone())); cfg.app_data(Data::new(customization_cqrs_query.clone())); diff --git a/src/ordering/adapters/types.rs b/src/ordering/adapters/types.rs index 5b1c66b..4e0c7c0 100644 --- a/src/ordering/adapters/types.rs +++ b/src/ordering/adapters/types.rs @@ -15,7 +15,7 @@ use postgres_es::PostgresCqrs; use crate::ordering::{ adapters::{ - // input::web::RoutesRepository, + input::web::RoutesRepository, output::db::{ category_view::CategoryView, customization_view::CustomizationView, kot_view::KotView, line_item_view::LineItemView, order_view::OrderView, product_view::ProductView,