Change Output to result

This commit is contained in:
eraden 2022-11-08 07:49:06 +01:00
parent a3dd9b85d8
commit b70a464afe
28 changed files with 517 additions and 288 deletions

View File

@ -13,11 +13,7 @@ pub async fn me(account_id: model::AccountId, db: Database) -> me::Output {
Ok(t) => t,
Err(e) => {
tracing::error!("{}", e);
return me::Output {
account: None,
addresses: None,
error: Some(Error::Account),
};
return Err(Error::Account);
}
};
@ -28,10 +24,7 @@ pub async fn me(account_id: model::AccountId, db: Database) -> me::Output {
tracing::error!("{}", e);
t.rollback().await.ok();
return me::Output {
error: Some(Error::Account),
..Default::default()
};
return Err(Error::Account);
}
};
let res = AccountAddresses { account_id }.run(&mut t).await;
@ -41,19 +34,12 @@ pub async fn me(account_id: model::AccountId, db: Database) -> me::Output {
tracing::error!("{}", e);
t.rollback().await.ok();
return me::Output {
error: Some(Error::Addresses),
..Default::default()
};
return Err(Error::Addresses);
}
};
t.commit().await.ok();
me::Output {
account: Some(account),
addresses: Some(addresses),
..Default::default()
}
Ok(me::Details { account, addresses })
}
pub async fn create_account(

View File

@ -49,15 +49,9 @@ impl Accounts for AccountsServer {
match res {
Ok(account) => {
self.mqtt_client.emit_account_created(&account).await;
register::Output {
account: Some(account),
error: None,
}
Ok(register::Details { account })
}
Err(_e) => register::Output {
account: None,
error: Some(Error::Account),
},
Err(_e) => Err(Error::Account),
}
}
}

View File

@ -161,6 +161,7 @@ async fn create_account(opts: CreateAccountOpts) -> Result<()> {
},
)
.await
.unwrap()
.unwrap();
Ok(())
}
@ -214,7 +215,7 @@ async fn reindex(opts: ReIndexOpts) -> Result<()> {
tracing::info!("{:?}", products);
for product in products {
if let Ok(res) = search
if let Ok(Err(e)) = search
.create_index(
tarpc::context::current(),
channels::search::create_index::Input::new(
@ -232,10 +233,8 @@ async fn reindex(opts: ReIndexOpts) -> Result<()> {
)
.await
{
if let Some(error) = res.error {
tracing::error!("{}", error);
return Ok(());
}
tracing::error!("{}", e);
return Ok(());
}
}
tracing::info!("Success!");

View File

@ -115,19 +115,20 @@ async fn update_cart_item(
)
.await
{
Ok(res) => res.item,
Ok(Ok(res)) => res.item,
Ok(Err(e)) => {
tracing::error!("{}", e);
return Err(routes::Error::CriticalFailure);
}
Err(e) => {
tracing::error!("{}", e);
return Err(routes::Error::CriticalFailure);
}
};
match item {
Some(item) => Ok(Json(api::UpdateItemOutput {
shopping_cart_item: item.into(),
})),
None => Err(routes::Error::Public(super::Error::ModifyItem.into())),
}
Ok(Json(api::UpdateItemOutput {
shopping_cart_item: item.into(),
}))
}
#[put("/shopping-cart")]
@ -158,7 +159,7 @@ async fn update_cart(
.collect()
};
use channels::carts::modify_cart::{Input, Output};
use channels::carts::modify_cart::{Details, Input};
let res = {
match cart
.modify_cart(
@ -172,17 +173,11 @@ async fn update_cart(
)
.await
{
Ok(Output {
cart: Some(cart), ..
}) => cart,
Ok(Output { error: Some(e), .. }) => {
Ok(Ok(Details { cart })) => cart,
Ok(Err(e)) => {
tracing::error!("{}", e);
return Err(routes::Error::Public(super::Error::ModifyItem.into()));
}
Ok(out) => {
tracing::error!("invalid output {:?}", out);
return Err(routes::Error::Public(super::Error::ModifyItem.into()));
}
Err(e) => {
tracing::error!("{}", e);
return Err(routes::Error::Public(super::Error::ModifyItem.into()));
@ -208,7 +203,7 @@ async fn delete_cart_item(
let token = credentials.require_user(tm.into_inner()).await?;
let sc = {
use channels::carts::active_shopping_cart::{Input, Output};
use channels::carts::active_shopping_cart::{Details, Input};
match cart
.active_shopping_cart(
tarpc::context::current(),
@ -218,17 +213,11 @@ async fn delete_cart_item(
)
.await
{
Ok(Output {
cart: Some(cart), ..
}) => cart,
Ok(Output { error: Some(e), .. }) => {
Ok(Ok(Details { cart })) => cart,
Ok(Err(e)) => {
tracing::error!("{}", e);
return Err(routes::Error::Public(super::Error::ModifyItem.into()));
}
Ok(out) => {
tracing::error!("invalid output {:?}", out);
return Err(routes::Error::Public(super::Error::ModifyItem.into()));
}
Err(e) => {
tracing::error!("{e}");
return Ok(
@ -250,7 +239,11 @@ async fn delete_cart_item(
)
.await
{
Ok(_) => Ok(HttpResponse::Ok().json(api::DeleteItemOutput { success: true })),
Ok(Ok(_)) => Ok(HttpResponse::Ok().json(api::DeleteItemOutput { success: true })),
Ok(Err(e)) => {
tracing::error!("{e:?}");
Err(routes::Error::Public(PublicError::DatabaseConnection))
}
Err(e) => {
tracing::error!("{e:?}");
Err(routes::Error::Public(PublicError::DatabaseConnection))
@ -277,7 +270,11 @@ pub(crate) async fn me(
)
.await
{
Ok(me) => Ok(Json((me.account.unwrap(), me.addresses.unwrap()).into())),
Ok(Ok(me)) => Ok(Json((me.account, me.addresses).into())),
Ok(Err(e)) => {
tracing::error!("{}", e);
Err(routes::Error::CriticalFailure)
}
Err(e) => {
tracing::error!("{}", e);
Err(routes::Error::CriticalFailure)

View File

@ -33,9 +33,7 @@ async fn search(
)
.await
{
Ok(channels::search::search::Output {
found: Some(res), ..
}) => res
Ok(Ok(channels::search::search::Details { found })) => found
.into_iter()
.filter_map(|s| {
s.parse::<model::RecordId>()

View File

@ -1,7 +1,7 @@
use std::collections::HashSet;
use channels::carts::modify_cart::CartDetails;
use channels::carts::{self, Error};
use channels::carts::{self, active_shopping_cart, modify_item, Error};
use model::ShoppingCartItem;
use crate::db::*;
@ -11,7 +11,7 @@ macro_rules! begin_t {
Ok(t) => t,
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::InternalServerError);
return Err(Error::InternalServerError);
}
}
};
@ -21,7 +21,7 @@ macro_rules! end_t {
($t: ident, $res: expr) => {
if let Err(e) = $t.commit().await {
tracing::error!("{}", e);
Output::error(Error::InternalServerError)
Err(Error::InternalServerError)
} else {
$res
}
@ -42,7 +42,7 @@ pub async fn modify_item(
if let Err(e) = dbm.run(&mut t).await {
tracing::error!("{}", e);
t.rollback().await.ok();
return Output::error(Error::InternalServerError);
return Err(Error::InternalServerError);
}
let dbm = AccountShoppingCarts {
@ -53,11 +53,11 @@ pub async fn modify_item(
Ok(carts) => carts,
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::NoCarts);
return Err(Error::NoCarts);
}
};
let cart = if carts.is_empty() {
return Output::error(Error::NoCarts);
return Err(Error::NoCarts);
} else {
carts.remove(0)
};
@ -69,7 +69,7 @@ pub async fn modify_item(
Ok(res) => res,
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::NoActiveCart);
return Err(Error::NoActiveCart);
}
};
@ -77,12 +77,12 @@ pub async fn modify_item(
Some(item) if **item.quantity == 0 => {
let dbm = DeleteShoppingCartItem { id: item.id };
match dbm.run(&mut t).await {
Ok(Some(res)) => Output::item(res),
Ok(None) => Output::default(),
Ok(Some(res)) => modify_item::Details { item: res },
Ok(None) => return Err(Error::DeleteItem(item.id)),
Err(e) => {
tracing::error!("{}", e);
t.rollback().await.ok();
return Output::error(Error::DeleteItem(item.id));
return Err(Error::DeleteItem(item.id));
}
}
}
@ -95,11 +95,11 @@ pub async fn modify_item(
quantity_unit: msg.quantity_unit,
};
match dbm.run(&mut t).await {
Ok(res) => Output::item(res),
Ok(res) => modify_item::Details { item: res },
Err(e) => {
tracing::error!("{}", e);
t.rollback().await.ok();
return Output::error(Error::ModifyItem(item.id));
return Err(Error::ModifyItem(item.id));
}
}
}
@ -111,24 +111,25 @@ pub async fn modify_item(
quantity_unit: msg.quantity_unit,
};
match dbm.run(&mut t).await {
Ok(res) => Output::item(res),
Ok(res) => modify_item::Details { item: res },
Err(e) => {
tracing::error!("{}", e);
t.rollback().await.ok();
return Output::error(Error::CreateItem);
return Err(Error::CreateItem);
}
}
}
};
end_t!(t, res)
end_t!(t, Ok(res))
}
pub async fn remove_product(
msg: carts::remove_product::Input,
db: Database,
) -> carts::remove_product::Output {
use carts::remove_product::Output;
use carts::remove_product::Details;
let dbm = RemoveCartItem {
shopping_cart_id: msg.shopping_cart_id,
shopping_cart_item_id: Some(msg.shopping_cart_item_id),
@ -137,21 +138,21 @@ pub async fn remove_product(
let mut t = begin_t!(db);
let res = match dbm.run(&mut t).await {
Ok(Some(res)) => Output::item(res),
Ok(None) => Output::default(),
Ok(Some(res)) => Details { item: res },
Ok(None) => return Err(Error::DeleteItem(msg.shopping_cart_item_id)),
Err(e) => {
tracing::error!("{}", e);
Output::error(Error::DeleteItem(msg.shopping_cart_item_id))
return Err(Error::DeleteItem(msg.shopping_cart_item_id));
}
};
end_t!(t, res)
end_t!(t, Ok(res))
}
pub async fn modify_cart(
msg: carts::modify_cart::Input,
db: Database,
) -> carts::modify_cart::Output {
use carts::modify_cart::Output;
use carts::modify_cart::{CartDetails, Details};
tracing::debug!("{:?}", msg);
@ -164,7 +165,7 @@ pub async fn modify_cart(
Ok(res) => res,
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::InternalServerError);
return Err(Error::InternalServerError);
}
};
let dbm = UpdateShoppingCart {
@ -182,7 +183,7 @@ pub async fn modify_cart(
Ok(res) => res,
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::ModifyCart(cart.id));
return Err(Error::ModifyCart(cart.id));
}
};
@ -201,7 +202,7 @@ pub async fn modify_cart(
Ok(v) => v,
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::LoadItems(cart.id));
return Err(Error::LoadItems(cart.id));
}
};
@ -218,7 +219,7 @@ pub async fn modify_cart(
Ok(_) => {}
Err(e) => {
tracing::error!("{}", e);
return Output::error(Error::DeleteItem(item.id));
return Err(Error::DeleteItem(item.id));
}
};
}
@ -226,25 +227,19 @@ pub async fn modify_cart(
let mut out = Vec::with_capacity(msg.items.len());
for item in msg.items {
let res = modify_item(item, db.clone()).await;
if let carts::modify_item::Output {
error: Some(error), ..
} = &res
{
return Output::error(error.clone());
}
if let Some(item) = res.item {
out.push(item);
}
let res = modify_item(item, db.clone()).await?;
out.push(res.item);
}
end_t!(
t,
Output::cart(CartDetails {
cart_id: cart.id,
items: out,
checkout_notes: cart.checkout_notes.unwrap_or_default(),
payment_method: cart.payment_method,
Ok(Details {
cart: CartDetails {
cart_id: cart.id,
items: out,
checkout_notes: cart.checkout_notes.unwrap_or_default(),
payment_method: cart.payment_method,
}
})
)
}
@ -259,7 +254,7 @@ pub async fn active_shopping_cart(
let mut t = begin_t!(db);
let dbm = crate::db::EnsureActiveShoppingCart {
let dbm = EnsureActiveShoppingCart {
buyer_id: input.buyer_id,
};
let cart = match dbm.run(&mut t).await {
@ -267,9 +262,9 @@ pub async fn active_shopping_cart(
Err(e) => {
tracing::error!("{}", e);
t.rollback().await.ok();
return Output::error(Error::NoActiveCart);
return Err(Error::NoActiveCart);
}
};
end_t!(t, Output::cart(cart))
end_t!(t, Ok(active_shopping_cart::Details { cart }))
}

View File

@ -81,25 +81,29 @@ pub mod register {
}
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub account: Option<model::FullAccount>,
pub error: Option<Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub account: model::FullAccount,
}
pub type Output = Result<Details, Error>;
}
pub mod me {
use crate::accounts::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub account_id: model::AccountId,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub account: Option<model::FullAccount>,
pub addresses: Option<Vec<model::AccountAddress>>,
pub error: Option<super::Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub account: model::FullAccount,
pub addresses: Vec<model::AccountAddress>,
}
pub type Output = Result<Details, Error>;
}
impl AsyncClient {

View File

@ -31,27 +31,12 @@ pub mod remove_product {
pub shopping_cart_item_id: model::ShoppingCartItemId,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub item: Option<model::ShoppingCartItem>,
pub error: Option<Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub item: model::ShoppingCartItem,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn item(item: model::ShoppingCartItem) -> Self {
Self {
item: Some(item),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod modify_item {
@ -65,27 +50,12 @@ pub mod modify_item {
pub quantity_unit: model::QuantityUnit,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub item: Option<model::ShoppingCartItem>,
pub error: Option<Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub item: model::ShoppingCartItem,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn item(item: model::ShoppingCartItem) -> Self {
Self {
item: Some(item),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod modify_cart {
@ -107,27 +77,12 @@ pub mod modify_cart {
pub payment_method: model::PaymentMethod,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub cart: Option<CartDetails>,
pub error: Option<Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub cart: CartDetails,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn cart(cart: CartDetails) -> Self {
Self {
cart: Some(cart),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod active_shopping_cart {
@ -138,27 +93,12 @@ pub mod active_shopping_cart {
pub buyer_id: model::AccountId,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub error: Option<Error>,
pub cart: Option<model::ShoppingCart>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub cart: model::ShoppingCart,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn cart(cart: model::ShoppingCart) -> Self {
Self {
cart: Some(cart),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod rpc {

View File

@ -37,27 +37,12 @@ pub mod search {
}
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub found: Option<Vec<String>>,
pub error: Option<Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub found: Vec<String>,
}
impl Output {
pub fn found(found: Vec<String>) -> Self {
Self {
found: Some(found),
..Default::default()
}
}
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod suggest {
@ -84,27 +69,12 @@ pub mod suggest {
}
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub found: Option<Vec<String>>,
pub error: Option<Error>,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub found: Vec<String>,
}
impl Output {
pub fn found(found: Vec<String>) -> Self {
Self {
found: Some(found),
..Default::default()
}
}
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod create_index {
@ -184,26 +154,11 @@ pub mod create_index {
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub found: Option<()>,
pub error: Option<Error>,
}
impl Output {
pub fn ok() -> Self {
Self {
found: Some(()),
..Default::default()
}
}
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod delete_index {
@ -234,26 +189,10 @@ pub mod delete_index {
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub found: Option<()>,
pub error: Option<Error>,
}
impl Output {
pub fn ok() -> Self {
Self {
found: Some(()),
..Default::default()
}
}
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
}
pub type Output = Result<Details, Error>;
}
pub mod rpc {

View File

@ -1,27 +1,35 @@
pub mod detailed_product {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub product_id: ProductId,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Output2 {
pub product: model::Product,
pub stocks: Vec<Stock>,
pub photos: Vec<Photo>,
}
pub type Output = Result<Output2, Error>;
}
pub mod detailed_products {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub products: Vec<DetailedProduct>,
}
pub type Output = Result<Details, Error>;
}

View File

@ -12,6 +12,9 @@ pub use product_variant::*;
pub static CLIENT_NAME: &str = "stocks";
#[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)]
pub enum Error {}
pub mod rpc {
use config::SharedAppConfig;

View File

@ -1,6 +1,8 @@
pub mod create_product {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct ProductInput {
pub name: ProductName,
@ -25,11 +27,13 @@ pub mod create_product {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product: model::Product,
pub stocks: Vec<Stock>,
pub photos: Vec<Photo>,
}
pub type Output = Result<Details, Error>;
}
pub mod update_product {
@ -51,13 +55,17 @@ pub mod update_product {
pub mod delete_product {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub product_id: ProductId,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product_id: ProductId,
}
pub type Output = Result<Details, Error>;
}

View File

@ -1,6 +1,8 @@
pub mod add_product_photo {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub local_path: LocalPath,
@ -9,18 +11,22 @@ pub mod add_product_photo {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub photo_id: ProductPhotoId,
pub product_variant_id: ProductVariantId,
pub local_path: LocalPath,
pub file_name: FileName,
pub unique_name: UniqueName,
}
pub type Output = Result<Details, Error>;
}
pub mod delete_product_photo {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub photo_id: ProductPhotoId,
@ -28,8 +34,10 @@ pub mod delete_product_photo {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub photo_id: ProductPhotoId,
pub product_variant_id: ProductVariantId,
}
pub type Output = Result<Details, Error>;
}

View File

@ -1,6 +1,8 @@
pub mod create_product_stock {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub product_variant_id: ProductVariantId,
@ -9,14 +11,18 @@ pub mod create_product_stock {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product_stock: Stock,
}
pub type Output = Result<Details, Error>;
}
pub mod update_product_stock {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub id: StockId,
@ -26,7 +32,9 @@ pub mod update_product_stock {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product_stock: Stock,
}
pub type Output = Result<Details, Error>;
}

View File

@ -1,6 +1,8 @@
pub mod create_product_variant {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub product_id: ProductId,
@ -11,14 +13,18 @@ pub mod create_product_variant {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product_variant: ProductVariant,
}
pub type Output = Result<Details, Error>;
}
pub mod update_product_variant {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub id: ProductVariantId,
@ -30,14 +36,18 @@ pub mod update_product_variant {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product_variant: ProductVariant,
}
pub type Output = Result<Details, Error>;
}
pub mod delete_product_variant {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub product_id: ProductId,
@ -45,8 +55,10 @@ pub mod delete_product_variant {
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub struct Details {
pub product_id: ProductId,
pub product_variant_id: ProductVariantId,
}
pub type Output = Result<Details, Error>;
}

View File

@ -900,6 +900,8 @@ impl ProductCategory {
pub mod v2 {
use derive_more::{Deref, Display, From};
#[cfg(feature = "dummy")]
use fake::Fake;
use serde::{Deserialize, Serialize};
pub use crate::{

View File

@ -14,14 +14,14 @@ pub async fn search(msg: search::Input, ctx: Context, _config: SharedAppConfig)
} = msg;
let query = QueryRequest::new(Dest::col_buc(collection, bucket), query).lang(lang.0);
match l.query(query) {
Ok(res) => search::Output::found(res),
Ok(res) => Ok(search::Details { found: res }),
Err(e) => {
tracing::error!("{e:?}");
search::Output::error(Error::QueryFailed)
Err(Error::QueryFailed)
}
}
} else {
search::Output::found(vec![])
Ok(search::Details { found: vec![] })
}
}
@ -38,14 +38,14 @@ pub async fn suggest(
} = msg;
let query = SuggestRequest::new(Dest::col_buc(collection, bucket), query).limit(10);
match l.suggest(query) {
Ok(res) => suggest::Output::found(res),
Ok(res) => Ok(suggest::Details { found: res }),
Err(e) => {
tracing::error!("{e:?}");
suggest::Output::error(Error::QueryFailed)
Err(Error::QueryFailed)
}
}
} else {
suggest::Output::found(vec![])
Ok(suggest::Details { found: vec![] })
}
}
@ -62,14 +62,14 @@ pub async fn create_index(
)
.lang(msg.lang.0),
) {
Ok(_) => create_index::Output::ok(),
Ok(_) => Ok(create_index::Details { found: Some(()) }),
Err(e) => {
tracing::error!("push {e:?}");
create_index::Output::error(Error::CantCreate)
Err(Error::CantCreate)
}
}
} else {
create_index::Output::ok()
Ok(create_index::Details { found: None })
}
}
@ -83,14 +83,14 @@ pub async fn delete_index(
ObjDest::new(Dest::col_buc(msg.collection, msg.bucket), &msg.key),
&msg.value,
)) {
Ok(_) => delete_index::Output::ok(),
Ok(_) => Ok(delete_index::Details { found: Some(()) }),
Err(e) => {
tracing::error!("pop {e:?}");
delete_index::Output::error(Error::CantCreate)
Err(Error::CantCreate)
}
}
} else {
delete_index::Output::ok()
Ok(delete_index::Details { found: None })
}
}

View File

@ -0,0 +1,23 @@
use channels::stocks::{detailed_product, detailed_products};
use channels::AsyncClient;
use config::SharedAppConfig;
use crate::db::Database;
pub async fn detailed_product(
input: detailed_product::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> detailed_product::Output {
todo!()
}
pub async fn detailed_products(
input: detailed_products::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> detailed_products::Output {
todo!()
}

View File

@ -0,0 +1,11 @@
pub mod load;
pub mod product;
pub mod product_photo;
pub mod product_stock;
pub mod product_variant;
pub use load::*;
pub use product::*;
pub use product_photo::*;
pub use product_stock::*;
pub use product_variant::*;

View File

@ -0,0 +1,32 @@
use channels::stocks::{create_product, delete_product, update_product};
use channels::AsyncClient;
use config::SharedAppConfig;
use crate::db::Database;
pub async fn create_product(
input: create_product::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> create_product::Output {
todo!()
}
pub async fn update_product(
input: update_product::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> update_product::Output {
todo!()
}
pub async fn delete_product(
input: delete_product::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> delete_product::Output {
todo!()
}

View File

@ -0,0 +1,23 @@
use channels::stocks::{add_product_photo, delete_product_photo};
use channels::AsyncClient;
use config::SharedAppConfig;
use crate::db::Database;
pub async fn add_product_photo(
input: add_product_photo::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> add_product_photo::Output {
todo!()
}
pub async fn delete_product_photo(
input: delete_product_photo::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> delete_product_photo::Output {
todo!()
}

View File

@ -0,0 +1,23 @@
use channels::stocks::{create_product_stock, update_product_stock};
use channels::AsyncClient;
use config::SharedAppConfig;
use crate::db::Database;
pub async fn create_product_stock(
input: create_product_stock::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> create_product_stock::Output {
todo!()
}
pub async fn update_product_stock(
input: update_product_stock::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> update_product_stock::Output {
todo!()
}

View File

@ -0,0 +1,32 @@
use channels::stocks::{create_product_variant, delete_product_variant, update_product_variant};
use channels::AsyncClient;
use config::SharedAppConfig;
use crate::db::Database;
pub async fn create_product_variant(
input: create_product_variant::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> create_product_variant::Output {
todo!()
}
pub async fn update_product_variant(
input: update_product_variant::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> update_product_variant::Output {
todo!()
}
pub async fn delete_product_variant(
input: delete_product_variant::Input,
db: Database,
mqtt: AsyncClient,
config: SharedAppConfig,
) -> delete_product_variant::Output {
todo!()
}

View File

@ -1,4 +1,6 @@
use config::SharedAppConfig;
use sqlx_core::pool::Pool;
use sqlx_core::postgres::Postgres;
mod photos;
mod product_photos;
@ -24,5 +26,7 @@ impl Database {
}
}
pub fn pool(&self) {}
pub fn pool(&self) -> Pool<Postgres> {
self.pool.clone()
}
}

View File

@ -1,8 +1,23 @@
use config::UpdateConfig;
mod actions;
mod context;
mod db;
mod mqtt;
mod rpc;
pub struct Opts {}
impl UpdateConfig for Opts {}
#[tokio::main]
async fn main() {}
async fn main() {
let opts = Opts {};
let config = config::default_load(&opts);
let db = db::Database::build(config.clone()).await;
let mqtt_client = mqtt::start(config.clone(), db.clone()).await;
rpc::start(config, db, mqtt_client).await;
}

View File

@ -0,0 +1,30 @@
use config::SharedAppConfig;
use rumqttc::{Event, Incoming};
use crate::db::Database;
pub async fn start(config: SharedAppConfig, _db: Database) -> channels::AsyncClient {
let (client, mut event_loop) = channels::stocks::mqtt::create_client(config);
let spawn_client = client.clone();
tokio::spawn(async move {
let _client = spawn_client.clone();
loop {
let notification = event_loop.poll().await;
match notification {
Ok(Event::Incoming(Incoming::Publish(publish))) => match publish.topic.as_str() {
_ => {}
},
Ok(Event::Incoming(_incoming)) => {}
Ok(Event::Outgoing(_outgoing)) => {}
Err(e) => {
tracing::error!("{}", e);
}
}
}
// tracing::info!("Mqtt channel closed");
});
client
}

View File

@ -0,0 +1,135 @@
use channels::stocks::rpc::Stocks;
use channels::AsyncClient;
use config::SharedAppConfig;
use crate::db::Database;
use crate::rpc::rpc::StocksServer;
pub mod rpc {
use channels::stocks::rpc::Stocks;
use channels::stocks::*;
use config::SharedAppConfig;
use tarpc::context;
use crate::actions;
#[derive(Clone)]
pub struct StocksServer {
pub db: crate::db::Database,
pub mqtt_client: channels::AsyncClient,
pub config: SharedAppConfig,
}
#[tarpc::server]
impl Stocks for StocksServer {
async fn create_product(
self,
_: context::Context,
input: create_product::Input,
) -> create_product::Output {
actions::create_product(input, self.db, self.mqtt_client, self.config).await
}
async fn update_product(
self,
_: context::Context,
input: update_product::Input,
) -> update_product::Output {
actions::update_product(input, self.db, self.mqtt_client, self.config).await
}
async fn delete_product(
self,
_: context::Context,
input: delete_product::Input,
) -> delete_product::Output {
actions::delete_product(input, self.db, self.mqtt_client, self.config).await
}
async fn create_product_variant(
self,
_: context::Context,
input: create_product_variant::Input,
) -> create_product_variant::Output {
actions::create_product_variant(input, self.db, self.mqtt_client, self.config).await
}
async fn update_product_variant(
self,
_: context::Context,
input: update_product_variant::Input,
) -> update_product_variant::Output {
actions::update_product_variant(input, self.db, self.mqtt_client, self.config).await
}
async fn delete_product_variant(
self,
_: context::Context,
input: delete_product_variant::Input,
) -> delete_product_variant::Output {
actions::delete_product_variant(input, self.db, self.mqtt_client, self.config).await
}
async fn add_product_photo(
self,
_: context::Context,
input: add_product_photo::Input,
) -> add_product_photo::Output {
actions::add_product_photo(input, self.db, self.mqtt_client, self.config).await
}
async fn delete_product_photo(
self,
_: context::Context,
input: delete_product_photo::Input,
) -> delete_product_photo::Output {
actions::delete_product_photo(input, self.db, self.mqtt_client, self.config).await
}
async fn create_product_stock(
self,
_: context::Context,
input: create_product_stock::Input,
) -> create_product_stock::Output {
actions::create_product_stock(input, self.db, self.mqtt_client, self.config).await
}
async fn update_product_stock(
self,
_: context::Context,
input: update_product_stock::Input,
) -> update_product_stock::Output {
actions::update_product_stock(input, self.db, self.mqtt_client, self.config).await
}
async fn detailed_product(
self,
_: context::Context,
input: detailed_product::Input,
) -> detailed_product::Output {
actions::detailed_product(input, self.db, self.mqtt_client, self.config).await
}
async fn detailed_products(
self,
_: context::Context,
input: detailed_products::Input,
) -> detailed_products::Output {
actions::detailed_products(input, self.db, self.mqtt_client, self.config).await
}
}
}
pub async fn start(config: SharedAppConfig, db: Database, mqtt_client: AsyncClient) {
let port = { config.lock().stocks_manager().rpc_port };
channels::rpc::start("stocks", port, || {
StocksServer {
db: db.clone(),
config: config.clone(),
mqtt_client: mqtt_client.clone(),
}
.serve()
})
.await;
}