organize general routes

This commit is contained in:
Arjun Patel
2019-01-19 00:42:56 -08:00
parent 59c327d8df
commit fc2b73b715
18 changed files with 154 additions and 83 deletions
+5 -5
View File
@@ -1,21 +1,21 @@
module.exports = models => { module.exports = models => {
const { Donors, Grants, Causes, Regions, Organizations } = models; const { Donor, Grant, Cause, Region, Organization } = models;
Donors.hasMany(Grants, { foreignKey: 'donor_id' }); Donor.hasMany(Grant, { foreignKey: 'donor_id' });
Grants.belongsToMany(Causes, { Grant.belongsToMany(Cause, {
through: 'grants_causes', through: 'grants_causes',
foreignKey: 'grant_id', foreignKey: 'grant_id',
otherKey: 'cause_id' otherKey: 'cause_id'
}); });
Grants.belongsToMany(Regions, { Grant.belongsToMany(Region, {
through: 'grants_regions', through: 'grants_regions',
foreignKey: 'grant_id', foreignKey: 'grant_id',
otherKey: 'region_id' otherKey: 'region_id'
}); });
Grants.belongsToMany(Organizations, { Grant.belongsToMany(Organization, {
through: 'grants_organizations', through: 'grants_organizations',
foreignKey: 'grant_id', foreignKey: 'grant_id',
otherKey: 'organization_id' otherKey: 'organization_id'
+15 -15
View File
@@ -1,9 +1,9 @@
const Sequelize = require('sequelize'), const Sequelize = require('sequelize'),
DonorsModel = require('../models/donors.model.js'), DonorModel = require('../models/donor.model.js'),
GrantsModel = require('../models/grants.model.js'), GrantModel = require('../models/grant.model.js'),
CausesModel = require('../models/causes.model.js'), CauseModel = require('../models/cause.model.js'),
RegionsModel = require('../models/regions.model.js'), RegionModel = require('../models/region.model.js'),
OrganizationsModel = require('../models/organizations.model.js'), OrganizationModel = require('../models/organization.model.js'),
associate = require('./associate'); associate = require('./associate');
const connection_uri = process.env.JAWSDB_MARIA_URL; const connection_uri = process.env.JAWSDB_MARIA_URL;
@@ -33,16 +33,16 @@ const sequelize = new Sequelize(connection_uri, {
const db = {}; const db = {};
//creating tables/models from imported functions //creating tables/models from imported functions
const Donors = DonorsModel(sequelize, Sequelize); const Donor = DonorModel(sequelize, Sequelize);
const Grants = GrantsModel(sequelize, Sequelize); const Grant = GrantModel(sequelize, Sequelize);
const Causes = CausesModel(sequelize, Sequelize); const Cause = CauseModel(sequelize, Sequelize);
const Regions = RegionsModel(sequelize, Sequelize); const Region = RegionModel(sequelize, Sequelize);
const Organizations = OrganizationsModel(sequelize, Sequelize); const Organization = OrganizationModel(sequelize, Sequelize);
db.Donors = Donors; db.Donor = Donor;
db.Grants = Grants; db.Grant = Grant;
db.Causes = Causes; db.Cause = Cause;
db.Regions = Regions; db.Region = Region;
db.Organizations = Organizations; db.Organization = Organization;
// make associations with created models // make associations with created models
associate(db); associate(db);
+2 -2
View File
@@ -3,11 +3,11 @@ const db = require('../config/db.config.js'),
jwt = require('jsonwebtoken'), jwt = require('jsonwebtoken'),
errorMaker = require('../helpers/error.maker'); errorMaker = require('../helpers/error.maker');
const Donors = db.Donors; const Donor = db.Donor;
// Find a Donor by email + login with JWT // Find a Donor by email + login with JWT
exports.login = (req, res, next) => { exports.login = (req, res, next) => {
Donors.findAll({ Donor.findAll({
where: { where: {
email: req.body.email email: req.body.email
} }
@@ -2,12 +2,12 @@ const db = require('../../config/db.config.js'),
bcrypt = require('bcrypt-nodejs'), bcrypt = require('bcrypt-nodejs'),
errorMaker = require('../../helpers/error.maker'); errorMaker = require('../../helpers/error.maker');
const Donors = db.Donors; const Donor = db.Donor;
// Create/post a Donor // Create/post a Donor
exports.create = (req, res, next) => { exports.create = (req, res, next) => {
// see if user already in db // see if user already in db
Donors.findAll({ Donor.findAll({
where: { where: {
email: req.body.email email: req.body.email
} }
@@ -22,7 +22,7 @@ exports.create = (req, res, next) => {
if (error) { if (error) {
return next(error); return next(error);
} else { } else {
Donors.create({ Donor.create({
first_name: req.body.first_name, first_name: req.body.first_name,
middle_name: req.body.middle_name, middle_name: req.body.middle_name,
last_name: req.body.last_name, last_name: req.body.last_name,
@@ -50,9 +50,9 @@ exports.create = (req, res, next) => {
.catch(error => next(error)); .catch(error => next(error));
}; };
// FETCH all Donors // FETCH all Donor
exports.findAll = (req, res, next) => { exports.findAll = (req, res, next) => {
Donors.findAll().then(donors => { Donor.findAll().then(donors => {
// Send all donors to Client // Send all donors to Client
res.status(200).json({ res.status(200).json({
donors, donors,
@@ -63,7 +63,7 @@ exports.findAll = (req, res, next) => {
// Find a Donor by Id // Find a Donor by Id
exports.findById = (req, res) => { exports.findById = (req, res) => {
Donors.findById(req.params.donor_id).then(donor => { Donor.findById(req.params.donor_id).then(donor => {
res.send(donor); res.send(donor);
}); });
}; };
@@ -71,7 +71,7 @@ exports.findById = (req, res) => {
// Delete a Donor by Id // Delete a Donor by Id
exports.delete = (req, res) => { exports.delete = (req, res) => {
const id = req.params.donor_id; const id = req.params.donor_id;
Donors.destroy({ Donor.destroy({
where: { id: id } where: { id: id }
}).then(() => { }).then(() => {
res.status(200).send('deleted successfully a donor with id = ' + id); res.status(200).send('deleted successfully a donor with id = ' + id);
@@ -81,7 +81,7 @@ exports.delete = (req, res) => {
// // Update a Donor // // Update a Donor
// exports.update = (req, res) => { // exports.update = (req, res) => {
// const id = req.params.donor_id; // const id = req.params.donor_id;
// Donors.update( { firstname: req.body.firstname, lastname: req.body.lastname, age: req.body.age }, // Donor.update( { firstname: req.body.firstname, lastname: req.body.lastname, age: req.body.age },
// { where: {id: req.params.donorId} } // { where: {id: req.params.donorId} }
// ).then(() => { // ).then(() => {
// res.status(200).send("updated successfully a donor with id = " + id); // res.status(200).send("updated successfully a donor with id = " + id);
@@ -1,14 +1,14 @@
const db = require('../../config/db.config.js'), const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker'); errorMaker = require('../../helpers/error.maker');
const { Grants, Causes, Regions, Organizations } = db; const { Grant, Cause, Region, Organization } = db;
// Create a grant for certain donor // Create a grant for certain donor
exports.create = (req, res, next) => { exports.create = (req, res, next) => {
const donor_id = req.params.donor_id; const donor_id = req.params.donor_id;
const { name, amount, monthly, causes, regions, organizations } = req.body; const { name, amount, monthly, causes, regions, organizations } = req.body;
Grants.create({ Grant.create({
donor_id, donor_id,
name, name,
amount, amount,
@@ -33,16 +33,16 @@ exports.create = (req, res, next) => {
// Find grants with causes, regions, and organizations by donor_id // Find grants with causes, regions, and organizations by donor_id
exports.findByDonorId = (req, res, next) => { exports.findByDonorId = (req, res, next) => {
Grants.findAll({ Grant.findAll({
where: { where: {
donor_id: req.params.donor_id donor_id: req.params.donor_id
}, },
include: [Causes, Regions, Organizations] include: [Cause, Region, Organization]
}) })
.then(grants => { .then(grants => {
res.status(200).json({ res.status(200).json({
grants, grants,
number_grants: grants.length number_items: grants.length
}); });
}) })
.catch(error => next(error)); .catch(error => next(error));
@@ -51,7 +51,7 @@ exports.findByDonorId = (req, res, next) => {
exports.delete = (req, res, next) => { exports.delete = (req, res, next) => {
const grant_id = req.params.grant_id; const grant_id = req.params.grant_id;
Grants.destroy({ where: { id: grant_id } }) Grant.destroy({ where: { id: grant_id } })
.then(result => { .then(result => {
var message = 'Already Deleted'; var message = 'Already Deleted';
if (result) { if (result) {
@@ -0,0 +1,16 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker');
const { Grant, Cause, Region, Organization } = db;
// FETCH all causes
exports.findAll = (req, res, next) => {
Cause.findAll()
.then(causes => {
res.status(200).json({
causes,
number_items: causes.length
});
})
.catch(error => next(error));
};
+5
View File
@@ -0,0 +1,5 @@
module.exports = {
cause: require('./cause.controller'),
region: require('./cause.controller'),
organization: require('./organization.controller')
};
@@ -0,0 +1,16 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker');
const { Grant, Cause, Region, Organization } = db;
// FETCH all organizations
exports.findAll = (req, res, next) => {
Organization.findAll()
.then(organizations => {
res.status(200).json({
organizations,
number_items: organizations.length
});
})
.catch(error => next(error));
};
@@ -0,0 +1,16 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker');
const { Grant, Cause, Region, Organization } = db;
// FETCH all regions
exports.findAll = (req, res, next) => {
Region.findAll()
.then(regions => {
res.status(200).json({
regions,
number_items: regions.length
});
})
.catch(error => next(error));
};
@@ -1,5 +1,5 @@
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Causes = sequelize.define('causes', { const Cause = sequelize.define('causes', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
@@ -7,5 +7,5 @@ module.exports = (sequelize, DataTypes) => {
}, },
name: { type: DataTypes.STRING, allowNull: false } name: { type: DataTypes.STRING, allowNull: false }
}); });
return Causes; return Cause;
}; };
@@ -1,5 +1,5 @@
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Donors = sequelize.define('donors', { const Donor = sequelize.define('donors', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
@@ -32,5 +32,5 @@ module.exports = (sequelize, DataTypes) => {
state: DataTypes.STRING, state: DataTypes.STRING,
country: DataTypes.STRING country: DataTypes.STRING
}); });
return Donors; return Donor;
}; };
@@ -1,5 +1,5 @@
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Grants = sequelize.define('grants', { const Grant = sequelize.define('grants', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
@@ -30,5 +30,5 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false allowNull: false
} }
}); });
return Grants; return Grant;
}; };
@@ -1,5 +1,5 @@
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Organizations = sequelize.define('organizations', { const Organization = sequelize.define('organizations', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
@@ -10,5 +10,5 @@ module.exports = (sequelize, DataTypes) => {
primary_cause: { type: DataTypes.STRING, allowNull: false }, primary_cause: { type: DataTypes.STRING, allowNull: false },
primary_region: { type: DataTypes.STRING, allowNull: false } primary_region: { type: DataTypes.STRING, allowNull: false }
}); });
return Organizations; return Organization;
}; };
@@ -1,5 +1,5 @@
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Regions = sequelize.define('regions', { const Region = sequelize.define('regions', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
@@ -7,5 +7,5 @@ module.exports = (sequelize, DataTypes) => {
}, },
name: { type: DataTypes.STRING, allowNull: false } name: { type: DataTypes.STRING, allowNull: false }
}); });
return Regions; return Region;
}; };
+36
View File
@@ -0,0 +1,36 @@
const express = require('express'),
router = express.Router(),
checkAuth = require('../middleware/check-auth');
const donor = require('../controllers/donor/donor.controller.js');
const grant = require('../controllers/donor/grant.controller.js');
// Signup route
router.post('/', donor.create);
// Retrieve all Donors
router.get('/', checkAuth, donor.findAll);
// Retrieve grants with causes and regions and charities details by donor_id
router.get('/grants/:donor_id', checkAuth, grant.findByDonorId);
/** Create grants with following body:
* - list of id's of selected causes and regions
* - FINAL list of id's of selected organizations
* - donor_id
* */
router.post('/grants/:donor_id', checkAuth, grant.create);
// Delete a grant of a donor
router.delete('/grants/:grant_id', checkAuth, grant.delete);
// // Retrieve a single Donor by Id
// router.get('/:donor_id', donor.findById);
// // Delete a Donor with Id
// router.delete('/:donor_id', donor.delete);
// // Update a Donor with Id
// router.put('/api/donors/:donor_id', donor.update);
module.exports = router;
-36
View File
@@ -1,36 +0,0 @@
const express = require('express'),
router = express.Router(),
checkAuth = require('../middleware/check-auth');
const donors = require('../controllers/donors/donors.controller.js');
const grants = require('../controllers/donors/grants.controller.js');
// Signup route
router.post('/', donors.create);
// Retrieve all Donors
router.get('/', checkAuth, donors.findAll);
// Retrieve grants with causes and regions and charities details by donor_id
router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
/** Create grants with following body:
* - list of id's of selected causes and regions
* - FINAL list of id's of selected organizations
* - donor_id
* */
router.post('/grants/:donor_id', checkAuth, grants.create);
// Delete a grant of a donor
router.delete('/grants/:grant_id', checkAuth, grants.delete);
// // Retrieve a single Donor by Id
// router.get('/:donor_id', donors.findById);
// // Delete a Donor with Id
// router.delete('/:donor_id', donors.delete);
// // Update a Donor with Id
// router.put('/api/donors/:donor_id', donors.update);
module.exports = router;
+15
View File
@@ -0,0 +1,15 @@
const express = require('express'),
router = express.Router();
const controllers = require('../controllers/general');
// Retrieve all causes
router.get('/causes', controllers.cause.findAll);
// Retrieve all regions
router.get('/regions', controllers.region.findAll);
// Retrieve all organizations
router.get('/organizations', controllers.organization.findAll);
module.exports = router;
+4 -1
View File
@@ -48,8 +48,11 @@ app.get('/', function(req, res, next) {
}); });
//adding routes to Express app //adding routes to Express app
app.use('/api/donors', require('./app/routes/donors.route.js')); app.use('/api/donors', require('./app/routes/donor.route.js'));
//auth route
app.use('/api/auth', require('./app/routes/auth.route.js')); app.use('/api/auth', require('./app/routes/auth.route.js'));
//general routes
app.use('/api', require('./app/routes/general.route.js'));
//404 not found error handling on any other routes //404 not found error handling on any other routes
app.use((req, res, next) => { app.use((req, res, next) => {