AkurAI Build
Menu

Public repository / b78e3a5270c5c5e1b0b04f1da8da4899aa5e4e22

AkurAI-Notes / crates/notes-core/src/entities/note.rs

Latest change 3f3c3218239131fdc60fb9196283c9a0162898a9 - E04: notes-core entities + CRUD handlers + collections schema + seed data by Claude Code

use akurai_json::Value;

pub struct Note {
    pub id: i64,
    pub title: String,
    pub body: String,
    pub format: String,
    pub notebook_id: i64,
    pub owner_sub: String,
    pub tag_ids: Vec<i64>,
    pub created_at: i64,
    pub updated_at: i64,
    pub archived: bool,
}

impl Note {
    pub fn from_json(v: &Value) -> Option<Self> {
        Some(Self {
            id: v.get("id")?.as_i64()?,
            title: v.get("title")?.as_str()?.to_string(),
            body: v.get("body")?.as_str().unwrap_or("").to_string(),
            format: v.get("format")?.as_str().unwrap_or("markdown").to_string(),
            notebook_id: v.get("notebookId")?.as_i64()?,
            owner_sub: v.get("ownerSub")?.as_str()?.to_string(),
            tag_ids: v.get("tagIds")?.as_str().unwrap_or("").split(',').filter_map(|s| s.trim().parse().ok()).collect(),
            created_at: v.get("createdAt")?.as_i64()?,
            updated_at: v.get("updatedAt")?.as_i64()?,
            archived: v.get("archived")?.as_bool().unwrap_or(false),
        })
    }

    pub fn to_json(&self) -> Value {
        Value::Object(vec![
            ("id".into(), Value::Int(self.id)),
            ("title".into(), Value::Str(self.title.clone())),
            ("body".into(), Value::Str(self.body.clone())),
            ("format".into(), Value::Str(self.format.clone())),
            ("notebookId".into(), Value::Int(self.notebook_id)),
            ("ownerSub".into(), Value::Str(self.owner_sub.clone())),
            ("tagIds".into(), Value::Str(self.tag_ids.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(","))),
            ("createdAt".into(), Value::Int(self.created_at)),
            ("updatedAt".into(), Value::Int(self.updated_at)),
            ("archived".into(), Value::Bool(self.archived)),
        ])
    }

    pub fn embed_text(&self) -> String {
        format!("{} {}", self.title, self.body)
    }
}