nicer redirect after creating team; Fixes #37
All checks were successful
CI/CD Pipeline / test (push) Successful in 4m10s
CI/CD Pipeline / deploy (push) Successful in 2m47s

This commit is contained in:
Philipp Hofer 2025-04-13 22:36:38 +02:00
parent 75c2bc9bbb
commit 50c5bb6cc3
2 changed files with 24 additions and 13 deletions

View File

@ -117,23 +117,23 @@ impl Team {
.ok()
}
async fn create(db: &SqlitePool, name: &str, route: &Route) -> Result<(), CreateError> {
async fn create(db: &SqlitePool, name: &str, route: &Route) -> Result<i64, CreateError> {
// get next station id which has the lowest amount of teams to have in the first place
// assigned
let Some(station) = route.get_next_first_station(db).await else {
return Err(CreateError::NoStationForRoute);
};
sqlx::query!(
"INSERT INTO team(name, route_id, first_station_id) VALUES (?, ?, ?)",
let result = sqlx::query!(
"INSERT INTO team(name, route_id, first_station_id) VALUES (?, ?, ?) RETURNING id",
name,
route.id,
station.id
)
.execute(db)
.fetch_one(db)
.await
.map_err(|e| CreateError::DuplicateName(e.to_string()))?;
Ok(())
Ok(result.id.unwrap())
}
async fn update_name(&self, db: &SqlitePool, name: &str) {

View File

@ -39,22 +39,33 @@ async fn create(
return Redirect::to("/admin/team");
};
match Team::create(&db, &form.name, &route).await {
Ok(()) => succ!(session, "Team '{}' erfolgreich erstellt!", form.name),
Err(CreateError::DuplicateName(e)) => err!(
let id = match Team::create(&db, &form.name, &route).await {
Ok(id) => {
succ!(session, "Team '{}' erfolgreich erstellt!", form.name);
id
}
Err(CreateError::DuplicateName(e)) => {
err!(
session,
"Team '{}' konnte _NICHT_ erstellt werden, da es bereits ein Team mit diesem Namen gibt ({e})!",
form.name
),
Err(CreateError::NoStationForRoute) => err!(
);
return Redirect::to("/admin/team");
}
Err(CreateError::NoStationForRoute) => {
err!(
session,
"Team '{}' konnte _NICHT_ erstellt werden, da in der angegebenen Route '{}' noch keine Stationen vorkommen",
form.name,
route.name
),
}
);
Redirect::to("/admin/team")
return Redirect::to("/admin/team");
}
};
Redirect::to(&format!("/admin/team/{}", id))
}
async fn delete(