feat: bootstrap ordering web adapter

This commit is contained in:
Aravinth Manivannan 2025-01-10 21:54:40 +05:30
parent 8ad620476d
commit 354188d8b0
Signed by: realaravinth
GPG key ID: F8F50389936984FF
6 changed files with 161 additions and 3 deletions

View file

@ -1,3 +1,4 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod web;

View file

@ -0,0 +1,114 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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::ordering::application::services::errors::*;
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
struct ErrorResponse {
error: String,
}
impl From<WebError> 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,
LineItemIDNotFound,
OrderIDNotFound,
KotIDNotFound,
DuplicateStoreName,
StoreIDNotFound,
CategoryIDNotFound,
DuplicateCategoryName,
DuplicateProductName,
ProductIDNotFound,
DuplicateCustomizationName,
CustomizationIDNotFound,
}
impl From<OrderingError> for WebError {
fn from(v: OrderingError) -> Self {
match v {
OrderingError::InternalError => Self::InternalError,
OrderingError::LineItemIDNotFound => Self::LineItemIDNotFound,
OrderingError::OrderIDNotFound => Self::OrderIDNotFound,
OrderingError::KotIDNotFound => Self::KotIDNotFound,
OrderingError::DuplicateStoreName => Self::DuplicateStoreName,
OrderingError::StoreIDNotFound => Self::StoreIDNotFound,
OrderingError::CategoryIDNotFound => Self::CategoryIDNotFound,
OrderingError::DuplicateCategoryName => Self::DuplicateCategoryName,
OrderingError::DuplicateProductName => Self::DuplicateProductName,
OrderingError::ProductIDNotFound => Self::ProductIDNotFound,
OrderingError::DuplicateCustomizationName => Self::DuplicateCustomizationName,
OrderingError::CustomizationIDNotFound => Self::CustomizationIDNotFound,
OrderingError::DuplicateKotID
| OrderingError::DuplicateKotID
| OrderingError::DuplicateOrderID
| OrderingError::DuplicateStoreID
| OrderingError::DuplicateCustomizationID
| OrderingError::DuplicateProductID
| OrderingError::DuplicateCategoryID
| OrderingError::DuplicateLineItemID => Self::InternalError,
}
}
}
impl ResponseError for WebError {
fn status_code(&self) -> StatusCode {
match self {
Self::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
Self::BadRequest => StatusCode::BAD_REQUEST,
Self::LineItemIDNotFound => StatusCode::NOT_FOUND,
Self::OrderIDNotFound => StatusCode::NOT_FOUND,
Self::KotIDNotFound => StatusCode::NOT_FOUND,
Self::DuplicateStoreName => StatusCode::BAD_REQUEST,
Self::StoreIDNotFound => StatusCode::NOT_FOUND,
Self::CategoryIDNotFound => StatusCode::NOT_FOUND,
Self::DuplicateCategoryName => StatusCode::BAD_REQUEST,
Self::DuplicateProductName => StatusCode::BAD_REQUEST,
Self::ProductIDNotFound => StatusCode::NOT_FOUND,
Self::DuplicateCustomizationName => StatusCode::BAD_REQUEST,
Self::CustomizationIDNotFound => StatusCode::NOT_FOUND,
}
}
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),
Self::LineItemIDNotFound => HttpResponse::NotFound().json(e),
Self::OrderIDNotFound => HttpResponse::NotFound().json(e),
Self::KotIDNotFound => HttpResponse::NotFound().json(e),
Self::DuplicateStoreName => HttpResponse::BadRequest().json(e),
Self::StoreIDNotFound => HttpResponse::NotFound().json(e),
Self::CategoryIDNotFound => HttpResponse::NotFound().json(e),
Self::DuplicateCategoryName => HttpResponse::BadRequest().json(e),
Self::DuplicateProductName => HttpResponse::BadRequest().json(e),
Self::ProductIDNotFound => HttpResponse::NotFound().json(e),
Self::DuplicateCustomizationName => HttpResponse::BadRequest().json(e),
Self::CustomizationIDNotFound => HttpResponse::NotFound().json(e),
}
}
}
pub type WebJsonRepsonse<V> = Result<V, WebError>;

View file

@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::sync::Arc;
use actix_web::web;
use crate::ordering::adapters::types;
mod errors;
mod routes;
pub use errors::WebJsonRepsonse;
pub use routes::RoutesRepository;
pub fn load_ctx() -> impl FnOnce(&mut web::ServiceConfig) {
let routes = types::WebOrderingRoutesRepository::new(Arc::new(RoutesRepository::default()));
let f = move |cfg: &mut web::ServiceConfig| {
cfg.app_data(routes);
};
Box::new(f)
}

View file

@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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 {}

View file

@ -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()));

View file

@ -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,
@ -30,7 +30,7 @@ use crate::ordering::{
},
};
//pub type WebOrderingRoutesRepository = Data<Arc<RoutesRepository>>;
pub type WebOrderingRoutesRepository = Data<Arc<RoutesRepository>>;
pub type OrderingOrderCqrsExec = Arc<PostgresCqrs<Order>>;
pub type OrderingOrderCqrsView = Arc<dyn ViewRepository<OrderView, Order>>;