Skip to content

Commit b8586af

Browse files
committed
mirage/config: Implement "GET /summary" handler
1 parent b6dce7c commit b8586af

File tree

2 files changed

+29
-611
lines changed

2 files changed

+29
-611
lines changed

mirage/config.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
import Response from 'ember-cli-mirage/response';
22

3-
import summaryFixture from '../mirage/fixtures/summary';
4-
53
export default function() {
6-
this.get('/summary', () => summaryFixture);
4+
this.get('/summary', function(schema) {
5+
let crates = schema.crates.all();
6+
7+
let just_updated = crates.sort((a, b) => compareIsoDates(b.updated_at, a.updated_at)).slice(0, 10);
8+
let most_downloaded = crates.sort((a, b) => b.downloads - a.downloads).slice(0, 10);
9+
let new_crates = crates.sort((a, b) => compareIsoDates(b.created_at, a.created_at)).slice(0, 10);
10+
11+
let num_crates = crates.length;
12+
let num_downloads = crates.models.reduce((sum, crate) => sum + crate.downloads, 0);
13+
14+
let popular_categories = schema.categories.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);
15+
let popular_keywords = schema.keywords.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);
16+
17+
return {
18+
just_updated: this.serialize(just_updated).crates,
19+
most_downloaded: this.serialize(most_downloaded).crates,
20+
new_crates: this.serialize(new_crates).crates,
21+
num_crates,
22+
num_downloads,
23+
popular_categories: this.serialize(popular_categories).categories,
24+
popular_keywords: this.serialize(popular_keywords).keywords,
25+
};
26+
});
727

828
this.namespace = '/api/v1';
929

@@ -201,3 +221,9 @@ function withMeta(response, meta) {
201221
function compareStrings(a, b) {
202222
return (a < b) ? -1 : (a > b) ? 1 : 0;
203223
}
224+
225+
function compareIsoDates(a, b) {
226+
let aDate = new Date(a);
227+
let bDate = new Date(b);
228+
return (aDate < bDate) ? -1 : (aDate > bDate) ? 1 : 0;
229+
}

0 commit comments

Comments
 (0)