#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EventLogDto {
pub sequence: u64,
pub event_type: String,
pub revision: u64,
pub payload: String,
pub recorded_at: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CounterViewDto {
pub count: i32,
pub latest_events: Vec<EventLogDto>,
pub last_sequence: u64,
pub realtime_enabled: bool,
}
#[server(prefix = "/api")]
pub async fn get_counter_view() -> Result<CounterViewDto, ServerFnError> {
#[cfg(feature = "ssr")]
{
get_counter_view_db().await
}
#[cfg(not(feature = "ssr"))]
{
unreachable!()
}
}
#[server(prefix = "/api")]
pub async fn increment_count(amount: i32) -> Result<CounterViewDto, ServerFnError> {
#[cfg(feature = "ssr")]
{
if amount <= 0 {
return Err(server_fn_error(crate::error::CounterAppError::validation(
"amount must be positive",
)));
}
run_cqrs_command(crate::domain::CounterCommand::Increment { amount }).await
}
#[cfg(not(feature = "ssr"))]
{
let _ = amount;
unreachable!()
}
}
#[server(prefix = "/api")]
pub async fn decrement_count(amount: i32) -> Result<CounterViewDto, ServerFnError> {
#[cfg(feature = "ssr")]
{
if amount <= 0 {
return Err(server_fn_error(crate::error::CounterAppError::validation(
"amount must be positive",
)));
}
run_cqrs_command(crate::domain::CounterCommand::Decrement { amount }).await
}
#[cfg(not(feature = "ssr"))]
{
let _ = amount;
unreachable!()
}
}
#[server(prefix = "/api")]
pub async fn reset_count() -> Result<CounterViewDto, ServerFnError> {
#[cfg(feature = "ssr")]
{
run_cqrs_command(crate::domain::CounterCommand::Reset).await
}
#[cfg(not(feature = "ssr"))]
{
unreachable!()
}
}