bazzar/crates/channels/src/stocks/categories.rs

136 lines
3.1 KiB
Rust
Raw Normal View History

2022-11-29 11:11:04 +01:00
use rumqttc::QoS;
use crate::AsyncClient;
impl AsyncClient {
pub async fn emit_category_created(&self, category: &model::v2::Category) {
self.publish_or_log(Topic::CategoryCreated, QoS::AtLeastOnce, true, category)
.await
}
pub async fn emit_category_updated(&self, category: &model::v2::Category) {
self.publish_or_log(Topic::CategoryUpdated, QoS::AtLeastOnce, true, category)
.await
}
pub async fn emit_category_deleted(&self, category_id: &model::v2::CategoryId) {
self.publish_or_log(Topic::CategoryDeleted, QoS::AtLeastOnce, true, category_id)
.await
}
}
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Topic {
CategoryCreated,
CategoryUpdated,
CategoryDeleted,
}
impl Topic {
pub fn to_str(self) -> &'static str {
match self {
Topic::CategoryCreated => "category/created",
Topic::CategoryUpdated => "category/updated",
Topic::CategoryDeleted => "category/deleted",
}
}
}
impl Into<String> for Topic {
fn into(self) -> String {
self.to_str().into()
}
}
impl<'s> PartialEq<&'s str> for Topic {
fn eq(&self, other: &&'s str) -> bool {
self.to_str() == *other
}
}
impl PartialEq<String> for Topic {
fn eq(&self, other: &String) -> bool {
self.to_str() == other.as_str()
}
}
2022-11-29 08:46:02 +01:00
pub mod create_category {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub parent_id: Option<CategoryId>,
pub name: CategoryName,
pub key: CategoryKey,
pub svg: CategorySvg,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub product: Category,
}
pub type Output = Result<Details, Error>;
}
pub mod delete_category {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub category_id: CategoryId,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
2022-11-29 11:11:04 +01:00
pub struct Details {
pub category: Option<Category>,
}
2022-11-29 08:46:02 +01:00
pub type Output = Result<Details, Error>;
}
pub mod update_category {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub id: CategoryId,
pub parent_id: Option<CategoryId>,
pub name: CategoryName,
pub key: CategoryKey,
pub svg: CategorySvg,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
2022-11-29 11:11:04 +01:00
pub category: Category,
2022-11-29 08:46:02 +01:00
}
pub type Output = Result<Details, Error>;
}
pub mod all_categories {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
2022-11-29 11:11:04 +01:00
pub struct Input {
pub limit: Limit,
pub offset: Offset,
}
2022-11-29 08:46:02 +01:00
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub categories: Vec<Category>,
}
pub type Output = Result<Details, Error>;
}