Skip to content

Commit dbeed01

Browse files
committed
fixup! fixup! editoast: work-schedules: add endpoints for easier edition and viewing
1 parent 61426f6 commit dbeed01

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

editoast/src/views/operational_studies.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::models::prelude::*;
2+
use crate::models::work_schedules::WorkSchedule;
23
use crate::models::Project;
34
use crate::models::Scenario;
45
use crate::models::Study;
@@ -58,4 +59,12 @@ impl Ordering {
5859
Ordering::LastModifiedDesc => Scenario::LAST_MODIFICATION.desc(),
5960
}
6061
}
62+
63+
pub fn as_work_schedule_ordering(&self) -> SortSetting<WorkSchedule> {
64+
match *self {
65+
Ordering::NameAsc => WorkSchedule::OBJ_ID.asc(),
66+
Ordering::NameDesc => WorkSchedule::OBJ_ID.desc(),
67+
_ => WorkSchedule::OBJ_ID.asc(),
68+
}
69+
}
6170
}

editoast/src/views/work_schedules.rs

+45-10
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ use crate::models::prelude::*;
55
use crate::models::work_schedules::WorkSchedule;
66
use crate::models::work_schedules::WorkScheduleGroup;
77
use crate::models::work_schedules::WorkScheduleType;
8+
use crate::views::operational_studies::Ordering;
9+
use crate::views::pagination::PaginationQueryParam;
10+
use crate::views::pagination::PaginationStats;
811
use crate::views::path::projection::Intersection;
912
use crate::views::path::projection::PathProjection;
1013
use crate::views::AuthenticationExt;
1114
use crate::views::AuthorizationError;
1215
use axum::extract::Json;
1316
use axum::extract::Path;
17+
use axum::extract::Query;
1418
use axum::extract::State;
1519
use axum::response::IntoResponse;
1620
use axum::Extension;
@@ -20,7 +24,8 @@ use derivative::Derivative;
2024
use editoast_authz::BuiltinRole;
2125
use editoast_derive::EditoastError;
2226
use editoast_models::DbConnectionPoolV2;
23-
use editoast_schemas::infra::{Direction, TrackRange};
27+
use editoast_schemas::infra::Direction;
28+
use editoast_schemas::infra::TrackRange;
2429
use serde::de::Error as SerdeError;
2530
use serde::Deserialize;
2631
use serde::Serialize;
@@ -29,6 +34,8 @@ use thiserror::Error;
2934
use utoipa::{IntoParams, ToSchema};
3035
use uuid::Uuid;
3136

37+
use super::pagination::PaginatedList;
38+
3239
crate::routes! {
3340
"/work_schedules" => {
3441
create,
@@ -469,20 +476,38 @@ async fn put_in_group(
469476
.await
470477
}
471478

479+
#[derive(Serialize, ToSchema)]
480+
#[cfg_attr(test, derive(Deserialize))]
481+
struct GroupContentResponse {
482+
#[schema(value_type = Vec<WorkSchedule>)]
483+
results: Vec<WorkSchedule>,
484+
#[serde(flatten)]
485+
stats: PaginationStats,
486+
}
487+
488+
#[derive(Debug, Clone, serde::Deserialize, utoipa::IntoParams)]
489+
#[into_params(parameter_in = Query)]
490+
pub struct WorkScheduleOrderingParam {
491+
#[serde(default)]
492+
pub ordering: Ordering,
493+
}
494+
472495
#[utoipa::path(
473496
get, path = "",
474497
tag = "work_schedules",
475-
params(WorkScheduleGroupIdParam),
498+
params(PaginationQueryParam, WorkScheduleGroupIdParam, WorkScheduleOrderingParam),
476499
responses(
477-
(status = 200, description = "The work schedules in the group", body = Vec<WorkSchedule>),
500+
(status = 200, description = "The work schedules in the group", body = inline(GroupContentResponse)),
478501
(status = 404, description = "Work schedule group not found"),
479502
)
480503
)]
481504
async fn get_group(
482505
State(db_pool): State<DbConnectionPoolV2>,
483506
Extension(auth): AuthenticationExt,
484507
Path(WorkScheduleGroupIdParam { id: group_id }): Path<WorkScheduleGroupIdParam>,
485-
) -> Result<Json<Vec<WorkSchedule>>> {
508+
Query(pagination_params): Query<PaginationQueryParam>,
509+
Query(ordering_params): Query<WorkScheduleOrderingParam>,
510+
) -> Result<Json<GroupContentResponse>> {
486511
let authorized = auth
487512
.check_roles([BuiltinRole::WorkScheduleRead].into())
488513
.await
@@ -491,6 +516,14 @@ async fn get_group(
491516
return Err(AuthorizationError::Unauthorized.into());
492517
}
493518

519+
let ordering = ordering_params.ordering;
520+
let settings = pagination_params
521+
.validate(1000)?
522+
.warn_page_size(100)
523+
.into_selection_settings()
524+
.filter(move || WorkSchedule::WORK_SCHEDULE_GROUP_ID.eq(group_id))
525+
.order_by(move || ordering.as_work_schedule_ordering());
526+
494527
let conn = &mut db_pool.get().await?;
495528

496529
// Check that the group exists
@@ -499,11 +532,12 @@ async fn get_group(
499532
})
500533
.await?;
501534

502-
let selection_setting =
503-
SelectionSettings::new().filter(move || WorkSchedule::WORK_SCHEDULE_GROUP_ID.eq(group_id));
504-
let work_schedules = WorkSchedule::list(conn, selection_setting).await?;
535+
let (work_schedules, stats) = WorkSchedule::list_paginated(conn, settings).await?;
505536

506-
Ok(Json(work_schedules))
537+
Ok(Json(GroupContentResponse {
538+
results: work_schedules,
539+
stats,
540+
}))
507541
}
508542

509543
#[cfg(test)]
@@ -769,10 +803,11 @@ pub mod tests {
769803

770804
// Get the content of the group
771805
let request = app.get(&work_schedule_url);
772-
let work_schedules = app
806+
let response = app
773807
.fetch(request)
774808
.assert_status(StatusCode::OK)
775-
.json_into::<Vec<WorkSchedule>>();
809+
.json_into::<GroupContentResponse>();
810+
let work_schedules = response.results;
776811
assert_eq!(1, work_schedules.len());
777812
assert_eq!(ref_obj_id, work_schedules[0].obj_id);
778813

0 commit comments

Comments
 (0)