use std::collections::BTreeSet;
use rusqlite::Row;
use serde::Serialize;
use crate::applebooks::database::{ABDatabaseName, ABQuery};
use super::datetime::DateTimeUtc;
#[derive(Debug, Default, Clone, Serialize)]
pub struct Book {
pub title: String,
pub author: String,
pub tags: BTreeSet<String>,
pub metadata: BookMetadata,
}
impl ABQuery for Book {
const DATABASE_NAME: ABDatabaseName = ABDatabaseName::Books;
const QUERY: &'static str = {
"SELECT
ZBKLIBRARYASSET.ZTITLE, -- 0 title
ZBKLIBRARYASSET.ZAUTHOR, -- 1 author
ZBKLIBRARYASSET.ZASSETID, -- 2 id
ZBKLIBRARYASSET.ZLASTOPENDATE -- 3 last_opened
FROM ZBKLIBRARYASSET
ORDER BY ZBKLIBRARYASSET.ZTITLE;"
};
fn from_row(row: &Row<'_>) -> Self {
let last_opened: f64 = row.get_unwrap(3);
Self {
title: row.get_unwrap(0),
author: row.get_unwrap(1),
tags: BTreeSet::new(),
metadata: BookMetadata {
id: row.get_unwrap(2),
last_opened: last_opened.into(),
},
}
}
}
#[derive(Debug, Default, Clone, Serialize)]
pub struct BookMetadata {
pub id: String,
pub last_opened: DateTimeUtc,
}