condensing the controllers for accessing each model separately

This commit is contained in:
Arjun Patel
2019-03-17 00:13:40 -07:00
parent 5587c296e4
commit 6306d3b1a7
27 changed files with 360 additions and 402 deletions
+36
View File
@@ -0,0 +1,36 @@
const db = require('../../models'),
errorMaker = require('../helpers/error.maker'),
textCleaner = require('../helpers/text_cleaner');
const { Grant, Cause, Region, Organization } = db;
// ADD a region into the database
exports.createRegion = (req, res, next) => {
const { name } = req.body;
Region.create({ name })
.then(region => {
res.status(200).json({
message: 'Region created',
region
});
})
.catch(error => next(error));
};
// FETCH all regions
exports.getAllRegions = (req, res, next) => {
Region.findAll()
.then(regions => {
regions = regions.map(region => {
region.name = textCleaner.titleCase(region.name);
return region;
});
res.status(200).json({
regions,
number_items: regions.length
});
})
.catch(error => next(error));
};