fetch newest quote for dashboard, helpers
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use axum::{http::StatusCode, response::IntoResponse};
|
||||
use chrono::{DateTime, FixedOffset};
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use rusqlite::{Connection, OptionalExtension};
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
@@ -34,6 +34,15 @@ pub enum QuoteError {
|
||||
DatabaseError(#[from] DatabaseError),
|
||||
}
|
||||
|
||||
impl Quote {
|
||||
pub fn get_creation_timestamp(&self) -> DateTime<Utc> {
|
||||
// unwrap here because all IDs use UUIDv7
|
||||
let (s, n) = self.id.get_timestamp().unwrap().to_unix();
|
||||
// unwrap here because timestamps held by UUIDs are valid by spec
|
||||
DateTime::from_timestamp(s as i64, n).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Quote {
|
||||
pub fn total_count(conn: &Connection) -> Result<i64, QuoteError> {
|
||||
Ok(conn.query_row("SELECT COUNT(*) FROM quotes", (), |r| r.get(0))?)
|
||||
@@ -92,6 +101,18 @@ impl Quote {
|
||||
public,
|
||||
})
|
||||
}
|
||||
pub fn get_newest(conn: &Connection) -> Result<Option<Quote>, QuoteError> {
|
||||
let id: Option<Uuid> = conn
|
||||
.query_row("SELECT id FROM quotes ORDER BY id DESC LIMIT 1", (), |r| {
|
||||
r.get(0)
|
||||
})
|
||||
.optional()?;
|
||||
|
||||
match id {
|
||||
Some(id) => Ok(Some(Self::get_by_id(conn, id)?)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
pub fn create(
|
||||
conn: &Connection,
|
||||
lines: Vec<(String, Name)>,
|
||||
|
||||
Reference in New Issue
Block a user