Skip to content

Commit 803e5ae

Browse files
committed
Total Downloads display in User Dashboard
1 parent 7ab2b1b commit 803e5ae

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

app/controllers/dashboard.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default Ember.Controller.extend({
1414
this.myCrates = [];
1515
this.myFollowing = [];
1616
this.myFeed = [];
17+
this.myStats = 0;
1718
},
1819

1920
visibleCrates: computed('myCreates', function() {
@@ -24,6 +25,10 @@ export default Ember.Controller.extend({
2425
return this.get('myFollowing').slice(0, TO_SHOW);
2526
}),
2627

28+
visibleStats: computed('myStats', function() {
29+
return this.get('myStats');
30+
}),
31+
2732
hasMoreCrates: computed('myCreates', function() {
2833
return this.get('myCrates.length') > TO_SHOW;
2934
}),

app/models/user.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ export default DS.Model.extend({
77
api_token: DS.attr('string'),
88
avatar: DS.attr('string'),
99
url: DS.attr('string'),
10+
11+
stats() {
12+
return this.store.adapterFor('user').stats(this.get('id'));
13+
},
1014
});

app/routes/dashboard.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default Ember.Route.extend(AuthenticatedRoute, {
1010
controller.set('fetchingFeed', true);
1111
controller.set('myCrates', this.get('data.myCrates'));
1212
controller.set('myFollowing', this.get('data.myFollowing'));
13+
controller.set('myStats', this.get('data.myStats'));
1314

1415
if (!controller.get('loadingMore')) {
1516
controller.set('myFeed', []);
@@ -30,9 +31,14 @@ export default Ember.Route.extend(AuthenticatedRoute, {
3031
following: 1
3132
});
3233

34+
let myStats = user.stats();
35+
3336
return Ember.RSVP.hash({
3437
myCrates,
35-
myFollowing
36-
}).then((hash) => this.set('data', hash));
38+
myFollowing,
39+
myStats
40+
}).then((hash) => {
41+
this.set('data', hash)
42+
});
3743
}
3844
});

app/styles/me.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,18 @@
8686
&:hover { background-color: darken($bg, 10%); }
8787
}
8888
}
89+
90+
#stats{
91+
margin-left: auto;
92+
padding: 10px;
93+
94+
span { margin-left: 10px; }
95+
.num {
96+
font-size: 30px;
97+
font-weight: bold;
98+
}
99+
.downloads {
100+
@include display-flex;
101+
@include align-items(center);
102+
}
103+
}

app/templates/dashboard.hbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
<div id='crates-heading'>
44
<img class='logo dashboard' src="/assets/dashboard.png"/>
55
<h1>My Dashboard</h1>
6+
<div id="stats">
7+
<div class='downloads'>
8+
<img class="download" src="/assets/download.png"/>
9+
<span class='num'>{{visibleStats.total_downloads}}</span>
10+
<span class='desc small'>Total Downloads</span>
11+
</div>
12+
</div>
613
</div>
714

15+
816
<div id='my-info'>
917
<div id='my-crate-lists' class='crate-lists'>
1018
<div id='my-crates'>

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub fn middleware(app: Arc<App>) -> MiddlewareBuilder {
131131
api_router.get("/categories/:category_id", C(category::show));
132132
api_router.get("/category_slugs", C(category::slugs));
133133
api_router.get("/users/:user_id", C(user::show));
134+
api_router.get("/users/:user_id/stats", C(user::stats));
134135
let api_router = Arc::new(R404(api_router));
135136

136137
let mut router = RouteBuilder::new();

src/user/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,24 @@ pub fn updates(req: &mut Request) -> CargoResult<Response> {
378378
Ok(req.json(&R{ versions: versions, meta: Meta { more: more } }))
379379
}
380380

381+
/// Handles the `GET /users/:user_id/stats` route.
382+
pub fn stats(req: &mut Request) -> CargoResult<Response> {
383+
use diesel::expression::dsl::sum;
384+
let name = &req.params()["user_id"].parse::<i32>().ok().unwrap();
385+
let conn = req.db_conn()?;
386+
387+
let data = crate_downloads::table.filter(crate_downloads::crate_id.eq_any(
388+
crate_owners::table.select(crate_owners::crate_id)
389+
.filter(crate_owners::owner_id.eq(name))
390+
)).select(sum(crate_downloads::downloads)).first::<i64>(&*conn)?;
391+
392+
#[derive(RustcEncodable)]
393+
struct R {
394+
total_downloads: i64
395+
}
396+
Ok(req.json(&R { total_downloads: data }))
397+
}
398+
381399
#[cfg(test)]
382400
mod tests {
383401
use super::*;

0 commit comments

Comments
 (0)