associations and adding more routes and structure

This commit is contained in:
Arjun Patel
2019-01-15 22:08:54 -08:00
parent b453c78c00
commit a4e4394329
11 changed files with 100 additions and 19 deletions
+5
View File
@@ -0,0 +1,5 @@
module.exports = models => {
const { Donors, Grants } = models;
Donors.hasMany(Grants, { foreignKey: 'donor_id' });
};
+7 -3
View File
@@ -1,6 +1,7 @@
const Sequelize = require('sequelize'),
DonorsModel = require('../models/donors.model.js'),
GrantsModel = require('../models/grants.model.js');
GrantsModel = require('../models/grants.model.js'),
associate = require('./associate');
const connection_uri = process.env.JAWSDB_MARIA_URL;
@@ -31,8 +32,11 @@ const db = {};
//creating tables/models from imported functions
const Donors = DonorsModel(sequelize, Sequelize);
const Grants = GrantsModel(sequelize, Sequelize);
db.donors = Donors;
db.grants = Grants;
db.Donors = Donors;
db.Grants = Grants;
// make associations with created models
associate(db);
db.Sequelize = Sequelize;
db.sequelize = sequelize;
+1 -1
View File
@@ -3,7 +3,7 @@ const db = require('../config/db.config.js'),
jwt = require('jsonwebtoken'),
errorMaker = require('../helpers/error.maker');
const Donors = db.donors;
const Donors = db.Donors;
// Find a Donor by email + login with JWT
exports.login = (req, res, next) => {
@@ -1,8 +1,8 @@
const db = require('../config/db.config.js'),
const db = require('../../config/db.config.js'),
bcrypt = require('bcrypt-nodejs'),
errorMaker = require('../helpers/error.maker');
errorMaker = require('../../helpers/error.maker');
const Donors = db.donors;
const Donors = db.Donors;
// Create/post a Donor
exports.create = (req, res, next) => {
@@ -0,0 +1,31 @@
const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker');
const Grants = db.Grants;
// Create a grant for certain donor
exports.create = (req, res, next) => {
const donor_id = req.params.donor_id;
const { name, amount, causes, regions, organizations } = req.body;
Grants.create({
name,
amount
});
};
// Find grants with cause and region and charity details by Id
exports.findByDonorId = (req, res, next) => {
Grants.findAll({
where: {
donor_id: req.params.donor_id
}
})
.then(grants => {
res.status(200).json({
grants,
number_grants: grants.length
});
})
.catch(error => next(error));
};
+11
View File
@@ -0,0 +1,11 @@
module.exports = (sequelize, DataTypes) => {
const Causes = sequelize.define('causes', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: { type: DataTypes.STRING, allowNull: false }
});
return Causes;
};
+14 -4
View File
@@ -5,10 +5,20 @@ module.exports = (sequelize, DataTypes) => {
primaryKey: true,
autoIncrement: true
},
name: DataTypes.STRING,
amount: DataTypes.INTEGER,
last_payment: DataTypes.DATE,
monthly: DataTypes.TINYINT(1),
name: {
type: DataTypes.STRING,
allowNull: false
},
amount: {
type: DataTypes.INTEGER,
allowNull: false
},
last_payment: {
type: DataTypes.DATE,
allowNull: false,
default: new Date()
},
monthly: DataTypes.BOOLEAN,
num_causes: DataTypes.INTEGER,
num_regions: DataTypes.INTEGER
});
View File
+11
View File
@@ -0,0 +1,11 @@
module.exports = (sequelize, DataTypes) => {
const Regions = sequelize.define('regions', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: { type: DataTypes.STRING, allowNull: false }
});
return Regions;
};
+17 -6
View File
@@ -2,7 +2,8 @@ const express = require('express'),
router = express.Router(),
checkAuth = require('../middleware/check-auth');
const donors = require('../controllers/donors.controller.js');
const donors = require('../controllers/donors/donors.controller.js');
const grants = require('../controllers/donors/grants.controller.js');
// Signup route
router.post('/', donors.create);
@@ -10,13 +11,23 @@ router.post('/', donors.create);
// Retrieve all Donors
router.get('/', checkAuth, donors.findAll);
// Retrieve a single Donor by Id
router.get('/:DonorId', donors.findById);
// Retrieve grants with cause and region and charity details by donor_id
router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
// Delete a Donor with Id
router.delete('/:DonorId', donors.delete);
/** Create grants with following body:
* - list of id's selected causes and regions
* - FINAL list of id's of selected organizations
* - donor_id
* */
router.post('/grants/:donor_id', checkAuth, grants.create);
// // 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/:DonorId', donors.update);
// router.put('/api/donors/:donor_id', donors.update);
module.exports = router;
-2
View File
@@ -5,8 +5,6 @@ const express = require('express'),
port = process.env.PORT,
db = require('./app/config/db.config.js');
console.log(process.env.JAWSDB_MARIA_URL);
app.use(bodyParser.json());
//handle CORS errors