@@ -5,12 +5,16 @@ use crate::models::prelude::*;
5
5
use crate :: models:: work_schedules:: WorkSchedule ;
6
6
use crate :: models:: work_schedules:: WorkScheduleGroup ;
7
7
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 ;
8
11
use crate :: views:: path:: projection:: Intersection ;
9
12
use crate :: views:: path:: projection:: PathProjection ;
10
13
use crate :: views:: AuthenticationExt ;
11
14
use crate :: views:: AuthorizationError ;
12
15
use axum:: extract:: Json ;
13
16
use axum:: extract:: Path ;
17
+ use axum:: extract:: Query ;
14
18
use axum:: extract:: State ;
15
19
use axum:: response:: IntoResponse ;
16
20
use axum:: Extension ;
@@ -20,7 +24,8 @@ use derivative::Derivative;
20
24
use editoast_authz:: BuiltinRole ;
21
25
use editoast_derive:: EditoastError ;
22
26
use editoast_models:: DbConnectionPoolV2 ;
23
- use editoast_schemas:: infra:: { Direction , TrackRange } ;
27
+ use editoast_schemas:: infra:: Direction ;
28
+ use editoast_schemas:: infra:: TrackRange ;
24
29
use serde:: de:: Error as SerdeError ;
25
30
use serde:: Deserialize ;
26
31
use serde:: Serialize ;
@@ -29,6 +34,8 @@ use thiserror::Error;
29
34
use utoipa:: { IntoParams , ToSchema } ;
30
35
use uuid:: Uuid ;
31
36
37
+ use super :: pagination:: PaginatedList ;
38
+
32
39
crate :: routes! {
33
40
"/work_schedules" => {
34
41
create,
@@ -469,20 +476,38 @@ async fn put_in_group(
469
476
. await
470
477
}
471
478
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
+
472
495
#[ utoipa:: path(
473
496
get, path = "" ,
474
497
tag = "work_schedules" ,
475
- params( WorkScheduleGroupIdParam ) ,
498
+ params( PaginationQueryParam , WorkScheduleGroupIdParam , WorkScheduleOrderingParam ) ,
476
499
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 ) ) ,
478
501
( status = 404 , description = "Work schedule group not found" ) ,
479
502
)
480
503
) ]
481
504
async fn get_group (
482
505
State ( db_pool) : State < DbConnectionPoolV2 > ,
483
506
Extension ( auth) : AuthenticationExt ,
484
507
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 > > {
486
511
let authorized = auth
487
512
. check_roles ( [ BuiltinRole :: WorkScheduleRead ] . into ( ) )
488
513
. await
@@ -491,6 +516,14 @@ async fn get_group(
491
516
return Err ( AuthorizationError :: Unauthorized . into ( ) ) ;
492
517
}
493
518
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
+
494
527
let conn = & mut db_pool. get ( ) . await ?;
495
528
496
529
// Check that the group exists
@@ -499,11 +532,12 @@ async fn get_group(
499
532
} )
500
533
. await ?;
501
534
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 ?;
505
536
506
- Ok ( Json ( work_schedules) )
537
+ Ok ( Json ( GroupContentResponse {
538
+ results : work_schedules,
539
+ stats,
540
+ } ) )
507
541
}
508
542
509
543
#[ cfg( test) ]
@@ -769,10 +803,11 @@ pub mod tests {
769
803
770
804
// Get the content of the group
771
805
let request = app. get ( & work_schedule_url) ;
772
- let work_schedules = app
806
+ let response = app
773
807
. fetch ( request)
774
808
. assert_status ( StatusCode :: OK )
775
- . json_into :: < Vec < WorkSchedule > > ( ) ;
809
+ . json_into :: < GroupContentResponse > ( ) ;
810
+ let work_schedules = response. results ;
776
811
assert_eq ! ( 1 , work_schedules. len( ) ) ;
777
812
assert_eq ! ( ref_obj_id, work_schedules[ 0 ] . obj_id) ;
778
813
0 commit comments