2 import _ from 'lodash/lodash'; |
2 import _ from 'lodash/lodash'; |
3 import * as constants from 'corpus-common-addon/utils/constants'; |
3 import * as constants from 'corpus-common-addon/utils/constants'; |
4 |
4 |
5 export default function() { |
5 export default function() { |
6 |
6 |
7 // These comments are here to help you get started. Feel free to delete them. |
7 // These comments are here to help you get started. Feel free to delete them. |
8 |
8 |
9 /* |
9 /* |
10 Config (with defaults). |
10 Config (with defaults). |
11 |
11 |
12 Note: these only affect routes defined *after* them! |
12 Note: these only affect routes defined *after* them! |
13 */ |
13 */ |
14 // this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server |
14 // this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server |
15 // this.namespace = ''; // make this `api`, for example, if your API is namespaced |
15 // this.namespace = ''; // make this `api`, for example, if your API is namespaced |
16 this.namespace = ENV.baseURL.replace(/\/$/,"")+'/api/v1'; |
16 this.namespace = ENV.baseURL.replace(/\/$/,'')+'/api/v1'; |
17 // this.timing = 400; // delay for each request, automatically set to 0 during testing |
17 // this.timing = 400; // delay for each request, automatically set to 0 during testing |
18 |
18 |
19 this.get('/documents'); |
19 this.get('/documents'); |
20 this.get('/documents/:id', function(db, request) { |
20 this.get('/documents/:id', function(db, request) { |
21 var docId = decodeURIComponent(request.params.id); |
21 var docId = decodeURIComponent(request.params.id); |
22 |
22 |
23 return { |
23 return { |
24 'document': db.documents.find(docId) |
24 'document': db.documents.find(docId) |
25 }; |
25 }; |
26 }); |
26 }); |
27 |
27 |
28 this.get('/languages', function(db) { |
28 this.get('/languages', function(db) { |
29 var res = {}; |
29 var res = {}; |
30 _.each(db.languages, function(lang) { |
30 _.each(db.languages, function(lang) { |
31 res[lang.id] = lang.count; |
31 res[lang.id] = lang.count; |
|
32 }); |
|
33 return res; |
32 }); |
34 }); |
33 return res; |
|
34 }); |
|
35 |
35 |
36 this.get('/lexvo/:ids', function(db, request) { |
36 this.get('/themes', function(db) { |
37 var langIds = decodeURIComponent(request.params.ids); |
37 var res = {}; |
38 var resMap = _.reduce(langIds.split(','), function(res, id) { |
38 _.each(db.themes, function(theme) { |
39 var fullId = id; |
39 res[theme.id] = {'label': theme.label, 'count': theme.count}; |
40 if(!_.startsWith(fullId, constants.LEXVO_BASE_URL)) { |
40 }); |
41 fullId = constants.LEXVO_BASE_URL + id; |
|
42 } |
|
43 var lexvoRes = db.lexvo.find(fullId); |
|
44 res[id] = lexvoRes?lexvoRes.name:null; |
|
45 return res; |
41 return res; |
46 }, {}); |
42 }); |
47 |
43 |
48 return { |
44 this.get('/lexvo/:ids', function(db, request) { |
49 'lexvoids': resMap |
45 var langIds = decodeURIComponent(request.params.ids); |
50 }; |
46 var resMap = _.reduce(langIds.split(','), function(res, id) { |
|
47 var fullId = id; |
|
48 if(!_.startsWith(fullId, constants.LEXVO_BASE_URL)) { |
|
49 fullId = constants.LEXVO_BASE_URL + id; |
|
50 } |
|
51 var lexvoRes = db.lexvo.find(fullId); |
|
52 res[id] = lexvoRes?lexvoRes.name:null; |
|
53 return res; |
|
54 }, {}); |
51 |
55 |
52 }); |
56 return { |
|
57 'lexvoids': resMap |
|
58 }; |
53 |
59 |
54 this.get('/bnf/:ids', function(db, request) { |
60 }); |
55 |
61 |
56 var bnfIds = decodeURIComponent(request.params.ids); |
62 this.get('/bnf/:ids', function(db, request) { |
57 var resMap = _.reduce(bnfIds.split(','), function(res, id) { |
|
58 var fullId = id; |
|
59 if(_.startsWith(fullId, constants.BNF_BASE_URL)) { |
|
60 fullId = fullId.slice(constants.BNF_BASE_URL.length); |
|
61 } else if (_.startsWith(fullId, constants.BNF_ARK_BASE_URL)) { |
|
62 fullId = fullId.slice(constants.BNF_ARK_BASE_URL.length); |
|
63 } else if (!_.startsWith(fullId, constants.BNF_ARK_BASE_ID)) { |
|
64 fullId = constants.BNF_ARK_BASE_ID + fullId; |
|
65 } |
|
66 var bnfRes = db.lexvo.find(fullId); |
|
67 res[fullId] = bnfRes?bnfRes.label:null; |
|
68 return res; |
|
69 }, {}); |
|
70 |
63 |
71 return { |
64 var bnfIds = decodeURIComponent(request.params.ids); |
72 'bnfids': resMap |
65 var resMap = _.reduce(bnfIds.split(','), function(res, id) { |
73 }; |
66 var fullId = id; |
|
67 if(_.startsWith(fullId, constants.BNF_BASE_URL)) { |
|
68 fullId = fullId.slice(constants.BNF_BASE_URL.length); |
|
69 } else if (_.startsWith(fullId, constants.BNF_ARK_BASE_URL)) { |
|
70 fullId = fullId.slice(constants.BNF_ARK_BASE_URL.length); |
|
71 } else if (!_.startsWith(fullId, constants.BNF_ARK_BASE_ID)) { |
|
72 fullId = constants.BNF_ARK_BASE_ID + fullId; |
|
73 } |
|
74 var bnfRes = db.lexvo.find(fullId); |
|
75 res[fullId] = bnfRes?bnfRes.label:null; |
|
76 return res; |
|
77 }, {}); |
74 |
78 |
75 }); |
79 return { |
|
80 'bnfids': resMap |
|
81 }; |
76 |
82 |
77 /* |
83 }); |
78 Route shorthand cheatsheet |
|
79 */ |
|
80 /* |
|
81 GET shorthands |
|
82 |
84 |
83 // Collections |
85 /* |
84 this.get('/contacts'); |
86 Route shorthand cheatsheet |
85 this.get('/contacts', 'users'); |
87 */ |
86 this.get('/contacts', ['contacts', 'addresses']); |
88 /* |
|
89 GET shorthands |
87 |
90 |
88 // Single objects |
91 // Collections |
89 this.get('/contacts/:id'); |
92 this.get('/contacts'); |
90 this.get('/contacts/:id', 'user'); |
93 this.get('/contacts', 'users'); |
91 this.get('/contacts/:id', ['contact', 'addresses']); |
94 this.get('/contacts', ['contacts', 'addresses']); |
92 */ |
|
93 |
95 |
94 /* |
96 // Single objects |
95 POST shorthands |
97 this.get('/contacts/:id'); |
|
98 this.get('/contacts/:id', 'user'); |
|
99 this.get('/contacts/:id', ['contact', 'addresses']); |
|
100 */ |
96 |
101 |
97 this.post('/contacts'); |
102 /* |
98 this.post('/contacts', 'user'); // specify the type of resource to be created |
103 POST shorthands |
99 */ |
|
100 |
104 |
101 /* |
105 this.post('/contacts'); |
102 PUT shorthands |
106 this.post('/contacts', 'user'); // specify the type of resource to be created |
|
107 */ |
103 |
108 |
104 this.put('/contacts/:id'); |
109 /* |
105 this.put('/contacts/:id', 'user'); // specify the type of resource to be updated |
110 PUT shorthands |
106 */ |
|
107 |
111 |
108 /* |
112 this.put('/contacts/:id'); |
109 DELETE shorthands |
113 this.put('/contacts/:id', 'user'); // specify the type of resource to be updated |
|
114 */ |
110 |
115 |
111 this.del('/contacts/:id'); |
116 /* |
112 this.del('/contacts/:id', 'user'); // specify the type of resource to be deleted |
117 DELETE shorthands |
113 |
118 |
114 // Single object + related resources. Make sure parent resource is first. |
119 this.del('/contacts/:id'); |
115 this.del('/contacts/:id', ['contact', 'addresses']); |
120 this.del('/contacts/:id', 'user'); // specify the type of resource to be deleted |
116 */ |
|
117 |
121 |
118 /* |
122 // Single object + related resources. Make sure parent resource is first. |
119 Function fallback. Manipulate data in the db via |
123 this.del('/contacts/:id', ['contact', 'addresses']); |
|
124 */ |
|
125 |
|
126 /* |
|
127 Function fallback. Manipulate data in the db via |
120 |
128 |
121 - db.{collection} |
129 - db.{collection} |
122 - db.{collection}.find(id) |
130 - db.{collection}.find(id) |
123 - db.{collection}.where(query) |
131 - db.{collection}.where(query) |
124 - db.{collection}.update(target, attrs) |
132 - db.{collection}.update(target, attrs) |
125 - db.{collection}.remove(target) |
133 - db.{collection}.remove(target) |
126 |
134 |
127 // Example: return a single object with related models |
135 // Example: return a single object with related models |
128 this.get('/contacts/:id', function(db, request) { |
136 this.get('/contacts/:id', function(db, request) { |
129 var contactId = +request.params.id; |
137 var contactId = +request.params.id; |
130 |
138 |
131 return { |
139 return { |
132 contact: db.contacts.find(contactId), |
140 contact: db.contacts.find(contactId), |
133 addresses: db.addresses.where({contact_id: contactId}) |
141 addresses: db.addresses.where({contact_id: contactId}) |
134 }; |
142 }; |
135 }); |
143 }); |
136 |
144 |
137 */ |
145 */ |
138 } |
146 } |
139 |
147 |
140 /* |
148 /* |
141 You can optionally export a config that is only loaded during tests |
149 You can optionally export a config that is only loaded during tests |
142 export function testConfig() { |
150 export function testConfig() { |
143 |
151 |
144 } |
152 } |
145 */ |
153 */ |