rship-entities-core 4.0.0-canary.53

Core entities for rship
Documentation
use myko::{
    command::{CommandContext, CommandError, CommandHandler},
    myko_command, myko_item,
    prelude::Uuid,
};

#[myko_item]
pub struct Project {
    /// Display name
    #[searchable]
    #[myko_rename]
    pub name: String,

    /// Description
    #[serde(default)]
    pub description: Option<String>,
}

// ─────────────────────────────────────────────────────────────────────────────
// Custom Commands
// ─────────────────────────────────────────────────────────────────────────────

#[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(())
    }
}

// Note: DeleteProject command is auto-generated by #[myko_item]