organize general routes
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
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',
|
||||
foreignKey: 'grant_id',
|
||||
otherKey: 'cause_id'
|
||||
});
|
||||
|
||||
Grants.belongsToMany(Regions, {
|
||||
Grant.belongsToMany(Region, {
|
||||
through: 'grants_regions',
|
||||
foreignKey: 'grant_id',
|
||||
otherKey: 'region_id'
|
||||
});
|
||||
|
||||
Grants.belongsToMany(Organizations, {
|
||||
Grant.belongsToMany(Organization, {
|
||||
through: 'grants_organizations',
|
||||
foreignKey: 'grant_id',
|
||||
otherKey: 'organization_id'
|
||||
|
||||
+15
-15
@@ -1,9 +1,9 @@
|
||||
const Sequelize = require('sequelize'),
|
||||
DonorsModel = require('../models/donors.model.js'),
|
||||
GrantsModel = require('../models/grants.model.js'),
|
||||
CausesModel = require('../models/causes.model.js'),
|
||||
RegionsModel = require('../models/regions.model.js'),
|
||||
OrganizationsModel = require('../models/organizations.model.js'),
|
||||
DonorModel = require('../models/donor.model.js'),
|
||||
GrantModel = require('../models/grant.model.js'),
|
||||
CauseModel = require('../models/cause.model.js'),
|
||||
RegionModel = require('../models/region.model.js'),
|
||||
OrganizationModel = require('../models/organization.model.js'),
|
||||
associate = require('./associate');
|
||||
|
||||
const connection_uri = process.env.JAWSDB_MARIA_URL;
|
||||
@@ -33,16 +33,16 @@ const sequelize = new Sequelize(connection_uri, {
|
||||
const db = {};
|
||||
|
||||
//creating tables/models from imported functions
|
||||
const Donors = DonorsModel(sequelize, Sequelize);
|
||||
const Grants = GrantsModel(sequelize, Sequelize);
|
||||
const Causes = CausesModel(sequelize, Sequelize);
|
||||
const Regions = RegionsModel(sequelize, Sequelize);
|
||||
const Organizations = OrganizationsModel(sequelize, Sequelize);
|
||||
db.Donors = Donors;
|
||||
db.Grants = Grants;
|
||||
db.Causes = Causes;
|
||||
db.Regions = Regions;
|
||||
db.Organizations = Organizations;
|
||||
const Donor = DonorModel(sequelize, Sequelize);
|
||||
const Grant = GrantModel(sequelize, Sequelize);
|
||||
const Cause = CauseModel(sequelize, Sequelize);
|
||||
const Region = RegionModel(sequelize, Sequelize);
|
||||
const Organization = OrganizationModel(sequelize, Sequelize);
|
||||
db.Donor = Donor;
|
||||
db.Grant = Grant;
|
||||
db.Cause = Cause;
|
||||
db.Region = Region;
|
||||
db.Organization = Organization;
|
||||
|
||||
// make associations with created models
|
||||
associate(db);
|
||||
|
||||
@@ -3,11 +3,11 @@ const db = require('../config/db.config.js'),
|
||||
jwt = require('jsonwebtoken'),
|
||||
errorMaker = require('../helpers/error.maker');
|
||||
|
||||
const Donors = db.Donors;
|
||||
const Donor = db.Donor;
|
||||
|
||||
// Find a Donor by email + login with JWT
|
||||
exports.login = (req, res, next) => {
|
||||
Donors.findAll({
|
||||
Donor.findAll({
|
||||
where: {
|
||||
email: req.body.email
|
||||
}
|
||||
|
||||
+8
-8
@@ -2,12 +2,12 @@ const db = require('../../config/db.config.js'),
|
||||
bcrypt = require('bcrypt-nodejs'),
|
||||
errorMaker = require('../../helpers/error.maker');
|
||||
|
||||
const Donors = db.Donors;
|
||||
const Donor = db.Donor;
|
||||
|
||||
// Create/post a Donor
|
||||
exports.create = (req, res, next) => {
|
||||
// see if user already in db
|
||||
Donors.findAll({
|
||||
Donor.findAll({
|
||||
where: {
|
||||
email: req.body.email
|
||||
}
|
||||
@@ -22,7 +22,7 @@ exports.create = (req, res, next) => {
|
||||
if (error) {
|
||||
return next(error);
|
||||
} else {
|
||||
Donors.create({
|
||||
Donor.create({
|
||||
first_name: req.body.first_name,
|
||||
middle_name: req.body.middle_name,
|
||||
last_name: req.body.last_name,
|
||||
@@ -50,9 +50,9 @@ exports.create = (req, res, next) => {
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
// FETCH all Donors
|
||||
// FETCH all Donor
|
||||
exports.findAll = (req, res, next) => {
|
||||
Donors.findAll().then(donors => {
|
||||
Donor.findAll().then(donors => {
|
||||
// Send all donors to Client
|
||||
res.status(200).json({
|
||||
donors,
|
||||
@@ -63,7 +63,7 @@ exports.findAll = (req, res, next) => {
|
||||
|
||||
// Find a Donor by Id
|
||||
exports.findById = (req, res) => {
|
||||
Donors.findById(req.params.donor_id).then(donor => {
|
||||
Donor.findById(req.params.donor_id).then(donor => {
|
||||
res.send(donor);
|
||||
});
|
||||
};
|
||||
@@ -71,7 +71,7 @@ exports.findById = (req, res) => {
|
||||
// Delete a Donor by Id
|
||||
exports.delete = (req, res) => {
|
||||
const id = req.params.donor_id;
|
||||
Donors.destroy({
|
||||
Donor.destroy({
|
||||
where: { id: id }
|
||||
}).then(() => {
|
||||
res.status(200).send('deleted successfully a donor with id = ' + id);
|
||||
@@ -81,7 +81,7 @@ exports.delete = (req, res) => {
|
||||
// // Update a Donor
|
||||
// exports.update = (req, res) => {
|
||||
// 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} }
|
||||
// ).then(() => {
|
||||
// res.status(200).send("updated successfully a donor with id = " + id);
|
||||
+6
-6
@@ -1,14 +1,14 @@
|
||||
const db = require('../../config/db.config.js'),
|
||||
errorMaker = require('../../helpers/error.maker');
|
||||
|
||||
const { Grants, Causes, Regions, Organizations } = db;
|
||||
const { Grant, Cause, Region, Organization } = db;
|
||||
|
||||
// Create a grant for certain donor
|
||||
exports.create = (req, res, next) => {
|
||||
const donor_id = req.params.donor_id;
|
||||
const { name, amount, monthly, causes, regions, organizations } = req.body;
|
||||
|
||||
Grants.create({
|
||||
Grant.create({
|
||||
donor_id,
|
||||
name,
|
||||
amount,
|
||||
@@ -33,16 +33,16 @@ exports.create = (req, res, next) => {
|
||||
|
||||
// Find grants with causes, regions, and organizations by donor_id
|
||||
exports.findByDonorId = (req, res, next) => {
|
||||
Grants.findAll({
|
||||
Grant.findAll({
|
||||
where: {
|
||||
donor_id: req.params.donor_id
|
||||
},
|
||||
include: [Causes, Regions, Organizations]
|
||||
include: [Cause, Region, Organization]
|
||||
})
|
||||
.then(grants => {
|
||||
res.status(200).json({
|
||||
grants,
|
||||
number_grants: grants.length
|
||||
number_items: grants.length
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
@@ -51,7 +51,7 @@ exports.findByDonorId = (req, res, next) => {
|
||||
exports.delete = (req, res, next) => {
|
||||
const grant_id = req.params.grant_id;
|
||||
|
||||
Grants.destroy({ where: { id: grant_id } })
|
||||
Grant.destroy({ where: { id: grant_id } })
|
||||
.then(result => {
|
||||
var message = 'Already Deleted';
|
||||
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));
|
||||
};
|
||||
@@ -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) => {
|
||||
const Causes = sequelize.define('causes', {
|
||||
const Cause = sequelize.define('causes', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
@@ -7,5 +7,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
},
|
||||
name: { type: DataTypes.STRING, allowNull: false }
|
||||
});
|
||||
return Causes;
|
||||
return Cause;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Donors = sequelize.define('donors', {
|
||||
const Donor = sequelize.define('donors', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
@@ -32,5 +32,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
state: DataTypes.STRING,
|
||||
country: DataTypes.STRING
|
||||
});
|
||||
return Donors;
|
||||
return Donor;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Grants = sequelize.define('grants', {
|
||||
const Grant = sequelize.define('grants', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
@@ -30,5 +30,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
allowNull: false
|
||||
}
|
||||
});
|
||||
return Grants;
|
||||
return Grant;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Organizations = sequelize.define('organizations', {
|
||||
const Organization = sequelize.define('organizations', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
@@ -10,5 +10,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
primary_cause: { type: DataTypes.STRING, allowNull: false },
|
||||
primary_region: { type: DataTypes.STRING, allowNull: false }
|
||||
});
|
||||
return Organizations;
|
||||
return Organization;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Regions = sequelize.define('regions', {
|
||||
const Region = sequelize.define('regions', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
@@ -7,5 +7,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
},
|
||||
name: { type: DataTypes.STRING, allowNull: false }
|
||||
});
|
||||
return Regions;
|
||||
return Region;
|
||||
};
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user