AkurAI Build
Menu

Public repository / master

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

Latest change 21fd83f4a6eb769a0ee34aa30f721b5cdc6881af - Fix IDP OAuth: split public issuer vs internal loopback URL for token exchange 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)
    }
}