11use axum:: Json ;
22use axum:: extract:: Path ;
3- use axum_extra:: json;
4- use axum_extra:: response:: ErasedJson ;
53use bigdecimal:: { BigDecimal , ToPrimitive } ;
64use diesel:: prelude:: * ;
75use diesel_async:: RunQueryDsl ;
@@ -46,6 +44,13 @@ pub async fn find_user(
4644 Ok ( Json ( GetResponse { user : user. into ( ) } ) )
4745}
4846
47+ #[ derive( Debug , Serialize , utoipa:: ToSchema ) ]
48+ pub struct StatsResponse {
49+ /// The total number of downloads for crates owned by the user.
50+ #[ schema( example = 123_456_789 ) ]
51+ pub total_downloads : u64 ,
52+ }
53+
4954/// Get user stats.
5055///
5156/// This currently only returns the total number of downloads for crates owned
@@ -57,15 +62,18 @@ pub async fn find_user(
5762 ( "id" = i32 , Path , description = "ID of the user" ) ,
5863 ) ,
5964 tag = "users" ,
60- responses( ( status = 200 , description = "Successful Response" ) ) ,
65+ responses( ( status = 200 , description = "Successful Response" , body = inline ( StatsResponse ) ) ) ,
6166) ]
62- pub async fn get_user_stats ( state : AppState , Path ( user_id) : Path < i32 > ) -> AppResult < ErasedJson > {
67+ pub async fn get_user_stats (
68+ state : AppState ,
69+ Path ( user_id) : Path < i32 > ,
70+ ) -> AppResult < Json < StatsResponse > > {
6371 let mut conn = state. db_read_prefer_primary ( ) . await ?;
6472
6573 use diesel:: dsl:: sum;
6674 use diesel_async:: RunQueryDsl ;
6775
68- let data = CrateOwner :: by_owner_kind ( OwnerKind :: User )
76+ let total_downloads = CrateOwner :: by_owner_kind ( OwnerKind :: User )
6977 . inner_join ( crates:: table)
7078 . inner_join ( crate_downloads:: table. on ( crates:: id. eq ( crate_downloads:: crate_id) ) )
7179 . filter ( crate_owners:: owner_id. eq ( user_id) )
@@ -75,5 +83,5 @@ pub async fn get_user_stats(state: AppState, Path(user_id): Path<i32>) -> AppRes
7583 . map ( |d| d. to_u64 ( ) . unwrap_or ( u64:: MAX ) )
7684 . unwrap_or ( 0 ) ;
7785
78- Ok ( json ! ( { " total_downloads" : data } ) )
86+ Ok ( Json ( StatsResponse { total_downloads } ) )
7987}
0 commit comments