post services integrations
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
use crate::services::ServicesContext;
|
||||
use crate::models::category;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, TransactionTrait, Set};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CategoryService {
|
||||
service_context: Arc<ServicesContext>,
|
||||
}
|
||||
|
||||
impl CategoryService {
|
||||
pub fn new(service_context: Arc<ServicesContext>) -> Self {
|
||||
Self { service_context }
|
||||
}
|
||||
|
||||
pub async fn create_category(
|
||||
&self,
|
||||
server_id: Uuid,
|
||||
name: String,
|
||||
) -> Result<category::Model, anyhow::Error> {
|
||||
let db = &self.service_context.repositories.server.context.db;
|
||||
let event_bus = &self.service_context.event_bus;
|
||||
|
||||
let txn = db.begin().await?;
|
||||
|
||||
let active = category::ActiveModel {
|
||||
server_id: Set(server_id),
|
||||
name: Set(name),
|
||||
..Default::default()
|
||||
};
|
||||
let cat = active.insert(&txn).await?;
|
||||
|
||||
txn.commit().await?;
|
||||
|
||||
event_bus.emit("category_created", cat.clone());
|
||||
|
||||
Ok(cat)
|
||||
}
|
||||
|
||||
pub async fn update_category(
|
||||
&self,
|
||||
id: Uuid,
|
||||
name: String,
|
||||
) -> Result<category::Model, anyhow::Error> {
|
||||
let db = &self.service_context.repositories.server.context.db;
|
||||
let event_bus = &self.service_context.event_bus;
|
||||
|
||||
let txn = db.begin().await?;
|
||||
|
||||
let existing = category::Entity::find_by_id(id)
|
||||
.one(&txn)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("Category not found"))?;
|
||||
|
||||
let mut active: category::ActiveModel = existing.into();
|
||||
active.name = Set(name);
|
||||
|
||||
let cat = active.update(&txn).await?;
|
||||
|
||||
txn.commit().await?;
|
||||
|
||||
event_bus.emit("category_updated", cat.clone());
|
||||
|
||||
Ok(cat)
|
||||
}
|
||||
|
||||
pub async fn delete_category(&self, id: Uuid) -> Result<bool, anyhow::Error> {
|
||||
let db = &self.service_context.repositories.server.context.db;
|
||||
let event_bus = &self.service_context.event_bus;
|
||||
|
||||
let txn = db.begin().await?;
|
||||
|
||||
let res = category::Entity::delete_by_id(id)
|
||||
.exec(&txn)
|
||||
.await?;
|
||||
|
||||
let deleted = res.rows_affected > 0;
|
||||
|
||||
txn.commit().await?;
|
||||
|
||||
if deleted {
|
||||
event_bus.emit("category_deleted", id);
|
||||
}
|
||||
|
||||
Ok(deleted)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user