associations and adding more routes and structure
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = models => {
|
||||||
|
const { Donors, Grants } = models;
|
||||||
|
|
||||||
|
Donors.hasMany(Grants, { foreignKey: 'donor_id' });
|
||||||
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
const Sequelize = require('sequelize'),
|
const Sequelize = require('sequelize'),
|
||||||
DonorsModel = require('../models/donors.model.js'),
|
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;
|
const connection_uri = process.env.JAWSDB_MARIA_URL;
|
||||||
|
|
||||||
@@ -31,8 +32,11 @@ const db = {};
|
|||||||
//creating tables/models from imported functions
|
//creating tables/models from imported functions
|
||||||
const Donors = DonorsModel(sequelize, Sequelize);
|
const Donors = DonorsModel(sequelize, Sequelize);
|
||||||
const Grants = GrantsModel(sequelize, Sequelize);
|
const Grants = GrantsModel(sequelize, Sequelize);
|
||||||
db.donors = Donors;
|
db.Donors = Donors;
|
||||||
db.grants = Grants;
|
db.Grants = Grants;
|
||||||
|
|
||||||
|
// make associations with created models
|
||||||
|
associate(db);
|
||||||
|
|
||||||
db.Sequelize = Sequelize;
|
db.Sequelize = Sequelize;
|
||||||
db.sequelize = sequelize;
|
db.sequelize = sequelize;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ 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 Donors = db.Donors;
|
||||||
|
|
||||||
// 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) => {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
const db = require('../config/db.config.js'),
|
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 Donors = db.Donors;
|
||||||
|
|
||||||
// Create/post a Donor
|
// Create/post a Donor
|
||||||
exports.create = (req, res, next) => {
|
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));
|
||||||
|
};
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -5,10 +5,20 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true
|
autoIncrement: true
|
||||||
},
|
},
|
||||||
name: DataTypes.STRING,
|
name: {
|
||||||
amount: DataTypes.INTEGER,
|
type: DataTypes.STRING,
|
||||||
last_payment: DataTypes.DATE,
|
allowNull: false
|
||||||
monthly: DataTypes.TINYINT(1),
|
},
|
||||||
|
amount: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
last_payment: {
|
||||||
|
type: DataTypes.DATE,
|
||||||
|
allowNull: false,
|
||||||
|
default: new Date()
|
||||||
|
},
|
||||||
|
monthly: DataTypes.BOOLEAN,
|
||||||
num_causes: DataTypes.INTEGER,
|
num_causes: DataTypes.INTEGER,
|
||||||
num_regions: DataTypes.INTEGER
|
num_regions: DataTypes.INTEGER
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -2,7 +2,8 @@ const express = require('express'),
|
|||||||
router = express.Router(),
|
router = express.Router(),
|
||||||
checkAuth = require('../middleware/check-auth');
|
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
|
// Signup route
|
||||||
router.post('/', donors.create);
|
router.post('/', donors.create);
|
||||||
@@ -10,13 +11,23 @@ router.post('/', donors.create);
|
|||||||
// Retrieve all Donors
|
// Retrieve all Donors
|
||||||
router.get('/', checkAuth, donors.findAll);
|
router.get('/', checkAuth, donors.findAll);
|
||||||
|
|
||||||
// Retrieve a single Donor by Id
|
// Retrieve grants with cause and region and charity details by donor_id
|
||||||
router.get('/:DonorId', donors.findById);
|
router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
|
||||||
|
|
||||||
// Delete a Donor with Id
|
/** Create grants with following body:
|
||||||
router.delete('/:DonorId', donors.delete);
|
* - 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
|
// // Update a Donor with Id
|
||||||
// router.put('/api/donors/:DonorId', donors.update);
|
// router.put('/api/donors/:donor_id', donors.update);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ const express = require('express'),
|
|||||||
port = process.env.PORT,
|
port = process.env.PORT,
|
||||||
db = require('./app/config/db.config.js');
|
db = require('./app/config/db.config.js');
|
||||||
|
|
||||||
console.log(process.env.JAWSDB_MARIA_URL);
|
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
//handle CORS errors
|
//handle CORS errors
|
||||||
|
|||||||
Reference in New Issue
Block a user