Skip to content

Commit

Permalink
editoast: delete paced train endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Youness CHRIFI ALAOUI <[email protected]>
  • Loading branch information
younesschrifi committed Feb 20, 2025
1 parent 4babb08 commit 30bb380
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ CREATE TABLE paced_train (
speed_limit_tag varchar(128),
power_restrictions jsonb NOT NULL,
options jsonb NOT NULL,
duration timestamptz NOT NULL,
step timestamptz NOT NULL
duration interval NOT NULL,
step interval NOT NULL
);
126 changes: 126 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,30 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/LightRollingStockWithLiveries'
/paced_train:
delete:
tags:
- timetable
- paced_train
summary: Delete a train schedule and its result
requestBody:
content:
application/json:
schema:
type: object
required:
- ids
properties:
ids:
type: array
items:
type: integer
format: int64
uniqueItems: true
required: true
responses:
'204':
description: All paced_trains have been deleted
/projects:
get:
tags:
Expand Down Expand Up @@ -2794,6 +2818,37 @@ paths:
type: array
items:
$ref: '#/components/schemas/Conflict'
/timetable/{id}/paced_trains:
post:
tags:
- timetable
- paced_train
summary: Create paced trains by batch
parameters:
- name: id
in: path
description: A timetable ID
required: true
schema:
type: integer
format: int64
requestBody:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/PacedTrainBase'
required: true
responses:
'200':
description: The created paced trains
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/PacedTrainResult'
/timetable/{id}/stdcm:
post:
tags:
Expand Down Expand Up @@ -4853,6 +4908,8 @@ components:
- $ref: '#/components/schemas/EditoastOperationErrorInvalidPatch'
- $ref: '#/components/schemas/EditoastOperationErrorModifyId'
- $ref: '#/components/schemas/EditoastOperationErrorObjectNotFound'
- $ref: '#/components/schemas/EditoastPacedTrainErrorBatchPacedTrainNotFound'
- $ref: '#/components/schemas/EditoastPacedTrainErrorDatabase'
- $ref: '#/components/schemas/EditoastPaginationErrorInvalidPage'
- $ref: '#/components/schemas/EditoastPaginationErrorInvalidPageSize'
- $ref: '#/components/schemas/EditoastPathfindingErrorInfraNotFound'
Expand Down Expand Up @@ -5461,6 +5518,49 @@ components:
type: string
enum:
- editoast:operation:ObjectNotFound
EditoastPacedTrainErrorBatchPacedTrainNotFound:
type: object
required:
- type
- status
- message
properties:
context:
type: object
required:
- number
properties:
number:
type: integer
message:
type: string
status:
type: integer
enum:
- 404
type:
type: string
enum:
- editoast:paced_train:BatchPacedTrainNotFound
EditoastPacedTrainErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:paced_train:Database
EditoastPaginationErrorInvalidPage:
type: object
required:
Expand Down Expand Up @@ -8298,6 +8398,32 @@ components:
- CreationDateDesc
- LastModifiedDesc
- LastModifiedAsc
PacedTrainBase:
allOf:
- $ref: '#/components/schemas/TrainScheduleBase'
- type: object
required:
- duration
- step
properties:
duration:
type: string
step:
type: string
PacedTrainResult:
allOf:
- $ref: '#/components/schemas/PacedTrainBase'
- type: object
required:
- id
- timetable_id
properties:
id:
type: integer
format: int64
timetable_id:
type: integer
format: int64
PaginationStats:
type: object
description: |-
Expand Down
2 changes: 2 additions & 0 deletions editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ crate::routes! {
&fonts,
&infra,
&layers,
&paced_train,
&projects,
&rolling_stock,
&search,
Expand Down Expand Up @@ -135,6 +136,7 @@ editoast_common::schemas! {
infra::schemas(),
operation::schemas(),
operational_studies::schemas(),
paced_train::schemas(),
pagination::schemas(),
path::schemas(),
projects::schemas(),
Expand Down
128 changes: 128 additions & 0 deletions editoast/src/views/paced_train.rs
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)
}
4 changes: 4 additions & 0 deletions front/public/locales/fr/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@
"UnsimulatedTrainSchedule": "La circulation '{{train_schedule_id}}' n'est pas simulée",
"InfraNotFound": "Infrastructure '{{infra_id}}' non trouvée"
},
"paced_train": {
"Database": "Erreur interne (base de données)",
"BatchPacedTrainNotFound": "Certaines missions sont introuvables"
},
"url": {
"InvalidUrl": "Url invalide '{{url}}'"
},
Expand Down
Loading

0 comments on commit 30bb380

Please sign in to comment.