Saving functionality is looking better

This commit is contained in:
Chris Cochrun 2024-10-12 21:54:57 -05:00
parent 3e1e46ce2b
commit a99e4fb3cf
15 changed files with 421 additions and 54 deletions

View file

@ -1,7 +1,7 @@
use std::path::PathBuf;
use color_eyre::eyre::Result;
use serde::{Deserialize, Serialize};
use sqlx::query;
use sqlx::{prelude::FromRow, query, sqlite::SqliteRow, Row, SqliteConnection};
use tracing::error;
use crate::model::Model;
@ -35,6 +35,24 @@ impl Presentation {
}
}
impl FromRow<'_, SqliteRow> for Presentation {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
Ok(Self {
id: row.try_get(0)?,
title: row.try_get(1)?,
path: PathBuf::from({
let string: String = row.try_get(2)?;
string
}),
kind: if row.try_get(3)? {
PresKind::Html
} else {
PresKind::Pdf
},
})
}
}
impl Model<Presentation> {
pub fn load_from_db(&mut self) {
let rt = tokio::runtime::Runtime::new().unwrap();
@ -61,6 +79,11 @@ impl Model<Presentation> {
}
}
pub async fn get_presentation_from_db(database_id: i32, db: &mut SqliteConnection) -> Result<Presentation> {
let row = query(r#"SELECT id as "id: i32", title, filePath as "path", html from presentations where id = $1"#).bind(database_id).fetch_one(db).await?;
Ok(Presentation::from_row(&row)?)
}
#[cfg(test)]
mod test {
use super::*;