|
| 1 | +import resource from 'resource-router-middleware'; |
| 2 | +import facets from '../models/facets'; |
| 3 | + |
| 4 | +export default resource({ |
| 5 | + |
| 6 | + /** Property name to store preloaded entity on `request`. */ |
| 7 | + id : 'facet', |
| 8 | + |
| 9 | + /** For requests with an `id`, you can auto-load the entity. |
| 10 | + * Errors terminate the request, success sets `req[id] = data`. |
| 11 | + */ |
| 12 | + load(req, id, callback) { |
| 13 | + var facet = facets.find( facet => facet.id===id ), |
| 14 | + err = facet ? null : 'Not found'; |
| 15 | + callback(err, user); |
| 16 | + }, |
| 17 | + |
| 18 | + /** GET / - List all entities */ |
| 19 | + index({ params }, res) { |
| 20 | + res.json(facets); |
| 21 | + }, |
| 22 | + |
| 23 | + /** POST / - Create a new entity */ |
| 24 | + create({ body }, res) { |
| 25 | + body.id = facets.length.toString(36); |
| 26 | + facets.push(body); |
| 27 | + res.json(body); |
| 28 | + }, |
| 29 | + |
| 30 | + /** GET /:id - Return a given entity */ |
| 31 | + read({ params }, res) { |
| 32 | + res.json(req.facet); |
| 33 | + }, |
| 34 | + |
| 35 | + /** PUT /:id - Update a given entity */ |
| 36 | + update({ facet, body }, res) { |
| 37 | + for (let key in body) { |
| 38 | + if (key!=='id') { |
| 39 | + facet[key] = body[key]; |
| 40 | + } |
| 41 | + } |
| 42 | + res.sendStatus(204); |
| 43 | + }, |
| 44 | + |
| 45 | + /** DELETE /:id - Delete a given entity */ |
| 46 | + delete({ facet }, res) { |
| 47 | + facets.splice(facets.indexOf(facet), 1); |
| 48 | + res.sendStatus(204); |
| 49 | + } |
| 50 | +}); |
0 commit comments