@@ -2,14 +2,13 @@ use super::helpers::pagination::*;
22use crate :: app:: AppState ;
33use crate :: models:: Category ;
44use crate :: schema:: categories;
5- use crate :: tasks:: spawn_blocking;
65use crate :: util:: errors:: AppResult ;
76use crate :: util:: RequestUtils ;
87use crate :: views:: { EncodableCategory , EncodableCategoryWithSubcategories } ;
98use axum:: extract:: Path ;
109use axum:: Json ;
11- use diesel:: prelude :: * ;
12- use diesel_async:: async_connection_wrapper :: AsyncConnectionWrapper ;
10+ use diesel:: QueryDsl ;
11+ use diesel_async:: RunQueryDsl ;
1312use http:: request:: Parts ;
1413use serde_json:: Value ;
1514
@@ -20,86 +19,77 @@ pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {
2019 // to paginate this.
2120 let options = PaginationOptions :: builder ( ) . gather ( & req) ?;
2221
23- let conn = app. db_read ( ) . await ?;
24- spawn_blocking ( move || {
25- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
22+ let mut conn = app. db_read ( ) . await ?;
2623
27- let query = req. query ( ) ;
28- let sort = query. get ( "sort" ) . map_or ( "alpha" , String :: as_str) ;
24+ let query = req. query ( ) ;
25+ let sort = query. get ( "sort" ) . map_or ( "alpha" , String :: as_str) ;
2926
30- let offset = options. offset ( ) . unwrap_or_default ( ) ;
27+ let offset = options. offset ( ) . unwrap_or_default ( ) ;
3128
32- let categories = Category :: toplevel ( conn, sort, options. per_page , offset) ?;
33- let categories = categories
34- . into_iter ( )
35- . map ( Category :: into)
36- . collect :: < Vec < EncodableCategory > > ( ) ;
29+ let categories = Category :: toplevel ( & mut conn, sort, options. per_page , offset) . await ?;
30+ let categories = categories
31+ . into_iter ( )
32+ . map ( Category :: into)
33+ . collect :: < Vec < EncodableCategory > > ( ) ;
3734
38- // Query for the total count of categories
39- let total = Category :: count_toplevel ( conn) ?;
35+ // Query for the total count of categories
36+ let total = Category :: count_toplevel ( & mut conn) . await ?;
4037
41- Ok ( Json ( json ! ( {
42- "categories" : categories,
43- "meta" : { "total" : total } ,
44- } ) ) )
45- } )
46- . await
38+ Ok ( Json ( json ! ( {
39+ "categories" : categories,
40+ "meta" : { "total" : total } ,
41+ } ) ) )
4742}
4843
4944/// Handles the `GET /categories/:category_id` route.
5045pub async fn show ( state : AppState , Path ( slug) : Path < String > ) -> AppResult < Json < Value > > {
51- let conn = state. db_read ( ) . await ?;
52- spawn_blocking ( move || {
53- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
46+ let mut conn = state. db_read ( ) . await ?;
5447
55- let cat: Category = Category :: by_slug ( & slug) . first ( conn) ?;
56- let subcats = cat
57- . subcategories ( conn) ?
58- . into_iter ( )
59- . map ( Category :: into)
60- . collect ( ) ;
61- let parents = cat
62- . parent_categories ( conn) ?
63- . into_iter ( )
64- . map ( Category :: into)
65- . collect ( ) ;
48+ let cat: Category = Category :: by_slug ( & slug) . first ( & mut conn) . await ?;
49+ let subcats = cat
50+ . subcategories ( & mut conn)
51+ . await ?
52+ . into_iter ( )
53+ . map ( Category :: into)
54+ . collect ( ) ;
55+ let parents = cat
56+ . parent_categories ( & mut conn)
57+ . await ?
58+ . into_iter ( )
59+ . map ( Category :: into)
60+ . collect ( ) ;
6661
67- let cat = EncodableCategory :: from ( cat) ;
68- let cat_with_subcats = EncodableCategoryWithSubcategories {
69- id : cat. id ,
70- category : cat. category ,
71- slug : cat. slug ,
72- description : cat. description ,
73- created_at : cat. created_at ,
74- crates_cnt : cat. crates_cnt ,
75- subcategories : subcats,
76- parent_categories : parents,
77- } ;
62+ let cat = EncodableCategory :: from ( cat) ;
63+ let cat_with_subcats = EncodableCategoryWithSubcategories {
64+ id : cat. id ,
65+ category : cat. category ,
66+ slug : cat. slug ,
67+ description : cat. description ,
68+ created_at : cat. created_at ,
69+ crates_cnt : cat. crates_cnt ,
70+ subcategories : subcats,
71+ parent_categories : parents,
72+ } ;
7873
79- Ok ( Json ( json ! ( { "category" : cat_with_subcats } ) ) )
80- } )
81- . await
74+ Ok ( Json ( json ! ( { "category" : cat_with_subcats } ) ) )
8275}
8376
8477/// Handles the `GET /category_slugs` route.
8578pub async fn slugs ( state : AppState ) -> AppResult < Json < Value > > {
86- let conn = state. db_read ( ) . await ?;
87- spawn_blocking ( move || {
88- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
79+ let mut conn = state. db_read ( ) . await ?;
8980
90- let slugs: Vec < Slug > = categories:: table
91- . select ( ( categories:: slug, categories:: slug, categories:: description) )
92- . order ( categories:: slug)
93- . load ( conn) ?;
81+ let slugs: Vec < Slug > = categories:: table
82+ . select ( ( categories:: slug, categories:: slug, categories:: description) )
83+ . order ( categories:: slug)
84+ . load ( & mut conn)
85+ . await ?;
9486
95- #[ derive( Serialize , Queryable ) ]
96- struct Slug {
97- id : String ,
98- slug : String ,
99- description : String ,
100- }
87+ #[ derive( Serialize , Queryable ) ]
88+ struct Slug {
89+ id : String ,
90+ slug : String ,
91+ description : String ,
92+ }
10193
102- Ok ( Json ( json ! ( { "category_slugs" : slugs } ) ) )
103- } )
104- . await
94+ Ok ( Json ( json ! ( { "category_slugs" : slugs } ) ) )
10595}
0 commit comments