use distributed::{sourced, Entity};
use serde::{Deserialize, Serialize};
#[derive(Default)]
pub struct Todo {
pub entity: Entity,
user_id: String,
task: String,
completed: bool,
}
#[sourced(entity)]
impl Todo {
pub fn new() -> Self {
Self::default()
}
#[event("initialized")]
pub fn initialize(&mut self, id: String, user_id: String, task: String) {
self.entity.set_id(&id);
self.user_id = user_id;
self.task = task;
}
#[event("completed", when = !self.completed)]
pub fn complete(&mut self) {
self.completed = true;
}
pub fn snapshot(&self) -> TodoSnapshot {
TodoSnapshot {
id: self.entity.id().to_string(),
user_id: self.user_id.clone(),
task: self.task.clone(),
completed: self.completed,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TodoSnapshot {
pub id: String,
pub user_id: String,
pub task: String,
pub completed: bool,
}