if there is just 1 route -> use that for a newly created station
This commit is contained in:
parent
d033d391b8
commit
d0c836e33f
@ -45,7 +45,11 @@ impl Route {
|
|||||||
.unwrap();
|
.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!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO route_station (route_id, station_id, pos)
|
INSERT INTO route_station (route_id, station_id, pos)
|
||||||
|
@ -80,10 +80,26 @@ impl Station {
|
|||||||
|
|
||||||
pub(crate) async fn create(db: &SqlitePool, name: &str) -> Result<(), String> {
|
pub(crate) async fn create(db: &SqlitePool, name: &str) -> Result<(), String> {
|
||||||
let code = generate_random_alphanumeric(8);
|
let code = generate_random_alphanumeric(8);
|
||||||
sqlx::query!("INSERT INTO station(name, pw) VALUES (?, ?)", name, code)
|
let station_id = sqlx::query!(
|
||||||
.execute(db)
|
"INSERT INTO station(name, pw) VALUES (?, ?) RETURNING id",
|
||||||
.await
|
name,
|
||||||
.map_err(|e| e.to_string())?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user