From 6ca4863c6b997b2b6ee03dfb3444d06b1b23efd8 Mon Sep 17 00:00:00 2001 From: jakubmanczak Date: Mon, 8 Jul 2024 16:36:07 +0200 Subject: [PATCH] make splash route have expected behavior --- src/routes/splash.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/routes/splash.rs b/src/routes/splash.rs index d454174..9d2d50c 100644 --- a/src/routes/splash.rs +++ b/src/routes/splash.rs @@ -1,9 +1,20 @@ -use axum::{routing::get, Router}; +use crate::setup; +use axum::{http::StatusCode, routing::get, Router}; +use sqlite::State; pub fn route() -> Router { - Router::new().route("/splash", get(random_splash())) + Router::new().route("/splash", get(random_splash)) } -fn random_splash() -> String { - "Hello".to_owned() +async fn random_splash() -> (StatusCode, String) { + let conn = setup::initialise_sqlite_connection(); + let query = "SELECT splash FROM splashes ORDER BY RANDOM() LIMIT 1"; + + let mut statement = conn.prepare(query).unwrap(); + + while let Ok(State::Row) = statement.next() { + return (StatusCode::OK, statement.read::(0).unwrap()); + } + + return (StatusCode::INTERNAL_SERVER_ERROR, "".to_owned()); }