Skip to content

Commit

Permalink
add /splash-json & /splashes routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmanczak committed Jul 11, 2024
1 parent 47fba62 commit 08ca6b4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
33 changes: 27 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.7.5"
axum = { version = "0.7.5", features = ["macros"] }
tokio = { version = "1.38.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
dotenvy = "0.15.7"
sqlite = "0.36.0"
tower-http = { version = "0.5.2", features = ["cors"] }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
57 changes: 55 additions & 2 deletions src/routes/splash.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
use crate::setup;
use axum::{http::StatusCode, routing::get, Router};
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Json, Router,
};
use serde::{Deserialize, Serialize};
use sqlite::State;

pub fn route() -> Router {
Router::new().route("/splash", get(random_splash))
Router::new()
.route("/splash", get(random_splash))
.route("/splash-json", get(random_splash_json))
.route("/splashes", get(all_splashes))
}

#[derive(Serialize, Deserialize)]
struct Splash {
id: String,
splash: String,
}

async fn random_splash() -> (StatusCode, String) {
Expand All @@ -18,3 +33,41 @@ async fn random_splash() -> (StatusCode, String) {

return (StatusCode::INTERNAL_SERVER_ERROR, "".to_owned());
}

async fn random_splash_json() -> Response {
let conn = setup::initialise_sqlite_connection();
let query = "SELECT * FROM splashes ORDER BY RANDOM() LIMIT 1";

let mut statement = conn.prepare(query).unwrap();

while let Ok(State::Row) = statement.next() {
let splash = Splash {
id: statement.read::<String, _>("id").unwrap(),
splash: statement.read::<String, _>("splash").unwrap(),
};
return Json(splash).into_response();
}

return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}

async fn all_splashes() -> Response {
let conn = setup::initialise_sqlite_connection();
let query = "SELECT * FROM splashes";

let mut statement = conn.prepare(query).unwrap();
let mut splashes: Vec<Splash> = Vec::new();

while let Ok(State::Row) = statement.next() {
splashes.push(Splash {
id: statement.read::<String, _>("id").unwrap(),
splash: statement.read::<String, _>("splash").unwrap(),
});
}

if !splashes.is_empty() {
return Json(splashes).into_response();
}

return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}

0 comments on commit 08ca6b4

Please sign in to comment.