if there is just 1 route -> use that for a newly created station
All checks were successful
CI/CD Pipeline / test (push) Successful in 6m51s
CI/CD Pipeline / deploy (push) Successful in 3m54s

This commit is contained in:
Philipp Hofer 2025-04-18 19:55:46 +02:00
parent d033d391b8
commit d0c836e33f
2 changed files with 25 additions and 5 deletions

View File

@ -45,7 +45,11 @@ impl Route {
.unwrap();
}
async fn add_station(&self, db: &SqlitePool, station: &Station) -> Result<(), String> {
pub(crate) async fn add_station(
&self,
db: &SqlitePool,
station: &Station,
) -> Result<(), String> {
sqlx::query!(
r#"
INSERT INTO route_station (route_id, station_id, pos)

View File

@ -80,10 +80,26 @@ impl Station {
pub(crate) async fn create(db: &SqlitePool, name: &str) -> Result<(), String> {
let code = generate_random_alphanumeric(8);
sqlx::query!("INSERT INTO station(name, pw) VALUES (?, ?)", name, code)
.execute(db)
let station_id = sqlx::query!(
"INSERT INTO station(name, pw) VALUES (?, ?) RETURNING id",
name,
code
)
.fetch_one(db)
.await
.map_err(|e| e.to_string())?;
let mut routes = Route::all(db).await.into_iter();
if let Some(route) = routes.next() {
if routes.next().is_none() {
// Just one route exists -> use it for new station
let station = Station::find_by_id(db, station_id.id)
.await
.expect("just created");
route.add_station(db, &station).await?;
}
}
Ok(())
}