From 08a1d16f88417e308a6d484af9710cc43a58de8f Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 28 Oct 2023 14:48:08 +0200 Subject: [PATCH 1/2] add functionality to show log --- src/model/log.rs | 12 ++++++++++++ src/tera/admin/mod.rs | 21 +++++++++++++++------ src/tera/misc.rs | 3 +-- src/tera/stat.rs | 2 +- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/model/log.rs b/src/model/log.rs index c2b04a3..b862c79 100644 --- a/src/model/log.rs +++ b/src/model/log.rs @@ -51,4 +51,16 @@ LIMIT 1000 ret.push_str(""); ret.replace('\n', "") } + + pub async fn show(db: &SqlitePool) -> String { + let mut ret = String::new(); + + for log in Self::last(db).await { + let utc_time: DateTime = Utc::from_utc_datetime(&Utc, &log.created_at); + let local_time = utc_time.with_timezone(&Local); + ret.push_str(&format!("- {} - {}\n", local_time, log.msg)); + } + + ret + } } diff --git a/src/tera/admin/mod.rs b/src/tera/admin/mod.rs index ee4f5fb..0bf05e7 100644 --- a/src/tera/admin/mod.rs +++ b/src/tera/admin/mod.rs @@ -1,25 +1,34 @@ use rocket::{get, routes, Route, State}; use sqlx::SqlitePool; -use crate::{model::log::Log, tera::Config}; +use crate::{ + model::{log::Log, user::AdminUser}, + tera::Config, +}; pub mod boat; pub mod planned_event; pub mod user; #[get("/rss?")] -async fn rss(db: &State, key: Option<&str>, config: &State) -> String { - match key { - Some(key) if key.eq(&config.rss_key) => Log::generate_feed(db).await, - _ => "Not allowed".to_string(), +async fn rss(db: &State, key: &str, config: &State) -> String { + if key.eq(&config.rss_key) { + Log::generate_feed(db).await + } else { + "Not allowed".into() } } +#[get("/rss", rank = 2)] +async fn show_rss(db: &State, _admin: AdminUser) -> String { + Log::show(db).await +} + pub fn routes() -> Vec { let mut ret = Vec::new(); ret.append(&mut user::routes()); ret.append(&mut boat::routes()); ret.append(&mut planned_event::routes()); - ret.append(&mut routes![rss]); + ret.append(&mut routes![rss, show_rss]); ret } diff --git a/src/tera/misc.rs b/src/tera/misc.rs index 24eb431..ef5da18 100644 --- a/src/tera/misc.rs +++ b/src/tera/misc.rs @@ -1,8 +1,7 @@ use rocket::{get, http::ContentType, routes, Route, State}; -use rocket_dyn_templates::{context, Template}; use sqlx::SqlitePool; -use crate::model::{planned_event::PlannedEvent, user::User}; +use crate::model::planned_event::PlannedEvent; #[get("/cal")] async fn cal(db: &State) -> (ContentType, String) { diff --git a/src/tera/stat.rs b/src/tera/stat.rs index 86449a3..e2d23ba 100644 --- a/src/tera/stat.rs +++ b/src/tera/stat.rs @@ -4,7 +4,7 @@ use sqlx::SqlitePool; use crate::model::{ stat::{self, Stat}, - user::{NonGuestUser, User}, + user::NonGuestUser, }; use super::log::KioskCookie; From dcb7bc4c246a3435d0910f68decbb84fd27c1e69 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 28 Oct 2023 14:48:51 +0200 Subject: [PATCH 2/2] clean --- src/tera/misc.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/tera/misc.rs b/src/tera/misc.rs index ef5da18..100890f 100644 --- a/src/tera/misc.rs +++ b/src/tera/misc.rs @@ -15,10 +15,7 @@ pub fn routes() -> Vec { #[cfg(test)] mod test { - use rocket::{ - http::{ContentType, Status}, - local::asynchronous::Client, - }; + use rocket::{http::Status, local::asynchronous::Client}; use sqlx::SqlitePool; use crate::testdb;