Skip to content

Commit

Permalink
editoast: change DeleteBatch signature to use Model Error type
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Valais <[email protected]>
  • Loading branch information
leovalais committed Dec 24, 2024
1 parent 9bef747 commit 588ed82
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ToTokens for DeleteBatchImpl {
async fn delete_batch<I: std::iter::IntoIterator<Item = #ty> + Send + 'async_trait>(
conn: &mut editoast_models::DbConnection,
ids: I,
) -> crate::error::Result<usize> {
) -> std::result::Result<usize, editoast_models::model::Error> {
use #table_mod::dsl;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
Expand Down
20 changes: 20 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4770,6 +4770,7 @@ components:
- $ref: '#/components/schemas/EditoastTowedRollingStockErrorIdNotFound'
- $ref: '#/components/schemas/EditoastTowedRollingStockErrorIsLocked'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorBatchTrainScheduleNotFound'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorDatabase'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorInfraNotFound'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorNotFound'
- $ref: '#/components/schemas/EditoastWorkScheduleErrorDatabase'
Expand Down Expand Up @@ -6264,6 +6265,25 @@ components:
type: string
enum:
- editoast:train_schedule:BatchTrainScheduleNotFound
EditoastTrainScheduleErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:train_schedule:Database
EditoastTrainScheduleErrorInfraNotFound:
type: object
required:
Expand Down
43 changes: 17 additions & 26 deletions editoast/src/models/prelude/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use editoast_models::DbConnection;
use std::result::Result;

use editoast_models::{model, DbConnection};

use crate::error::EditoastError;
use crate::error::Result;

/// Describes how a [Model](super::Model) can be deleted from the database
///
Expand All @@ -12,19 +13,12 @@ pub trait Delete: Sized {
/// Deletes the row corresponding to this model instance
///
/// Returns `true` if the row was deleted, `false` if it didn't exist
async fn delete(
&self,
conn: &mut DbConnection,
) -> std::result::Result<bool, editoast_models::model::Error>;
async fn delete(&self, conn: &mut DbConnection) -> Result<bool, model::Error>;

/// Just like [Delete::delete] but returns `Err(fail())` if the row didn't exist
async fn delete_or_fail<E, F>(
&self,
conn: &mut DbConnection,
fail: F,
) -> std::result::Result<(), E>
async fn delete_or_fail<E, F>(&self, conn: &mut DbConnection, fail: F) -> Result<(), E>
where
E: From<editoast_models::model::Error>,
E: From<model::Error>,
F: FnOnce() -> E + Send + 'async_trait,
{
match self.delete(conn).await {
Expand All @@ -48,19 +42,12 @@ where
for<'async_trait> K: Send + 'async_trait,
{
/// Deletes the row #`id` from the database
async fn delete_static(
conn: &mut DbConnection,
id: K,
) -> std::result::Result<bool, editoast_models::model::Error>;
async fn delete_static(conn: &mut DbConnection, id: K) -> Result<bool, model::Error>;

/// Just like [DeleteStatic::delete_static] but returns `Err(fail())` if the row didn't exist
async fn delete_static_or_fail<E, F>(
conn: &mut DbConnection,
id: K,
fail: F,
) -> std::result::Result<(), E>
async fn delete_static_or_fail<E, F>(conn: &mut DbConnection, id: K, fail: F) -> Result<(), E>
where
E: From<editoast_models::model::Error>,
E: From<model::Error>,
F: FnOnce() -> E + Send + 'async_trait,
{
match Self::delete_static(conn, id).await {
Expand All @@ -86,21 +73,25 @@ where
async fn delete_batch<I: IntoIterator<Item = K> + Send + 'async_trait>(
conn: &mut DbConnection,
ids: I,
) -> Result<usize>;
) -> Result<usize, model::Error>;

/// Just like [DeleteBatch::delete_batch] but returns `Err(fail(missing))` where `missing`
/// is the number of rows that were not deleted
async fn delete_batch_or_fail<I, E, F>(conn: &mut DbConnection, ids: I, fail: F) -> Result<()>
async fn delete_batch_or_fail<I, E, F>(
conn: &mut DbConnection,
ids: I,
fail: F,
) -> Result<(), E>
where
I: Send + IntoIterator<Item = K> + 'async_trait,
E: EditoastError,
E: From<model::Error>,
F: FnOnce(usize) -> E + Send + 'async_trait,
{
let ids = ids.into_iter().collect::<Vec<_>>();
let expected_count = ids.len();
let count = Self::delete_batch(conn, ids).await?;
if count != expected_count {
Err(fail(expected_count - count).into())
Err(fail(expected_count - count))
} else {
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/models/work_schedules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum WorkScheduleType {

#[derive(Debug, Default, Clone, Model, Serialize, Deserialize, ToSchema)]
#[model(table = editoast_models::tables::work_schedule)]
#[model(gen(batch_ops = cd, list))]
#[model(gen(batch_ops = c, list))]
pub struct WorkSchedule {
pub id: i64,
pub start_date_time: DateTime<Utc>,
Expand Down
3 changes: 3 additions & 0 deletions editoast/src/views/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub enum TrainScheduleError {
#[error("Infra '{infra_id}', could not be found")]
#[editoast_error(status = 404)]
InfraNotFound { infra_id: i64 },
#[error(transparent)]
#[editoast_error(status = 500)]
Database(#[from] editoast_models::model::Error),
}

#[derive(IntoParams, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions front/public/locales/en/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"train_schedule": {
"BatchShouldHaveSameTimetable": "Batch should have the same timetable",
"BatchTrainScheduleNotFound": "Some Train Schedules could not be found",
"Database": "Internal error (database)",
"NoSimulation": "No simulation given",
"NoTrainSchedules": "No train schedules given",
"NotFound": "Train Schedule '{{train_schedule_id}}' could not be found",
Expand Down
1 change: 1 addition & 0 deletions front/public/locales/fr/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
"train_schedule": {
"BatchShouldHaveSameTimetable": "Le lot doit avoir une grille horaire identique",
"BatchTrainScheduleNotFound": "Certaines circulations sont introuvables",
"Database": "Erreur interne (base de données)",
"NoSimulation": "Aucune simulation fournie",
"NoTrainSchedules": "Aucune circulation de train fournie",
"NotFound": "Circulation '{{train_schedule_id}}' non trouvée",
Expand Down

0 comments on commit 588ed82

Please sign in to comment.