author | Chloe Laisne <chloe.laisne@gmail.com> |
Sat, 09 Jul 2016 00:59:32 +0200 | |
changeset 236 | ac6928e86d14 |
parent 173 | cf7b221238fd |
child 237 | 69a9f3687902 |
permissions | -rw-r--r-- |
173 | 1 |
import { JSONAPISerializer } from 'ember-cli-mirage'; |
2 |
||
3 |
import _ from 'lodash/lodash'; |
|
4 |
||
5 |
export default JSONAPISerializer.extend({ |
|
236
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
6 |
|
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
7 |
serialize: function(response, request) { |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
8 |
// Remove models with no name |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
9 |
response.models = response.models.filter(element => element.label); |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
10 |
// Alphabetical order |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
11 |
if(request.queryParams.sort === 'alphabetical') { |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
12 |
response.models.sort(function(a, b) { |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
13 |
var aLabel = a.label.toUpperCase(); |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
14 |
var bLabel = b.label.toUpperCase(); |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
15 |
if (aLabel < bLabel) return -1; |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
16 |
if (aLabel > bLabel) return 1; |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
17 |
return 0; |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
18 |
}); |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
19 |
// Descending order |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
20 |
} else if(request.queryParams.sort === 'descending') { |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
21 |
response.models.sort(function(a, b) { |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
22 |
return b.count - a.count; |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
23 |
}); |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
24 |
} |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
25 |
var index = request.queryParams.page * request.queryParams.limit; |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
26 |
var slice = response.models.slice(index, index + request.queryParams.limit); |
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
27 |
return _(slice).map((theme) => { return [theme.id, {count: theme.count, label: theme.label}];}).object().value(); |
173 | 28 |
} |
236
ac6928e86d14
Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents:
173
diff
changeset
|
29 |
|
173 | 30 |
}); |