11use crate :: api:: v2:: models:: { NavigationData , UnhydratedItemNavigationData } ;
22use crate :: models:: ComicId ;
33use actix_web:: { error, Result } ;
4- use database:: models:: Item as DatabaseItem ;
4+ use database:: models:: { Item as DatabaseItem , ItemId as DatabaseItemId , PreviousAppearances } ;
55use database:: DbPoolConnection ;
66use std:: convert:: TryInto ;
77
8+ #[ derive( Clone , Copy , Debug ) ]
9+ pub enum ItemNavigationDataSorting {
10+ ByCount ,
11+ ByLastAppearance ,
12+ }
13+
814#[ tracing:: instrument( skip( conn) ) ]
915#[ allow( clippy:: too_many_lines) ]
1016pub async fn fetch_all_item_navigation_data (
1117 conn : & mut DbPoolConnection ,
1218 comic_id : ComicId ,
1319 include_guest_comics : Option < bool > ,
1420 include_non_canon_comics : Option < bool > ,
21+ sorting : ItemNavigationDataSorting ,
1522) -> Result < Vec < UnhydratedItemNavigationData > > {
1623 let first_last_counts = DatabaseItem :: first_and_last_apperances_and_count (
1724 & mut * * conn,
@@ -21,7 +28,10 @@ pub async fn fetch_all_item_navigation_data(
2128 . await
2229 . map_err ( error:: ErrorInternalServerError ) ?;
2330
24- let previous = DatabaseItem :: previous_apperances_by_comic_id_mapped_by_id (
31+ let PreviousAppearances {
32+ appearances : previous,
33+ order : last_appearance_order,
34+ } = DatabaseItem :: previous_apperances_by_comic_id_mapped_by_id (
2535 & mut * * conn,
2636 comic_id. into_inner ( ) ,
2737 include_guest_comics,
@@ -39,37 +49,83 @@ pub async fn fetch_all_item_navigation_data(
3949 . await
4050 . map_err ( error:: ErrorInternalServerError ) ?;
4151
42- Ok ( first_last_counts
43- . into_iter ( )
44- . map ( |flc| UnhydratedItemNavigationData {
45- id : flc. id . into ( ) ,
46- navigation_data : NavigationData {
47- first : flc
48- . first
49- . map ( TryInto :: try_into)
50- . transpose ( )
51- . expect ( "database has valid comicIds" ) ,
52- previous : previous
53- . get ( & flc. id )
54- . copied ( )
55- . map ( TryInto :: try_into)
56- . transpose ( )
57- . expect ( "database has valid comicIds" ) ,
58- next : next
59- . get ( & flc. id )
60- . copied ( )
61- . map ( TryInto :: try_into)
62- . transpose ( )
63- . expect ( "database has valid comicIds" ) ,
64- last : flc
65- . last
66- . map ( TryInto :: try_into)
67- . transpose ( )
68- . expect ( "database has valid comicIds" ) ,
69- } ,
70- count : flc. count ,
71- } )
72- . collect ( ) )
52+ match sorting {
53+ ItemNavigationDataSorting :: ByCount => Ok ( first_last_counts
54+ . into_iter ( )
55+ . map ( |flc| UnhydratedItemNavigationData {
56+ id : flc. id . into ( ) ,
57+ navigation_data : NavigationData {
58+ first : flc
59+ . first
60+ . map ( TryInto :: try_into)
61+ . transpose ( )
62+ . expect ( "database has valid comicIds" ) ,
63+ previous : previous
64+ . get ( & flc. id )
65+ . copied ( )
66+ . map ( TryInto :: try_into)
67+ . transpose ( )
68+ . expect ( "database has valid comicIds" ) ,
69+ next : next
70+ . get ( & flc. id )
71+ . copied ( )
72+ . map ( TryInto :: try_into)
73+ . transpose ( )
74+ . expect ( "database has valid comicIds" ) ,
75+ last : flc
76+ . last
77+ . map ( TryInto :: try_into)
78+ . transpose ( )
79+ . expect ( "database has valid comicIds" ) ,
80+ } ,
81+ count : flc. count ,
82+ } )
83+ . collect ( ) ) ,
84+ ItemNavigationDataSorting :: ByLastAppearance => Ok ( last_appearance_order
85+ . iter ( )
86+ . map ( DatabaseItemId :: as_inner)
87+ . chain (
88+ first_last_counts
89+ . iter ( )
90+ . filter ( |flc| !last_appearance_order. iter ( ) . any ( |& i| i == flc. id ) )
91+ . map ( |flc| & flc. id ) ,
92+ )
93+ . map ( |& id| {
94+ first_last_counts
95+ . iter ( )
96+ . find ( |flc| flc. id == id)
97+ . expect ( "database has valid itemIds" )
98+ } )
99+ . map ( |flc| UnhydratedItemNavigationData {
100+ id : flc. id . into ( ) ,
101+ navigation_data : NavigationData {
102+ first : flc
103+ . first
104+ . map ( TryInto :: try_into)
105+ . transpose ( )
106+ . expect ( "database has valid comicIds" ) ,
107+ previous : previous
108+ . get ( & flc. id )
109+ . copied ( )
110+ . map ( TryInto :: try_into)
111+ . transpose ( )
112+ . expect ( "database has valid comicIds" ) ,
113+ next : next
114+ . get ( & flc. id )
115+ . copied ( )
116+ . map ( TryInto :: try_into)
117+ . transpose ( )
118+ . expect ( "database has valid comicIds" ) ,
119+ last : flc
120+ . last
121+ . map ( TryInto :: try_into)
122+ . transpose ( )
123+ . expect ( "database has valid comicIds" ) ,
124+ } ,
125+ count : flc. count ,
126+ } )
127+ . collect ( ) ) ,
128+ }
73129}
74130
75131#[ tracing:: instrument( skip( conn) ) ]
0 commit comments