@fabrix/spool-hapi
Version:
Spool - Hapi.js. This spool binds the routes compiled in spool-router to a Hapi Server.
228 lines (227 loc) • 9.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Boom = require("boom");
const lodash_1 = require("lodash");
const common_1 = require("@fabrix/fabrix/dist/common");
class TapestryController extends common_1.FabrixController {
create(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
this.log.debug('[TapestryController] (create) model =', request.params.model, ', payload =', request.payload, 'options =', options);
return TapestryService.create(request.params.model, request.payload, options)
.then(elements => {
return elements || {};
})
.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
find(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const criteria = this.app.spools.hapi.getCriteriaFromQuery(request.query);
let response;
this.log.debug('[TapestryController] (find) model =', request.params.model, ', criteria =', request.query, request.params.id, 'options =', options);
if (request.params.id) {
response = TapestryService.find(request.params.model, request.params.id, options);
}
else {
response = TapestryService.find(request.params.model, criteria, options);
}
return response
.then(result => {
if (!result) {
return Boom.notFound('E_NOT_FOUND');
}
return result;
})
.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
update(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const criteria = this.app.spools.hapi.getCriteriaFromQuery(request.query);
const params = request.params;
let res;
this.log.debug('[TapestryController] (update) model =', request.params.model, ', criteria =', request.query, request.params.id, ', values = ', request.payload);
if (request.params.id) {
res = TapestryService.update(params.model, params.id, request.payload, options);
}
else {
res = TapestryService.update(params.model, criteria, request.payload, options);
}
return res
.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
destroy(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const criteria = this.app.spools.hapi.getCriteriaFromQuery(request.query);
let res;
this.log.debug('[TapestryController] (destroy) model =', request.params.model, ', query =', request.query);
if (request.params.id) {
res = TapestryService.destroy(request.params.model, request.params.id, options)
.then(result => {
return result || {};
});
}
else {
res = TapestryService.destroy(request.params.model, criteria, options)
.then(result => {
return result || {};
});
}
return res.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
createAssociation(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const parentModel = request.params.parentModel;
const parentId = request.params.parentId;
const childAttribute = request.params.childAttribute;
const payload = request.payload;
this.log.debug('[TapestryController] (createAssociation)', parentModel, '->', childAttribute, ', payload =', payload, 'options =', options);
return TapestryService.createAssociation(parentModel, parentId, childAttribute, payload, options)
.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
findAssociation(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const criteria = this.app.spools.hapi.getCriteriaFromQuery(request.query);
const parentModel = request.params.parentModel;
const parentId = request.params.parentId;
const childAttribute = request.params.childAttribute;
const childId = request.params.childId;
let res;
this.log.debug('[TapestryController] (findAssociation)', parentModel, parentId, '->', childAttribute, childId, ', criteria =', request.query);
if (childId) {
res = TapestryService.findAssociation(parentModel, parentId, childAttribute, childId, lodash_1.extend({ findOne: true }, options));
}
else {
res = TapestryService.findAssociation(parentModel, parentId, childAttribute, criteria, options);
}
return res.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
updateAssociation(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const criteria = this.app.spools.hapi.getCriteriaFromQuery(request.query);
const parentModel = request.params.parentModel;
const parentId = request.params.parentId;
const childAttribute = request.params.childAttribute;
const childId = request.params.childId;
let res;
this.log.debug('[TapestryController] (updateAssociation)', parentModel, parentId, '->', childAttribute, childId, ', criteria =', request.query);
if (childId) {
res = TapestryService.updateAssociation(parentModel, parentId, childAttribute, childId, request.payload, lodash_1.extend({ findOne: true }, options));
}
else {
res = TapestryService.updateAssociation(parentModel, parentId, childAttribute, criteria, request.payload);
}
return res.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
destroyAssociation(request) {
const TapestryService = this.app.services.TapestryService;
const options = this.app.spools.hapi.getOptionsFromQuery(request.query);
const criteria = this.app.spools.hapi.getCriteriaFromQuery(request.query);
const parentModel = request.params.parentModel;
const parentId = request.params.parentId;
const childAttribute = request.params.childAttribute;
const childId = request.params.childId;
let response;
this.log.debug('[TapestryController] (destroyAssociation)', parentModel, parentId, '->', childAttribute, childId, ', criteria =', request.query);
if (childId) {
response = TapestryService.destroyAssociation(parentModel, parentId, childAttribute, childId, options);
}
else {
response = TapestryService.destroyAssociation(parentModel, parentId, childAttribute, criteria, options);
}
return response
.then(result => {
if (!result) {
return Boom.notFound('E_NOT_FOUND');
}
return result;
})
.catch(error => {
if (error.code === 'E_VALIDATION') {
return Boom.badRequest(error.code);
}
else if (error.code === 'E_NOT_FOUND') {
return Boom.notFound(error.code);
}
else {
return Boom.badImplementation(error.code);
}
});
}
}
exports.TapestryController = TapestryController;