use myko::{
command::{CommandContext, CommandError, CommandHandler},
myko_command, myko_item,
prelude::Uuid,
};
#[myko_item]
pub struct Project {
#[searchable]
#[myko_rename]
pub name: String,
#[serde(default)]
pub description: Option<String>,
}
#[myko_command(ProjectId)]
pub struct CreateProject {
pub name: String,
#[serde(default)]
pub description: Option<String>,
}
impl CommandHandler for CreateProject {
fn execute(self, ctx: CommandContext) -> Result<ProjectId, CommandError> {
let id = ProjectId(Uuid::new_v4().to_string().into());
let project = Project {
id: id.clone(),
name: self.name,
description: self.description,
};
ctx.emit_set(&project)?;
Ok(id)
}
}
#[myko_command]
pub struct SaveProject {
pub id: ProjectId,
pub name: String,
#[serde(default)]
pub description: Option<String>,
}
impl CommandHandler for SaveProject {
fn execute(self, ctx: CommandContext) -> Result<(), CommandError> {
let project = Project {
id: self.id,
name: self.name,
description: self.description,
};
ctx.emit_set(&project)?;
Ok(())
}
}