|
1 | 1 | import Response from 'ember-cli-mirage/response';
|
2 | 2 |
|
3 |
| -import summaryFixture from '../mirage/fixtures/summary'; |
4 |
| - |
5 | 3 | 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 | + .map(it => ({ ...it, versions: null })), |
| 20 | + most_downloaded: this.serialize(most_downloaded).crates |
| 21 | + .map(it => ({ ...it, versions: null })), |
| 22 | + new_crates: this.serialize(new_crates).crates |
| 23 | + .map(it => ({ ...it, versions: null })), |
| 24 | + num_crates, |
| 25 | + num_downloads, |
| 26 | + popular_categories: this.serialize(popular_categories).categories, |
| 27 | + popular_keywords: this.serialize(popular_keywords).keywords, |
| 28 | + }; |
| 29 | + }); |
7 | 30 |
|
8 | 31 | this.namespace = '/api/v1';
|
9 | 32 |
|
@@ -204,3 +227,9 @@ function withMeta(response, meta) {
|
204 | 227 | function compareStrings(a, b) {
|
205 | 228 | return (a < b) ? -1 : (a > b) ? 1 : 0;
|
206 | 229 | }
|
| 230 | + |
| 231 | +function compareIsoDates(a, b) { |
| 232 | + let aDate = new Date(a); |
| 233 | + let bDate = new Date(b); |
| 234 | + return (aDate < bDate) ? -1 : (aDate > bDate) ? 1 : 0; |
| 235 | +} |
0 commit comments