-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.js
80 lines (67 loc) · 2.78 KB
/
methods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var _ = require('lodash');
module.exports = function(server) {
server.method('getFolders', function(next) {
return next(null, _.keys(server.app.content));
});
server.method('getFolder', function(folder_name, query, next) {
if (!_.has(server.app.content, [folder_name])) {
return next('No folder found');
}
var folder = {};
folder.meta = {};
folder.meta = _.omit(server.app.content[folder_name].meta, server.app.ignoreFromList)
if (query.meta != true) {
var items_name = 'items';
folder[items_name] = [];
// Pull out reserved queries
var limit = parseInt(query.limit);
var random = false;
if (_.has(query, 'random')) {
random = (query['random'].toLowerCase() != 'false');
}
var draft = false;
if (_.has(query, 'draft')) {
draft = (query['draft'].toLowerCase() != 'false');
}
query = _.omit(query, ['limit', 'random', 'draft']);
// Convert queries
_.forEach(query, function(n, key) {
if (_.indexOf(server.app.content[folder_name].meta.types.boolean, key) > -1) {
query[key] = (n.toLowerCase() != 'false');
} else if (_.indexOf(server.app.content[folder_name].meta.types.number, key)> -1) {
query[key] = Number(n);
}
});
// Filter items by coverted query
var items = _.filter(server.app.content[folder_name].items, query)
// Remove unwanted items in list view
_.forEach(items, function (item){
folder[items_name].push(_.omit(item, server.app.ignoreFromList));
})
// Sane Defaults
if (draft) {
folder[items_name] = _.filter(folder[items_name], 'draft', true)
} else {
folder[items_name] = _.reject(folder[items_name], 'draft', true)
}
// Sort returned items
if (random != true) {
folder[items_name] = _.sortBy(folder[items_name], 'date').reverse();
} else {
folder[items_name] = _.shuffle(folder[items_name]);
}
// Slice array down to size
if (!isNaN(limit)){
folder[items_name] = folder[items_name].slice(0, limit);
}
}
return next(null, folder);
});
server.method('getFolderItem', function(folder_name, item_name, next) {
if (!_.has(server.app.content, [folder_name])) {
return next('No item found');
}
var item = _.find(server.app.content[folder_name].items, 'slug', item_name);
return next(null, item);
});
};