-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
editoast: delete paced train endpoint
Signed-off-by: Youness CHRIFI ALAOUI <[email protected]>
- Loading branch information
1 parent
4babb08
commit 30bb380
Showing
7 changed files
with
354 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use crate::error::Result; | ||
use axum::extract::Json; | ||
use axum::extract::State; | ||
use axum::{response::IntoResponse, Extension}; | ||
use editoast_authz::BuiltinRole; | ||
use editoast_derive::EditoastError; | ||
use editoast_models::DbConnectionPoolV2; | ||
use editoast_schemas::{paced_train::PacedTrainBase, train_schedule::TrainScheduleBase}; | ||
use serde::{Deserialize, Serialize}; | ||
use thiserror::Error; | ||
use utoipa::ToSchema; | ||
|
||
use super::AuthenticationExt; | ||
use crate::views::train_schedule::BatchRequest; | ||
use crate::{ | ||
models::paced_train::{PacedTrain, PacedTrainChangeset}, | ||
views::AuthorizationError, | ||
}; | ||
|
||
crate::routes! { | ||
"/paced_train" => { | ||
delete, | ||
}, | ||
} | ||
|
||
editoast_common::schemas! { | ||
PacedTrainBase, | ||
} | ||
|
||
#[derive(Debug, Error, EditoastError)] | ||
#[editoast_error(base_id = "paced_train")] | ||
pub enum PacedTrainError { | ||
#[error("{number} paced train(s) could not be found")] | ||
#[editoast_error(status = 404)] | ||
BatchPacedTrainNotFound { number: usize }, | ||
#[error(transparent)] | ||
#[editoast_error(status = 500)] | ||
Database(#[from] editoast_models::model::Error), | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, ToSchema)] | ||
pub struct PacedTrainForm { | ||
/// Timetable attached to the train schedule | ||
pub timetable_id: Option<i64>, | ||
#[serde(flatten)] | ||
pub paced_train: PacedTrainBase, | ||
} | ||
|
||
impl From<PacedTrainForm> for PacedTrainChangeset { | ||
fn from( | ||
PacedTrainForm { | ||
timetable_id, | ||
paced_train, | ||
}: PacedTrainForm, | ||
) -> Self { | ||
Self::from(paced_train).flat_timetable_id(timetable_id) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] | ||
pub struct PacedTrainResult { | ||
id: i64, | ||
timetable_id: i64, | ||
#[serde(flatten)] | ||
pub paced_train: PacedTrainBase, | ||
} | ||
|
||
impl From<PacedTrain> for PacedTrainResult { | ||
fn from(value: PacedTrain) -> Self { | ||
Self { | ||
id: value.id, | ||
timetable_id: value.timetable_id, | ||
paced_train: PacedTrainBase { | ||
train_schedule_base: TrainScheduleBase { | ||
train_name: value.train_name, | ||
labels: value.labels.into_iter().flatten().collect(), | ||
rolling_stock_name: value.rolling_stock_name, | ||
start_time: value.start_time, | ||
schedule: value.schedule, | ||
margins: value.margins, | ||
initial_speed: value.initial_speed, | ||
comfort: value.comfort, | ||
path: value.path, | ||
constraint_distribution: value.constraint_distribution, | ||
speed_limit_tag: value.speed_limit_tag.map(Into::into), | ||
power_restrictions: value.power_restrictions, | ||
options: value.options, | ||
}, | ||
duration: value.duration.try_into().unwrap(), | ||
step: value.step.try_into().unwrap(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// Delete a train schedule and its result | ||
#[utoipa::path( | ||
delete, path = "", | ||
tag = "timetable,paced_train", | ||
request_body = inline(BatchRequest), | ||
responses( | ||
(status = 204, description = "All paced_trains have been deleted") | ||
) | ||
)] | ||
async fn delete( | ||
State(db_pool): State<DbConnectionPoolV2>, | ||
Extension(auth): AuthenticationExt, | ||
Json(BatchRequest { | ||
ids: paced_train_ids, | ||
}): Json<BatchRequest>, | ||
) -> Result<impl IntoResponse> { | ||
let authorized = auth | ||
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableWrite].into()) | ||
.await | ||
.map_err(AuthorizationError::AuthError)?; | ||
if !authorized { | ||
return Err(AuthorizationError::Forbidden.into()); | ||
} | ||
|
||
use crate::models::DeleteBatch; | ||
let conn = &mut db_pool.get().await?; | ||
PacedTrain::delete_batch_or_fail(conn, paced_train_ids, |number| { | ||
PacedTrainError::BatchPacedTrainNotFound { number } | ||
}) | ||
.await?; | ||
|
||
Ok(axum::http::StatusCode::NO_CONTENT) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.