improvements, styling, additional infos
This commit is contained in:
@ -299,6 +299,50 @@ ORDER BY departure DESC
|
||||
ret
|
||||
}
|
||||
|
||||
pub async fn year_first_logbook_entry(db: &SqlitePool, user: &User) -> Option<i32> {
|
||||
let log: Option<Self> = 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 arrival
|
||||
LIMIT 1
|
||||
", user.id)
|
||||
)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
.unwrap(); //TODO: fixme
|
||||
|
||||
if let Some(log) = log {
|
||||
Some(log.arrival.unwrap().year())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn year_last_logbook_entry(db: &SqlitePool, user: &User) -> Option<i32> {
|
||||
let log: Option<Self> = 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 arrival DESC
|
||||
LIMIT 1
|
||||
", user.id)
|
||||
)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
.unwrap(); //TODO: fixme
|
||||
|
||||
if let Some(log) = log {
|
||||
Some(log.arrival.unwrap().year())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn completed_wanderfahrten_with_user_over_km_in_year(
|
||||
db: &SqlitePool,
|
||||
user: &User,
|
||||
|
@ -1,5 +1,4 @@
|
||||
use serde::Serialize;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[derive(Serialize, PartialEq, Debug)]
|
||||
pub(crate) enum Level {
|
||||
@ -31,7 +30,7 @@ impl Level {
|
||||
} else if km < Level::GOLD.required_km() {
|
||||
Level::GOLD
|
||||
} else if km < Level::DIAMOND.required_km() {
|
||||
Level::BRONZE
|
||||
Level::DIAMOND
|
||||
} else {
|
||||
Level::DONE
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use equatorprice::Level;
|
||||
use serde::Serialize;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use super::{stat::Stat, user::User};
|
||||
use super::{logbook::Logbook, stat::Stat, user::User};
|
||||
|
||||
pub(crate) mod equatorprice;
|
||||
pub(crate) mod rowingbadge;
|
||||
@ -14,6 +14,9 @@ pub(crate) struct Achievements {
|
||||
pub(crate) curr_equatorprice_name: String,
|
||||
pub(crate) new_equatorprice_this_season: bool,
|
||||
pub(crate) rowingbadge: Option<rowingbadge::Status>,
|
||||
pub(crate) all_time_km: i32,
|
||||
pub(crate) year_first_mentioned: Option<i32>,
|
||||
pub(crate) year_last_mentioned: Option<i32>,
|
||||
}
|
||||
|
||||
impl Achievements {
|
||||
@ -31,25 +34,18 @@ impl Achievements {
|
||||
.await
|
||||
.rowed_km
|
||||
};
|
||||
println!(
|
||||
"old: {}; new: {}",
|
||||
rowed_km,
|
||||
rowed_km - rowed_km_this_season
|
||||
);
|
||||
println!(
|
||||
"old: {:?}; new: {:?}",
|
||||
Level::curr_level(rowed_km),
|
||||
Level::curr_level(rowed_km - rowed_km_this_season)
|
||||
);
|
||||
|
||||
let new_equatorprice_this_season =
|
||||
Level::curr_level(rowed_km) != Level::curr_level(rowed_km - rowed_km_this_season);
|
||||
println!("{new_equatorprice_this_season:?}");
|
||||
|
||||
Self {
|
||||
equatorprice: equatorprice::Next::new(rowed_km),
|
||||
curr_equatorprice_name: equatorprice::Level::curr_level(rowed_km).desc().to_string(),
|
||||
new_equatorprice_this_season,
|
||||
rowingbadge: rowingbadge::Status::for_user(db, user).await,
|
||||
all_time_km: rowed_km,
|
||||
year_first_mentioned: Logbook::year_first_logbook_entry(db, user).await,
|
||||
year_last_mentioned: Logbook::year_last_logbook_entry(db, user).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -69,26 +69,29 @@ impl TryFrom<&User> for AgeBracket {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: &User) -> Result<Self, Self::Error> {
|
||||
if let Some(birthdate) = value.birthdate.clone() {
|
||||
let today = Local::now().date_naive();
|
||||
let birthdate = NaiveDate::parse_from_str(&birthdate, "%Y-%m-%d").unwrap();
|
||||
let Some(birthdate) = value.birthdate.clone() else {
|
||||
return Err("User has no birthdate".to_string());
|
||||
};
|
||||
|
||||
let age = today.year() - birthdate.year();
|
||||
if age <= 14 {
|
||||
Ok(AgeBracket::Till14)
|
||||
} else if age <= 18 {
|
||||
Ok(AgeBracket::From14Till18)
|
||||
} else if age <= 30 {
|
||||
Ok(AgeBracket::From19Till30)
|
||||
} else if age <= 60 {
|
||||
Ok(AgeBracket::From31Till60)
|
||||
} else if age <= 75 {
|
||||
Ok(AgeBracket::From61Till75)
|
||||
} else {
|
||||
Ok(AgeBracket::From76)
|
||||
}
|
||||
let Ok(birthdate) = NaiveDate::parse_from_str(&birthdate, "%Y-%m-%d") else {
|
||||
return Err("Birthdate in wrong format...".to_string());
|
||||
};
|
||||
|
||||
let today = Local::now().date_naive();
|
||||
|
||||
let age = today.year() - birthdate.year();
|
||||
if age <= 14 {
|
||||
Ok(AgeBracket::Till14)
|
||||
} else if age <= 18 {
|
||||
Ok(AgeBracket::From14Till18)
|
||||
} else if age <= 30 {
|
||||
Ok(AgeBracket::From19Till30)
|
||||
} else if age <= 60 {
|
||||
Ok(AgeBracket::From31Till60)
|
||||
} else if age <= 75 {
|
||||
Ok(AgeBracket::From61Till75)
|
||||
} else {
|
||||
Err("User has no birthdate".to_string())
|
||||
Ok(AgeBracket::From76)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user