extract common functionality to functoin
This commit is contained in:
parent
0c906f2bda
commit
b5fd4018ff
@ -63,6 +63,7 @@ pub enum LogbookCreateError {
|
|||||||
BoatNotFound,
|
BoatNotFound,
|
||||||
TooManyRowers(usize, usize),
|
TooManyRowers(usize, usize),
|
||||||
ShipmasterAlreadyOnWater,
|
ShipmasterAlreadyOnWater,
|
||||||
|
RowerAlreadyOnWater(User),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Logbook {
|
impl Logbook {
|
||||||
@ -146,10 +147,9 @@ impl Logbook {
|
|||||||
return Err(LogbookCreateError::BoatAlreadyOnWater);
|
return Err(LogbookCreateError::BoatAlreadyOnWater);
|
||||||
}
|
}
|
||||||
if (User::find_by_id(db, log.shipmaster as i32).await.unwrap()).on_water(db).await {
|
if (User::find_by_id(db, log.shipmaster as i32).await.unwrap()).on_water(db).await {
|
||||||
return Err(LogbookCreateError::BoatAlreadyOnWater);
|
return Err(LogbookCreateError::ShipmasterAlreadyOnWater);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if log.rower.len() > boat.amount_seats as usize - 1 {
|
if log.rower.len() > boat.amount_seats as usize - 1 {
|
||||||
return Err(LogbookCreateError::TooManyRowers(
|
return Err(LogbookCreateError::TooManyRowers(
|
||||||
boat.amount_seats as usize,
|
boat.amount_seats as usize,
|
||||||
@ -157,6 +157,13 @@ impl Logbook {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for rower in &log.rower {
|
||||||
|
let user = User::find_by_id(db, *rower as i32).await.unwrap();
|
||||||
|
if user.on_water(db).await {
|
||||||
|
return Err(LogbookCreateError::RowerAlreadyOnWater(user));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut tx = db.begin().await.unwrap();
|
let mut tx = db.begin().await.unwrap();
|
||||||
|
|
||||||
let departure = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap();
|
let departure = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap();
|
||||||
|
103
src/tera/log.rs
103
src/tera/log.rs
@ -15,7 +15,7 @@ use tera::Context;
|
|||||||
|
|
||||||
use crate::model::{
|
use crate::model::{
|
||||||
boat::Boat,
|
boat::Boat,
|
||||||
logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError},
|
logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError, LogbookUpdateError},
|
||||||
logtype::LogType,
|
logtype::LogType,
|
||||||
user::{AdminUser, User},
|
user::{AdminUser, User},
|
||||||
};
|
};
|
||||||
@ -34,7 +34,7 @@ impl<'r> FromRequest<'r> for KioskCookie {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/", rank=2)]
|
||||||
async fn index(
|
async fn index(
|
||||||
db: &State<SqlitePool>,
|
db: &State<SqlitePool>,
|
||||||
flash: Option<FlashMessage<'_>>,
|
flash: Option<FlashMessage<'_>>,
|
||||||
@ -71,10 +71,10 @@ fn new_kiosk(cookies: &CookieJar<'_>) -> Redirect {
|
|||||||
let mut cookie = Cookie::new("kiosk", format!("yes"));
|
let mut cookie = Cookie::new("kiosk", format!("yes"));
|
||||||
cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12));
|
cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12));
|
||||||
cookies.add_private(cookie);
|
cookies.add_private(cookie);
|
||||||
Redirect::to("/log/kiosk")
|
Redirect::to("/log")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/kiosk")]
|
#[get("/")]
|
||||||
async fn kiosk(
|
async fn kiosk(
|
||||||
db: &State<SqlitePool>,
|
db: &State<SqlitePool>,
|
||||||
flash: Option<FlashMessage<'_>>,
|
flash: Option<FlashMessage<'_>>,
|
||||||
@ -105,12 +105,7 @@ async fn kiosk(
|
|||||||
Template::render("kiosk", context.into_json())
|
Template::render("kiosk", context.into_json())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/", data = "<data>", rank = 2)]
|
async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect>{
|
||||||
async fn create(
|
|
||||||
db: &State<SqlitePool>,
|
|
||||||
data: Form<LogToAdd>,
|
|
||||||
_adminuser: AdminUser,
|
|
||||||
) -> Flash<Redirect> {
|
|
||||||
match Logbook::create(
|
match Logbook::create(
|
||||||
db,
|
db,
|
||||||
data.into_inner()
|
data.into_inner()
|
||||||
@ -120,28 +115,47 @@ async fn create(
|
|||||||
Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"),
|
Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"),
|
||||||
Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Boot schon am Wasser")),
|
Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Boot schon am Wasser")),
|
||||||
Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Schiffsführer schon am Wasser")),
|
Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Schiffsführer schon am Wasser")),
|
||||||
|
Err(LogbookCreateError::RowerAlreadyOnWater(rower)) => Flash::error(Redirect::to("/log"), format!("Ruderer {} schon am Wasser", rower.name)),
|
||||||
Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log"), format!("Boot gesperrt")),
|
Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log"), format!("Boot gesperrt")),
|
||||||
Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log"), format!("Boot gibt's ned")),
|
Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log"), format!("Boot gibt's ned")),
|
||||||
Err(LogbookCreateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")),
|
Err(LogbookCreateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")),
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/", data = "<data>", rank = 2)]
|
||||||
|
async fn create(
|
||||||
|
db: &State<SqlitePool>,
|
||||||
|
data: Form<LogToAdd>,
|
||||||
|
_adminuser: AdminUser,
|
||||||
|
) -> Flash<Redirect> {
|
||||||
|
create_logbook(db, data).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/", data = "<data>")]
|
#[post("/", data = "<data>")]
|
||||||
async fn create_kiosk(db: &State<SqlitePool>, data: Form<LogToAdd>) -> Flash<Redirect> {
|
async fn create_kiosk(db: &State<SqlitePool>, data: Form<LogToAdd>, _kiosk: KioskCookie) -> Flash<Redirect> {
|
||||||
match Logbook::create(
|
create_logbook(db, data).await
|
||||||
db,
|
}
|
||||||
data.into_inner()
|
|
||||||
)
|
async fn home_logbook(db: &SqlitePool, data: Form<LogToFinalize>, logbook_id: i32, user: &User) -> Flash<Redirect>{
|
||||||
.await
|
let logbook: Option<Logbook> = Logbook::find_by_id(db, logbook_id).await;
|
||||||
{
|
let Some(logbook) = logbook else {
|
||||||
Ok(_) => Flash::success(Redirect::to("/log/kiosk"), "Ausfahrt erfolgreich hinzugefügt"),
|
return Flash::error(
|
||||||
Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot schon am Wasser")),
|
Redirect::to("/admin/log"),
|
||||||
Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Schiffsführer schon am Wasser")),
|
format!("Log with ID {} does not exist!", logbook_id),
|
||||||
Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot gesperrt")),
|
)
|
||||||
Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot gibt's ned")),
|
};
|
||||||
Err(LogbookCreateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log/kiosk"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")),
|
|
||||||
|
match logbook.home(db, user, data.into_inner()).await {
|
||||||
|
Ok(_) => Flash::success(Redirect::to("/log"), "Successfully updated log"),
|
||||||
|
Err(LogbookUpdateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")),
|
||||||
|
Err(_) => Flash::error(
|
||||||
|
Redirect::to("/log"),
|
||||||
|
format!("Logbook with ID {} could not be updated!", logbook_id),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<logbook_id>", data = "<data>")]
|
#[post("/<logbook_id>", data = "<data>")]
|
||||||
@ -149,31 +163,10 @@ async fn home_kiosk(
|
|||||||
db: &State<SqlitePool>,
|
db: &State<SqlitePool>,
|
||||||
data: Form<LogToFinalize>,
|
data: Form<LogToFinalize>,
|
||||||
logbook_id: i32,
|
logbook_id: i32,
|
||||||
|
_kiosk: KioskCookie
|
||||||
) -> Flash<Redirect> {
|
) -> Flash<Redirect> {
|
||||||
let logbook = Logbook::find_by_id(db, logbook_id).await;
|
let logbook = Logbook::find_by_id(db, logbook_id).await.unwrap(); //TODO: fixme
|
||||||
let Some(logbook) = logbook else {
|
home_logbook(db, data, logbook_id, &User::find_by_id(db, logbook.shipmaster as i32).await.unwrap()).await
|
||||||
return Flash::error(
|
|
||||||
Redirect::to("/log/kiosk"),
|
|
||||||
format!("Log with ID {} does not exist!", logbook_id),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
match logbook
|
|
||||||
.home(
|
|
||||||
db,
|
|
||||||
&User::find_by_id(db, logbook.shipmaster as i32)
|
|
||||||
.await
|
|
||||||
.unwrap(),
|
|
||||||
data.into_inner(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => Flash::success(Redirect::to("/log/kiosk"), "Successfully updated log"),
|
|
||||||
Err(_) => Flash::error(
|
|
||||||
Redirect::to("/log/kiosk"),
|
|
||||||
format!("Logbook with ID {} could not be updated!", logbook_id),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<logbook_id>", data = "<data>", rank = 2)]
|
#[post("/<logbook_id>", data = "<data>", rank = 2)]
|
||||||
@ -183,21 +176,7 @@ async fn home(
|
|||||||
logbook_id: i32,
|
logbook_id: i32,
|
||||||
adminuser: AdminUser,
|
adminuser: AdminUser,
|
||||||
) -> Flash<Redirect> {
|
) -> Flash<Redirect> {
|
||||||
let logbook = Logbook::find_by_id(db, logbook_id).await;
|
home_logbook(db, data, logbook_id, &adminuser.user).await
|
||||||
let Some(logbook) = logbook else {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to("/admin/log"),
|
|
||||||
format!("Log with ID {} does not exist!", logbook_id),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
match logbook.home(db, &adminuser.user, data.into_inner()).await {
|
|
||||||
Ok(_) => Flash::success(Redirect::to("/log"), "Successfully updated log"),
|
|
||||||
Err(_) => Flash::error(
|
|
||||||
Redirect::to("/log"),
|
|
||||||
format!("Logbook with ID {} could not be updated!", logbook_id),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
{% extends "base" %}
|
{% extends "base" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
|
{% if flash %}
|
||||||
|
{{ macros::alert(message=flash.1, type=flash.0, class="sm:col-span-2 lg:col-span-3") }}
|
||||||
|
{% endif %}
|
||||||
<div class="max-w-screen-lg w-full">
|
<div class="max-w-screen-lg w-full">
|
||||||
<h1 class="h1">Logbuch</h1>
|
<h1 class="h1">Logbuch</h1>
|
||||||
<h2>Neue Ausfahrt starten</h2>
|
<h2>Neue Ausfahrt starten</h2>
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
{% extends "base" %}
|
{% extends "base" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
{% if flash %}
|
||||||
|
{{ macros::alert(message=flash.1, type=flash.0, class="sm:col-span-2 lg:col-span-3") }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="max-w-screen-lg w-full">
|
<div class="max-w-screen-lg w-full">
|
||||||
<h1 class="h1">Logbuch</h1>
|
<h1 class="h1">Logbuch</h1>
|
||||||
<h2>Neue Ausfahrt starten</h2>
|
<h2>Neue Ausfahrt starten</h2>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user