use uuid::Uuid;
use backbone_core::GenericCrudService;
use crate::domain::entity::Event;
use crate::infrastructure::persistence::event_command_repository::{
CreateEventInput, EventCommandRepository, EventRow, PatchEventInput,
};
use crate::presentation::dto::{CreateEventDto, UpdateEventDto};
pub type EventService = GenericCrudService<
Event,
CreateEventDto,
UpdateEventDto,
crate::infrastructure::persistence::EventRepository,
>;
use super::event_error::{EventError, EventResult};
pub const PUBLISH_FENCED_FIELDS: [&str; 2] = ["is_published", "date_publish"];
pub struct EventCommandService {
events: EventCommandRepository,
}
impl EventCommandService {
pub fn new(events: EventCommandRepository) -> Self {
Self { events }
}
pub async fn create(
&self,
input: &CreateEventInput,
actor: Option<Uuid>,
) -> EventResult<EventRow> {
if input.date_end < input.date_begin {
return Err(EventError::Validation(
"date_end precedes date_begin".to_string(),
));
}
if input.is_multi_slots && input.event_slot_count < 1 {
return Err(EventError::Validation(
"multi-slot events declare at least one slot".to_string(),
));
}
self.events.create(input, actor).await
}
pub async fn patch(
&self,
id: Uuid,
patch: &PatchEventInput,
actor: Option<Uuid>,
) -> EventResult<EventRow> {
self.events.patch(id, patch, actor).await
}
pub async fn publish(&self, id: Uuid, actor: Option<Uuid>) -> EventResult<EventRow> {
self.events.publish(id, actor).await
}
pub async fn unpublish(&self, id: Uuid, actor: Option<Uuid>) -> EventResult<EventRow> {
self.events.unpublish(id, actor).await
}
pub async fn mark_done(&self, id: Uuid, actor: Option<Uuid>) -> EventResult<EventRow> {
self.events.mark_done(id, actor).await
}
pub async fn get(&self, id: Uuid) -> EventResult<EventRow> {
self.events.find(id).await
}
pub async fn list(&self, limit: i64) -> EventResult<Vec<EventRow>> {
self.events.list(limit).await
}
pub async fn linked_products(&self) -> EventResult<Vec<Uuid>> {
self.events.list_linked_products().await
}
}