Skip to content

Commit

Permalink
editoast: change Model trait DeleteStatic to use editoast_models::Error
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 f53725a commit 9bef747
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ToTokens for DeleteStaticImpl {
async fn delete_static(
conn: &mut editoast_models::DbConnection,
#id_ident: #ty,
) -> crate::error::Result<bool> {
) -> std::result::Result<bool, editoast_models::model::Error> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use std::ops::DerefMut;
Expand Down
211 changes: 120 additions & 91 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2327,10 +2327,6 @@ paths:
description: The requested rolling stock was not found
'409':
description: The requested rolling stock is used
content:
application/json:
schema:
$ref: '#/components/schemas/RollingStockError'
patch:
tags:
- rolling_stock
Expand Down Expand Up @@ -4530,6 +4526,25 @@ components:
type: string
enum:
- editoast:delimited_area:InvalidLocations
EditoastDocumentErrorsDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:document:Database
EditoastDocumentErrorsNotFound:
type: object
required:
Expand Down Expand Up @@ -4683,6 +4698,7 @@ components:
- $ref: '#/components/schemas/EditoastCoreErrorUnparsableErrorOutput'
- $ref: '#/components/schemas/EditoastDatabaseAccessErrorDatabaseAccessError'
- $ref: '#/components/schemas/EditoastDelimitedAreaErrorInvalidLocations'
- $ref: '#/components/schemas/EditoastDocumentErrorsDatabase'
- $ref: '#/components/schemas/EditoastDocumentErrorsNotFound'
- $ref: '#/components/schemas/EditoastEditionErrorInfraIsLocked'
- $ref: '#/components/schemas/EditoastEditionErrorSplitTrackSectionBadOffset'
Expand Down Expand Up @@ -4725,11 +4741,13 @@ components:
- $ref: '#/components/schemas/EditoastRollingStockErrorBasePowerClassEmpty'
- $ref: '#/components/schemas/EditoastRollingStockErrorCannotCreateCompoundImage'
- $ref: '#/components/schemas/EditoastRollingStockErrorCannotReadImage'
- $ref: '#/components/schemas/EditoastRollingStockErrorDatabase'
- $ref: '#/components/schemas/EditoastRollingStockErrorIsLocked'
- $ref: '#/components/schemas/EditoastRollingStockErrorIsUsed'
- $ref: '#/components/schemas/EditoastRollingStockErrorKeyNotFound'
- $ref: '#/components/schemas/EditoastRollingStockErrorLiveryMultipartError'
- $ref: '#/components/schemas/EditoastRollingStockErrorNameAlreadyUsed'
- $ref: '#/components/schemas/EditoastScenarioErrorDatabase'
- $ref: '#/components/schemas/EditoastScenarioErrorInfraNotFound'
- $ref: '#/components/schemas/EditoastScenarioErrorNotFound'
- $ref: '#/components/schemas/EditoastScenarioErrorTimetableNotFound'
Expand All @@ -4742,16 +4760,19 @@ components:
- $ref: '#/components/schemas/EditoastStdcmErrorRollingStockNotFound'
- $ref: '#/components/schemas/EditoastStdcmErrorTimetableNotFound'
- $ref: '#/components/schemas/EditoastStdcmErrorTowedRollingStockNotFound'
- $ref: '#/components/schemas/EditoastStudyErrorDatabase'
- $ref: '#/components/schemas/EditoastStudyErrorNotFound'
- $ref: '#/components/schemas/EditoastStudyErrorStartDateAfterEndDate'
- $ref: '#/components/schemas/EditoastTemporarySpeedLimitErrorNameAlreadyUsed'
- $ref: '#/components/schemas/EditoastTimetableErrorDatabase'
- $ref: '#/components/schemas/EditoastTimetableErrorInfraNotFound'
- $ref: '#/components/schemas/EditoastTimetableErrorNotFound'
- $ref: '#/components/schemas/EditoastTowedRollingStockErrorIdNotFound'
- $ref: '#/components/schemas/EditoastTowedRollingStockErrorIsLocked'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorBatchTrainScheduleNotFound'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorInfraNotFound'
- $ref: '#/components/schemas/EditoastTrainScheduleErrorNotFound'
- $ref: '#/components/schemas/EditoastWorkScheduleErrorDatabase'
- $ref: '#/components/schemas/EditoastWorkScheduleErrorNameAlreadyUsed'
- $ref: '#/components/schemas/EditoastWorkScheduleErrorWorkScheduleGroupNotFound'
description: Generated error type for Editoast
Expand Down Expand Up @@ -5579,6 +5600,25 @@ components:
type: string
enum:
- editoast:rollingstocks:CannotReadImage
EditoastRollingStockErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:rollingstocks:Database
EditoastRollingStockErrorIsLocked:
type: object
required:
Expand Down Expand Up @@ -5697,6 +5737,25 @@ components:
type: string
enum:
- editoast:rollingstocks:NameAlreadyUsed
EditoastScenarioErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:scenario:Database
EditoastScenarioErrorInfraNotFound:
type: object
required:
Expand Down Expand Up @@ -5980,6 +6039,25 @@ components:
type: string
enum:
- editoast:stdcm_v2:TowedRollingStockNotFound
EditoastStudyErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:study:Database
EditoastStudyErrorNotFound:
type: object
required:
Expand Down Expand Up @@ -6047,6 +6125,25 @@ components:
type: string
enum:
- editoast:temporary_speed_limit:NameAlreadyUsed
EditoastTimetableErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:timetable:Database
EditoastTimetableErrorInfraNotFound:
type: object
required:
Expand Down Expand Up @@ -6215,6 +6312,25 @@ components:
type: string
enum:
- editoast:train_schedule:NotFound
EditoastWorkScheduleErrorDatabase:
type: object
required:
- type
- status
- message
properties:
context:
type: object
message:
type: string
status:
type: integer
enum:
- 500
type:
type: string
enum:
- editoast:work_schedule:Database
EditoastWorkScheduleErrorNameAlreadyUsed:
type: object
required:
Expand Down Expand Up @@ -9191,68 +9307,6 @@ components:
version:
type: integer
format: int64
RollingStockError:
oneOf:
- type: string
enum:
- CannotReadImage
- type: string
enum:
- CannotCreateCompoundImage
- type: object
required:
- KeyNotFound
properties:
KeyNotFound:
type: object
required:
- rolling_stock_key
properties:
rolling_stock_key:
$ref: '#/components/schemas/RollingStockKey'
- type: object
required:
- NameAlreadyUsed
properties:
NameAlreadyUsed:
type: object
required:
- name
properties:
name:
type: string
- type: object
required:
- IsLocked
properties:
IsLocked:
type: object
required:
- rolling_stock_id
properties:
rolling_stock_id:
type: integer
format: int64
- type: object
required:
- IsUsed
properties:
IsUsed:
type: object
required:
- rolling_stock_id
- usage
properties:
rolling_stock_id:
type: integer
format: int64
usage:
type: array
items:
$ref: '#/components/schemas/ScenarioReference'
- type: string
enum:
- BasePowerClassEmpty
RollingStockForm:
type: object
required:
Expand Down Expand Up @@ -9338,31 +9392,6 @@ components:
format: double
supported_signaling_systems:
$ref: '#/components/schemas/RollingStockSupportedSignalingSystems'
RollingStockKey:
oneOf:
- type: object
required:
- type
- key
properties:
key:
type: integer
format: int64
type:
type: string
enum:
- Id
- type: object
required:
- type
- key
properties:
key:
type: string
type:
type: string
enum:
- Name
RollingStockLivery:
type: object
required:
Expand Down
5 changes: 3 additions & 2 deletions editoast/src/models/infra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl Infra {
}

/// Clear generated data of the infra
/// This function will update `generated_version` acordingly.
/// This function will update `generated_version` accordingly.
pub async fn clear(&mut self, conn: &mut DbConnection) -> Result<bool> {
// TODO: lock self for update
generated_data::clear_all(conn, self.id).await?;
Expand Down Expand Up @@ -301,6 +301,7 @@ impl Infra {
})
})
.await
.map_err(Into::into)
}
}

Expand Down Expand Up @@ -386,7 +387,7 @@ pub mod tests {
}

#[rstest]
// The fixture leaks the persisted infra because we explicitely opened a
// The fixture leaks the persisted infra because we explicitly opened a
// connection. This should be fixed by the testing utils rework. The ignore
// should be removed after.
#[ignore]
Expand Down
17 changes: 12 additions & 5 deletions editoast/src/models/prelude/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ where
for<'async_trait> K: Send + 'async_trait,
{
/// Deletes the row #`id` from the database
async fn delete_static(conn: &mut DbConnection, id: K) -> Result<bool>;
async fn delete_static(
conn: &mut DbConnection,
id: K,
) -> std::result::Result<bool, editoast_models::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) -> Result<()>
async fn delete_static_or_fail<E, F>(
conn: &mut DbConnection,
id: K,
fail: F,
) -> std::result::Result<(), E>
where
E: EditoastError,
E: From<editoast_models::model::Error>,
F: FnOnce() -> E + Send + 'async_trait,
{
match Self::delete_static(conn, id).await {
Ok(true) => Ok(()),
Ok(false) => Err(fail().into()),
Err(e) => Err(e),
Ok(false) => Err(fail()),
Err(e) => Err(E::from(e)),
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions editoast/src/models/timetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ impl Timetable {
impl DeleteStatic<i64> for Timetable {
#[allow(clippy::blocks_in_conditions)] // TODO: Remove this once using clippy 0.1.80
#[tracing::instrument(name = "model:delete_static<Timetable>", skip_all, ret, err)]
async fn delete_static(conn: &mut DbConnection, id: i64) -> Result<bool> {
diesel::delete(dsl::timetable.filter(dsl::id.eq(id)))
async fn delete_static(
conn: &mut DbConnection,
id: i64,
) -> std::result::Result<bool, editoast_models::model::Error> {
let n = diesel::delete(dsl::timetable.filter(dsl::id.eq(id)))
.execute(conn.write().await.deref_mut())
.await
.map(|n| n == 1)
.map_err(Into::into)
.await?;
Ok(n == 1)
}
}

Expand Down
Loading

0 comments on commit 9bef747

Please sign in to comment.