bazzar/crates/stock_manager/src/rpc.rs
2022-11-29 15:18:31 +01:00

182 lines
5.4 KiB
Rust

use channels::stocks::create_category::{Input, Output};
use channels::stocks::rpc::Stocks;
use channels::stocks::*;
use channels::AsyncClient;
use config::SharedAppConfig;
use tarpc::context;
use crate::actions;
use crate::db::Database;
#[derive(Clone)]
pub struct StocksServer {
pub db: Database,
pub mqtt_client: 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 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
}
async fn shopping_cart_products(
self,
_: context::Context,
input: find_products::Input,
) -> find_products::Output {
actions::find_products(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 shopping_cart_product_variants(
self,
_: context::Context,
input: find_product_variants::Input,
) -> find_product_variants::Output {
actions::find_product_variants(input, self.db, self.mqtt_client, self.config).await
}
async fn all_product_photo(
self,
_: context::Context,
input: all_product_photo::Input,
) -> all_product_photo::Output {
actions::all_product_photo(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 create_category(self, _: context::Context, input: Input) -> Output {
actions::create_category(input, self.db, self.mqtt_client, self.config).await
}
async fn delete_category(
self,
_: context::Context,
input: delete_category::Input,
) -> delete_category::Output {
actions::delete_category(input, self.db, self.mqtt_client, self.config).await
}
async fn update_category(
self,
_: context::Context,
input: update_category::Input,
) -> update_category::Output {
actions::update_category(input, self.db, self.mqtt_client, self.config).await
}
async fn all_categories(
self,
_: context::Context,
input: all_categories::Input,
) -> all_categories::Output {
actions::all_categories(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;
}