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

136 lines
3.1 KiB
Rust

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()
}
}
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)]
pub struct Details {
pub category: Option<Category>,
}
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 {
pub category: Category,
}
pub type Output = Result<Details, Error>;
}
pub mod all_categories {
use model::v2::*;
use crate::stocks::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub limit: Limit,
pub offset: Offset,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub categories: Vec<Category>,
}
pub type Output = Result<Details, Error>;
}