Skip to content

Commit

Permalink
fixup! editoast: work-schedules: add endpoints for easier edition and…
Browse files Browse the repository at this point in the history
… viewing
  • Loading branch information
eckter committed Nov 21, 2024
1 parent 2c360cb commit f937a33
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions editoast/src/views/work_schedules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::views::path::projection::PathProjection;
use crate::views::AuthenticationExt;
use crate::views::AuthorizationError;
use crate::AppState;
use axum::extract::Json;
use axum::extract::Path;
use axum::extract::State;
use axum::extract::{Json, Path};
use axum::response::IntoResponse;
use axum::Extension;
use chrono::DateTime;
Expand Down Expand Up @@ -369,29 +370,35 @@ async fn delete_group(
let conn = &mut db_pool.get().await?;
let group_id = raw_group_id.id;

// Check that the group exists
let work_schedule_group = WorkScheduleGroup::retrieve_or_fail(conn, group_id, || {
WorkScheduleError::WorkScheduleGroupNotFound { id: group_id }
})
.await?;
conn.transaction(|conn| {
Box::pin(async move {
// Check that the group exists
let work_schedule_group =
WorkScheduleGroup::retrieve_or_fail(&mut conn.clone(), group_id, || {
WorkScheduleError::WorkScheduleGroupNotFound { id: group_id }
})
.await?;

// List work schedules to be deleted
let selection_setting =
SelectionSettings::new().filter(move || WorkSchedule::WORK_SCHEDULE_GROUP_ID.eq(group_id));
let work_schedule_ids = WorkSchedule::list(conn, selection_setting)
.await?
.into_iter()
.map(|work_schedule| work_schedule.id);
// List work schedules to be deleted
let selection_setting = SelectionSettings::new()
.filter(move || WorkSchedule::WORK_SCHEDULE_GROUP_ID.eq(group_id));
let work_schedule_ids = WorkSchedule::list(&mut conn.clone(), selection_setting)
.await?
.into_iter()
.map(|work_schedule| work_schedule.id);

// Delete them
use crate::models::DeleteBatch;
let conn = &mut db_pool.get().await?;
WorkSchedule::delete_batch(conn, work_schedule_ids).await?;
// Delete them
use crate::models::DeleteBatch;
let conn = &mut db_pool.get().await?;
WorkSchedule::delete_batch(conn, work_schedule_ids).await?;

// Delete the group itself
WorkScheduleGroup::delete(&work_schedule_group, conn).await?;
// Delete the group itself
WorkScheduleGroup::delete(&work_schedule_group, conn).await?;

Ok(axum::http::StatusCode::NO_CONTENT)
Ok(axum::http::StatusCode::NO_CONTENT)
})
})
.await
}

#[utoipa::path(
Expand Down Expand Up @@ -459,20 +466,26 @@ async fn put_in_group(
let db_pool = app_state.db_pool_v2.clone();
let conn = &mut db_pool.get().await?;

// Check that the group exists
WorkScheduleGroup::retrieve_or_fail(conn, group_id, || {
WorkScheduleError::WorkScheduleGroupNotFound { id: group_id }
})
.await?;
conn.transaction(|conn| {
Box::pin(async move {
// Check that the group exists
WorkScheduleGroup::retrieve_or_fail(&mut conn.clone(), group_id, || {
WorkScheduleError::WorkScheduleGroupNotFound { id: group_id }
})
.await?;

// Create work schedules
let work_schedules_changesets = work_schedules
.into_iter()
.map(|work_schedule| work_schedule.into_work_schedule_changeset(group_id))
.collect::<Vec<_>>();
let work_schedules = WorkSchedule::create_batch(conn, work_schedules_changesets).await?;
// Create work schedules
let work_schedules_changesets = work_schedules
.into_iter()
.map(|work_schedule| work_schedule.into_work_schedule_changeset(group_id))
.collect::<Vec<_>>();
let work_schedules =
WorkSchedule::create_batch(&mut conn.clone(), work_schedules_changesets).await?;

Ok(Json(work_schedules))
Ok(Json(work_schedules))
})
})
.await
}

#[utoipa::path(
Expand Down

0 comments on commit f937a33

Please sign in to comment.