Skip to content

Commit

Permalink
vingo: add recent scans
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 21, 2024
1 parent 5a795ef commit 3c64933
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion vingo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ fn open_routes() -> Router<AppState> {
.route("/auth/callback", get(auth::callback))
.route("/scans", post(scans::add))
.route("/version", get(info::version))
.route("/recent_scans", get(scans::recent))
.route("/seasons", get(seasons::get_until_now))
}

fn authenticated_routes() -> Router<AppState> {
Expand All @@ -118,7 +120,6 @@ fn authenticated_routes() -> Router<AppState> {
)
.route("/scans", get(scans::get_for_current_user))
.route("/leaderboard", get(leaderboard::get))
.route("/seasons", get(seasons::get_until_now))
.route("/settings", get(settings::get).patch(settings::update))
.route_layer(from_fn(middleware::is_logged_in))
}
Expand Down
31 changes: 29 additions & 2 deletions vingo/src/routes/scans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use crate::{
};

use axum::{extract::State, Json};
use chrono::Local;
use chrono::{Local, Duration};
use reqwest::StatusCode;
use serde::{Serialize};
use sea_orm::{
ActiveModelTrait, ColumnTrait, EntityTrait, JoinType::InnerJoin, QueryFilter, QuerySelect,
RelationTrait, Set,
RelationTrait, Set, FromQueryResult, entity::prelude::DateTimeWithTimeZone, Statement, DbBackend,
Value
};
use tower_sessions::Session;

Expand Down Expand Up @@ -102,3 +104,28 @@ pub async fn add(state: State<AppState>, body: String) -> ResponseResult<String>

Ok(user.name)
}

#[derive(Debug, FromQueryResult, Serialize)]
pub struct RecentScanItem {
id: i32,
scan_time: DateTimeWithTimeZone,
}

pub async fn recent(state: State<AppState>) -> ResponseResult<Json<Vec<RecentScanItem>>> {
let fourteen_days_ago = (Local::now() - Duration::days(14)).naive_local();
let scans = RecentScanItem::find_by_statement(Statement::from_sql_and_values(DbBackend::Postgres, "
SELECT DISTINCT ON (s.user_id, DATE(s.scan_time)) s.id, s.scan_time
FROM (
SELECT scan.id, scan.scan_time, \"user\".id as user_id
FROM scan
INNER JOIN card ON card.serial = scan.card_serial
INNER JOIN \"user\" ON \"user\".id = card.user_id
WHERE scan.scan_time > $1
) s
ORDER BY s.user_id, DATE(s.scan_time), s.id;", [Value::ChronoDateTime(Some(Box::new(fourteen_days_ago)))]))
.all(&state.db)
.await
.or_log((StatusCode::INTERNAL_SERVER_ERROR, "could not get all recent scans"))?;

Ok(Json(scans))
}

0 comments on commit 3c64933

Please sign in to comment.