@@ -8,6 +8,7 @@ use axum::Json;
88use axum:: extract:: { FromRequestParts , Path , Query } ;
99use diesel:: QueryDsl ;
1010use diesel_async:: RunQueryDsl ;
11+ use futures_util:: FutureExt ;
1112use http:: request:: Parts ;
1213
1314#[ derive( Debug , Deserialize , FromRequestParts , utoipa:: IntoParams ) ]
@@ -62,11 +63,13 @@ pub async fn list_categories(
6263
6364 let offset = options. offset ( ) . unwrap_or_default ( ) ;
6465
65- let categories = Category :: toplevel ( & mut conn, sort, options. per_page , offset) . await ?;
66- let categories = categories. into_iter ( ) . map ( Category :: into) . collect ( ) ;
66+ let ( categories, total) = tokio:: try_join!(
67+ Category :: toplevel( & mut conn, sort, options. per_page, offset) . boxed( ) ,
68+ // Query for the total count of categories
69+ Category :: count_toplevel( & mut conn) . boxed( ) ,
70+ ) ?;
6771
68- // Query for the total count of categories
69- let total = Category :: count_toplevel ( & mut conn) . await ?;
72+ let categories = categories. into_iter ( ) . map ( Category :: into) . collect ( ) ;
7073
7174 let meta = ListMeta { total } ;
7275 Ok ( Json ( ListResponse { categories, meta } ) )
@@ -94,18 +97,13 @@ pub async fn find_category(
9497 let mut conn = state. db_read ( ) . await ?;
9598
9699 let cat: Category = Category :: by_slug ( & slug) . first ( & mut conn) . await ?;
97- let subcats = cat
98- . subcategories ( & mut conn)
99- . await ?
100- . into_iter ( )
101- . map ( Category :: into)
102- . collect ( ) ;
103- let parents = cat
104- . parent_categories ( & mut conn)
105- . await ?
106- . into_iter ( )
107- . map ( Category :: into)
108- . collect ( ) ;
100+ let ( subcats, parents) = tokio:: try_join!(
101+ cat. subcategories( & mut conn) ,
102+ cat. parent_categories( & mut conn) . boxed( ) ,
103+ ) ?;
104+
105+ let subcats = subcats. into_iter ( ) . map ( Category :: into) . collect ( ) ;
106+ let parents = parents. into_iter ( ) . map ( Category :: into) . collect ( ) ;
109107
110108 let mut category = EncodableCategory :: from ( cat) ;
111109 category. subcategories = Some ( subcats) ;
0 commit comments