use std::sync::Arc; use application::channels::{ ChannelCommandDeps, CreateChannelCommand, DeleteChannelCommand, UpdateChannelCommand, }; use uuid::Uuid; use crate::error::{domain_err, ok_json}; pub async fn list_channels( channel_query: &Arc, owner_id: Uuid, ) -> String { match channel_query.find_by_owner(owner_id.into()).await { Ok(channels) => ok_json(&channels), Err(e) => domain_err(e), } } pub async fn get_channel( channel_query: &Arc, id: Uuid, ) -> String { match channel_query.find_by_id(id.into()).await { Ok(Some(channel)) => ok_json(&channel), Ok(None) => serde_json::json!({"error": "Channel not found"}).to_string(), Err(e) => domain_err(e), } } pub async fn create_channel( cmd_deps: &Arc, owner_id: Uuid, name: &str, timezone: &str, ) -> String { let cmd = CreateChannelCommand { owner_id: owner_id.into(), name: name.to_string(), timezone: timezone.to_string(), }; match application::channels::create::execute(cmd_deps, cmd).await { Ok(channel) => ok_json(&channel), Err(e) => domain_err(e), } } pub async fn update_channel( cmd_deps: &Arc, channel_id: Uuid, owner_id: Uuid, name: Option, timezone: Option, description: Option, schedule_config: Option, ) -> String { let cmd = UpdateChannelCommand { channel_id: channel_id.into(), owner_id: owner_id.into(), name, description: description.map(Some), timezone, schedule_config, rotation_policy: None, auto_schedule: None, }; match application::channels::update::execute(cmd_deps, cmd).await { Ok(channel) => ok_json(&channel), Err(e) => domain_err(e), } } pub async fn delete_channel( cmd_deps: &Arc, channel_id: Uuid, owner_id: Uuid, ) -> String { let cmd = DeleteChannelCommand { channel_id: channel_id.into(), owner_id: owner_id.into(), }; match application::channels::delete::execute(cmd_deps, cmd).await { Ok(()) => serde_json::json!({"deleted": channel_id}).to_string(), Err(e) => domain_err(e), } }