extract common functionality to functoin
This commit is contained in:
parent
0c906f2bda
commit
b5fd4018ff
@ -63,6 +63,7 @@ pub enum LogbookCreateError {
|
||||
BoatNotFound,
|
||||
TooManyRowers(usize, usize),
|
||||
ShipmasterAlreadyOnWater,
|
||||
RowerAlreadyOnWater(User),
|
||||
}
|
||||
|
||||
impl Logbook {
|
||||
@ -146,10 +147,9 @@ impl Logbook {
|
||||
return Err(LogbookCreateError::BoatAlreadyOnWater);
|
||||
}
|
||||
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 {
|
||||
return Err(LogbookCreateError::TooManyRowers(
|
||||
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 departure = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap();
|
||||
|
101
src/tera/log.rs
101
src/tera/log.rs
@ -15,7 +15,7 @@ use tera::Context;
|
||||
|
||||
use crate::model::{
|
||||
boat::Boat,
|
||||
logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError},
|
||||
logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError, LogbookUpdateError},
|
||||
logtype::LogType,
|
||||
user::{AdminUser, User},
|
||||
};
|
||||
@ -34,7 +34,7 @@ impl<'r> FromRequest<'r> for KioskCookie {
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
#[get("/", rank=2)]
|
||||
async fn index(
|
||||
db: &State<SqlitePool>,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
@ -71,10 +71,10 @@ fn new_kiosk(cookies: &CookieJar<'_>) -> Redirect {
|
||||
let mut cookie = Cookie::new("kiosk", format!("yes"));
|
||||
cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12));
|
||||
cookies.add_private(cookie);
|
||||
Redirect::to("/log/kiosk")
|
||||
Redirect::to("/log")
|
||||
}
|
||||
|
||||
#[get("/kiosk")]
|
||||
#[get("/")]
|
||||
async fn kiosk(
|
||||
db: &State<SqlitePool>,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
@ -105,12 +105,7 @@ async fn kiosk(
|
||||
Template::render("kiosk", context.into_json())
|
||||
}
|
||||
|
||||
#[post("/", data = "<data>", rank = 2)]
|
||||
async fn create(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<LogToAdd>,
|
||||
_adminuser: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect>{
|
||||
match Logbook::create(
|
||||
db,
|
||||
data.into_inner()
|
||||
@ -120,28 +115,47 @@ async fn create(
|
||||
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::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::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)")),
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[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>")]
|
||||
async fn create_kiosk(db: &State<SqlitePool>, data: Form<LogToAdd>) -> Flash<Redirect> {
|
||||
match Logbook::create(
|
||||
db,
|
||||
data.into_inner()
|
||||
async fn create_kiosk(db: &State<SqlitePool>, data: Form<LogToAdd>, _kiosk: KioskCookie) -> Flash<Redirect> {
|
||||
create_logbook(db, data).await
|
||||
}
|
||||
|
||||
async fn home_logbook(db: &SqlitePool, data: Form<LogToFinalize>, logbook_id: i32, user: &User) -> Flash<Redirect>{
|
||||
let logbook: Option<Logbook> = Logbook::find_by_id(db, logbook_id).await;
|
||||
let Some(logbook) = logbook else {
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/log"),
|
||||
format!("Log with ID {} does not exist!", logbook_id),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Flash::success(Redirect::to("/log/kiosk"), "Ausfahrt erfolgreich hinzugefügt"),
|
||||
Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot schon am Wasser")),
|
||||
Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Schiffsführer schon am Wasser")),
|
||||
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>")]
|
||||
@ -149,31 +163,10 @@ async fn home_kiosk(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<LogToFinalize>,
|
||||
logbook_id: i32,
|
||||
_kiosk: KioskCookie
|
||||
) -> Flash<Redirect> {
|
||||
let logbook = Logbook::find_by_id(db, logbook_id).await;
|
||||
let Some(logbook) = logbook else {
|
||||
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),
|
||||
),
|
||||
}
|
||||
let logbook = Logbook::find_by_id(db, logbook_id).await.unwrap(); //TODO: fixme
|
||||
home_logbook(db, data, logbook_id, &User::find_by_id(db, logbook.shipmaster as i32).await.unwrap()).await
|
||||
}
|
||||
|
||||
#[post("/<logbook_id>", data = "<data>", rank = 2)]
|
||||
@ -183,21 +176,7 @@ async fn home(
|
||||
logbook_id: i32,
|
||||
adminuser: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
let logbook = Logbook::find_by_id(db, logbook_id).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),
|
||||
),
|
||||
}
|
||||
home_logbook(db, data, logbook_id, &adminuser.user).await
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
|
@ -4,6 +4,10 @@
|
||||
{% extends "base" %}
|
||||
|
||||
{% 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">
|
||||
<h1 class="h1">Logbuch</h1>
|
||||
<h2>Neue Ausfahrt starten</h2>
|
||||
|
@ -4,6 +4,10 @@
|
||||
{% extends "base" %}
|
||||
|
||||
{% 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">
|
||||
<h1 class="h1">Logbuch</h1>
|
||||
<h2>Neue Ausfahrt starten</h2>
|
||||
|
Loading…
x
Reference in New Issue
Block a user