@@ -7,13 +7,13 @@ use callisto::{
77} ;
88use common:: errors:: MegaError ;
99use common:: model:: Pagination ;
10- use sea_orm:: RelationTrait ;
1110use sea_orm:: prelude:: Expr ;
1211use sea_orm:: sea_query:: OnConflict ;
1312use sea_orm:: {
1413 ActiveModelTrait , ColumnTrait , Condition , EntityTrait , IntoActiveModel , JoinType ,
1514 PaginatorTrait , QueryFilter , QuerySelect , Set ,
1615} ;
16+ use sea_orm:: { QueryOrder , RelationTrait } ;
1717
1818use crate :: model:: common:: { ItemDetails , ListParams } ;
1919use crate :: storage:: base_storage:: { BaseStorage , StorageConnector } ;
@@ -69,7 +69,7 @@ impl ClStorage {
6969 ]
7070 } ;
7171
72- let query = mega_cl:: Entity :: find ( )
72+ let base_query = mega_cl:: Entity :: find ( )
7373 . join (
7474 JoinType :: LeftJoin ,
7575 callisto:: entity_ext:: mega_cl:: Relation :: ItemLabels . def ( ) ,
@@ -80,20 +80,23 @@ impl ClStorage {
8080 )
8181 . filter ( mega_cl:: Column :: Status . is_in ( status) )
8282 . filter ( cond)
83- . distinct ( ) ;
83+ . distinct ( )
84+ . order_by_asc ( mega_cl:: Column :: Id ) ;
8485
8586 let mut sort_map = HashMap :: new ( ) ;
8687 sort_map. insert ( "created_at" , mega_cl:: Column :: CreatedAt ) ;
8788 sort_map. insert ( "updated_at" , mega_cl:: Column :: UpdatedAt ) ;
8889
89- let query = apply_sort ( query, params. sort_by . as_deref ( ) , params. asc , & sort_map) ;
90- let paginator = query. paginate ( self . get_connection ( ) , page. per_page ) ;
91- let num_pages = paginator. num_items ( ) . await ?;
90+ let sorted_query = apply_sort ( base_query, params. sort_by . as_deref ( ) , params. asc , & sort_map) ;
9291
93- let ( cl_list, page) = paginator
94- . fetch_page ( page. page - 1 )
95- . await
96- . map ( |m| ( m, num_pages) ) ?;
92+ let paginator = sorted_query. paginate ( self . get_connection ( ) , page. per_page ) ;
93+ let total = paginator. num_items ( ) . await ?;
94+
95+ let cl_list = paginator. fetch_page ( page. page - 1 ) . await ?;
96+
97+ if cl_list. is_empty ( ) {
98+ return Ok ( ( vec ! [ ] , 0 ) ) ;
99+ }
97100
98101 let ids = cl_list. iter ( ) . map ( |m| m. id ) . collect :: < Vec < _ > > ( ) ;
99102
@@ -124,7 +127,7 @@ impl ClStorage {
124127
125128 let res = combine_item_list :: < mega_cl:: Entity > ( labels, assignees, conversations) ;
126129
127- Ok ( ( res, page ) )
130+ Ok ( ( res, total ) )
128131 }
129132
130133 pub async fn get_cl_suggestions_by_query (
0 commit comments