diff --git a/src/api/mod.rs b/src/api/mod.rs index 5bae6c2..592d409 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -42,6 +42,7 @@ pub fn api_router() -> Router { .route("/api/persons/{id}", get(persons::get_by_id)) .route("/api/persons/{id}/names", get(persons::pid_names)) .route("/api/persons/{id}/addname", post(persons::add_name)) + .route("/api/names", get(persons::n_all)) .route("/api/names/{id}", get(persons::n_by_id)) .route("/api/names/{id}/setprimary", post(persons::n_setprimary)) // quotes diff --git a/src/api/persons.rs b/src/api/persons.rs index 343f6f9..5772f26 100644 --- a/src/api/persons.rs +++ b/src/api/persons.rs @@ -93,6 +93,11 @@ pub async fn add_name( Ok((StatusCode::CREATED, Json(n)).into_response()) } +pub async fn n_all(headers: HeaderMap) -> Result { + User::authenticate(&headers)?.required()?; + let conn = database::conn()?; + Ok(Json(Name::get_all(&conn)?).into_response()) +} pub async fn n_by_id(Path(id): Path, headers: HeaderMap) -> Result { User::authenticate(&headers)?.required()?; let conn = database::conn()?; diff --git a/src/quotes/mod.rs b/src/quotes/mod.rs index 33aa529..48fefb3 100644 --- a/src/quotes/mod.rs +++ b/src/quotes/mod.rs @@ -113,6 +113,20 @@ impl Quote { None => Ok(None), } } + pub fn get_newest_public(conn: &Connection) -> Result, QuoteError> { + let id: Option = conn + .query_row( + "SELECT id FROM quotes ORDER BY id DESC LIMIT 1 WHERE public = 1", + (), + |r| r.get(0), + ) + .optional()?; + + match id { + Some(id) => Ok(Some(Self::get_by_id(conn, id)?)), + None => Ok(None), + } + } pub fn get_chronological_offset( conn: &Connection, offset: i64, diff --git a/src/web/pages/dashboard.rs b/src/web/pages/dashboard.rs index 34f68e8..29c803f 100644 --- a/src/web/pages/dashboard.rs +++ b/src/web/pages/dashboard.rs @@ -25,7 +25,10 @@ pub async fn page(req: Request) -> Result { let u = User::authenticate(req.headers()).ok().flatten(); let conn = database::conn()?; - let newest_quote = Quote::get_newest(&conn)?; + let newest_quote = match u { + Some(_) => Quote::get_newest(&conn)?, + None => Quote::get_newest_public(&conn)?, + }; Ok(base( "Dashboard | Mnemosyne",