From 1b9c96dcdd861d34386c5c193afb47e7e1870298 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 4 Mar 2024 22:27:54 +0100 Subject: [PATCH] show scheckbuch user their previous trips --- src/model/logbook.rs | 32 ++++++++++++++++++++++++++++++++ src/model/user.rs | 22 ---------------------- src/tera/planned.rs | 6 ++++++ templates/planned.html.tera | 19 +++++++++++++++++++ 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index b3e4253..805c4ae 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -226,6 +226,38 @@ ORDER BY departure DESC ret } + pub async fn completed_with_user( + db: &SqlitePool, + user: &User, + ) -> Vec { + let logs = sqlx::query_as( + &format!(" + SELECT id, boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype + FROM logbook + JOIN rower ON logbook.id = rower.logbook_id + WHERE arrival is not null AND rower_id = {} + ORDER BY departure DESC + ", user.id) + ) + .fetch_all(db) + .await + .unwrap(); //TODO: fixme + + let mut ret = Vec::new(); + for log in logs { + ret.push(LogbookWithBoatAndRowers { + rowers: Rower::for_log(db, &log).await, + boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), + shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), + steering_user: User::find_by_id(db, log.steering_person as i32) + .await + .unwrap(), + logbook: log, + }); + } + ret + } + pub async fn completed(db: &SqlitePool) -> Vec { let year = chrono::Utc::now().year(); let logs = sqlx::query_as( diff --git a/src/model/user.rs b/src/model/user.rs index ba7a4b7..3806430 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -225,28 +225,6 @@ impl User { .count } - pub async fn rowed_km(&self, db: &SqlitePool) -> i32 { - sqlx::query!( - "SELECT COALESCE(SUM(distance_in_km),0) as rowed_km - FROM ( - SELECT distance_in_km - FROM logbook - WHERE shipmaster = ?1 - UNION - SELECT l.distance_in_km - FROM logbook l - INNER JOIN rower r ON r.logbook_id = l.id - WHERE r.rower_id = ?1 - - );", - self.id, - ) - .fetch_one(db) - .await - .unwrap() - .rowed_km - } - pub async fn has_role(&self, db: &SqlitePool, role: &str) -> bool { if sqlx::query!( "SELECT * FROM user_role WHERE user_id=? AND role_id = (SELECT id FROM role WHERE name = ?)", diff --git a/src/tera/planned.rs b/src/tera/planned.rs index 4268e3a..468bd05 100644 --- a/src/tera/planned.rs +++ b/src/tera/planned.rs @@ -10,6 +10,7 @@ use tera::Context; use crate::model::{ log::Log, + logbook::Logbook, tripdetails::TripDetails, triptype::TripType, user::{AllowedForPlannedTripsUser, User, UserWithRoles}, @@ -31,6 +32,11 @@ async fn index( context.insert("trip_types", &triptypes); } + if user.has_role(db, "scheckbuch").await { + let last_trips = Logbook::completed_with_user(db, &user).await; + context.insert("last_trips", &last_trips); + } + let days = user.get_days(db).await; if let Some(msg) = flash { diff --git a/templates/planned.html.tera b/templates/planned.html.tera index 4d283df..8431e55 100644 --- a/templates/planned.html.tera +++ b/templates/planned.html.tera @@ -1,8 +1,27 @@ {% import "includes/macros" as macros %} +{% import "includes/forms/log" as log %} {% extends "base" %} {% block content %}
{% if flash %}{{ macros::alert(message=flash.1, type=flash.0, class="sm:col-span-2 lg:col-span-3") }}{% endif %} + {% if "scheckbuch" in loggedin_user.roles %} +
+ +
+ {% endif %} {% if "paid" not in loggedin_user.roles and "Donau Linz" in loggedin_user.roles %}