'use strict'; var _ = require('underscore'); var joi = require('joi'); var Foxx = require('@arangodb/foxx'); var ArangoError = require('@arangodb').ArangoError; var <%= repository %> = require('../<%= repositoryPath %>'); var <%= model %> = require('../<%= modelPath %>'); var controller = new Foxx.Controller(applicationContext); var <%= modelInstance %>IdSchema = joi.string().required() .description('The id of the <%= modelInstance %>') .meta({allowMultiple: false}); var <%= repositoryInstance %> = new <%= repository %>( applicationContext.collection('<%= collectionName %>'), {model: <%= model %>} ); /** Lists of all <%= collectionName %>. * * This function simply returns the list of all <%= model %>. */ controller.get('/', function (req, res) { res.json(_.map(<%= repositoryInstance %>.all(), function (model) { return model.forClient(); })); }); /** Creates a new <%= modelInstance %>. * * Creates a new <%= modelInstance %>. The information has to be in the * requestBody. */ controller.post('/', function (req, res) { var <%= modelInstance %> = req.parameters.<%= modelInstance %>; res.json(<%= repositoryInstance %>.save(<%= modelInstance %>).forClient()); }) .bodyParam('<%= modelInstance %>', { description: 'The <%= modelInstance %> you want to create', type: <%= model %> }); /** Reads a <%= modelInstance %>. * * Reads a <%= modelInstance %>. */ controller.get('/:id', function (req, res) { var id = req.urlParameters.id; res.json(<%= repositoryInstance %>.byId(id).forClient()); }) .pathParam('id', <%= modelInstance %>IdSchema) .errorResponse(ArangoError, 404, 'The <%= modelInstance %> could not be found'); /** Replaces a <%= modelInstance %>. * * Changes a <%= modelInstance %>. The information has to be in the * requestBody. */ controller.put('/:id', function (req, res) { var id = req.urlParameters.id; var <%= modelInstance %> = req.parameters.<%= modelInstance %>; res.json(<%= repositoryInstance %>.replaceById(id, <%= modelInstance %>)); }) .pathParam('id', <%= modelInstance %>IdSchema) .bodyParam('<%= modelInstance %>', { description: 'The <%= modelInstance %> you want your old one to be replaced with', type: <%= model %> }) .errorResponse(ArangoError, 404, 'The <%= modelInstance %> could not be found'); /** Updates a <%= modelInstance %>. * * Changes a <%= modelInstance %>. The information has to be in the * requestBody. */ controller.patch('/:id', function (req, res) { var id = req.urlParameters.id; var patchData = req.parameters.patch; res.json(<%= repositoryInstance %>.updateById(id, patchData)); }) .pathParam('id', <%= modelInstance %>IdSchema) .bodyParam('patch', { description: 'The patch data you want your <%= modelInstance %> to be updated with', type: joi.object().required() }) .errorResponse(ArangoError, 404, 'The <%= modelInstance %> could not be found'); /** Removes a <%= modelInstance %>. * * Removes a <%= modelInstance %>. */ controller.delete('/:id', function (req, res) { var id = req.urlParameters.id; <%= repositoryInstance %>.removeById(id); res.json({success: true}); }) .pathParam('id', <%= modelInstance %>IdSchema) .errorResponse(ArangoError, 404, 'The <%= modelInstance %> could not be found');