diff --git a/app/config/associate.js b/app/config/associate.js index 259dec6..95ecf1c 100644 --- a/app/config/associate.js +++ b/app/config/associate.js @@ -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' diff --git a/app/config/db.config.js b/app/config/db.config.js index 8c3c1c7..a7e4a70 100644 --- a/app/config/db.config.js +++ b/app/config/db.config.js @@ -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); diff --git a/app/controllers/auth.controller.js b/app/controllers/auth.controller.js index fde83fc..83981ea 100644 --- a/app/controllers/auth.controller.js +++ b/app/controllers/auth.controller.js @@ -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 } diff --git a/app/controllers/donors/donors.controller.js b/app/controllers/donor/donor.controller.js similarity index 86% rename from app/controllers/donors/donors.controller.js rename to app/controllers/donor/donor.controller.js index c0c5816..7a87242 100644 --- a/app/controllers/donors/donors.controller.js +++ b/app/controllers/donor/donor.controller.js @@ -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); diff --git a/app/controllers/donors/grants.controller.js b/app/controllers/donor/grant.controller.js similarity index 85% rename from app/controllers/donors/grants.controller.js rename to app/controllers/donor/grant.controller.js index 986f8df..30c003e 100644 --- a/app/controllers/donors/grants.controller.js +++ b/app/controllers/donor/grant.controller.js @@ -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) { diff --git a/app/controllers/general/cause.controller.js b/app/controllers/general/cause.controller.js new file mode 100644 index 0000000..73b5923 --- /dev/null +++ b/app/controllers/general/cause.controller.js @@ -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)); +}; diff --git a/app/controllers/general/index.js b/app/controllers/general/index.js new file mode 100644 index 0000000..29d05c1 --- /dev/null +++ b/app/controllers/general/index.js @@ -0,0 +1,5 @@ +module.exports = { + cause: require('./cause.controller'), + region: require('./cause.controller'), + organization: require('./organization.controller') +}; diff --git a/app/controllers/general/organization.controller.js b/app/controllers/general/organization.controller.js new file mode 100644 index 0000000..887553a --- /dev/null +++ b/app/controllers/general/organization.controller.js @@ -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)); +}; diff --git a/app/controllers/general/region.controller.js b/app/controllers/general/region.controller.js new file mode 100644 index 0000000..c4b4db0 --- /dev/null +++ b/app/controllers/general/region.controller.js @@ -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)); +}; diff --git a/app/models/causes.model.js b/app/models/cause.model.js similarity index 75% rename from app/models/causes.model.js rename to app/models/cause.model.js index 266d378..1499e3e 100644 --- a/app/models/causes.model.js +++ b/app/models/cause.model.js @@ -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; }; diff --git a/app/models/donors.model.js b/app/models/donor.model.js similarity index 91% rename from app/models/donors.model.js rename to app/models/donor.model.js index 57520a9..defba63 100644 --- a/app/models/donors.model.js +++ b/app/models/donor.model.js @@ -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; }; diff --git a/app/models/grants.model.js b/app/models/grant.model.js similarity index 89% rename from app/models/grants.model.js rename to app/models/grant.model.js index 705d63e..076cc47 100644 --- a/app/models/grants.model.js +++ b/app/models/grant.model.js @@ -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; }; diff --git a/app/models/organizations.model.js b/app/models/organization.model.js similarity index 81% rename from app/models/organizations.model.js rename to app/models/organization.model.js index cd153a4..0cb749b 100644 --- a/app/models/organizations.model.js +++ b/app/models/organization.model.js @@ -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; }; diff --git a/app/models/regions.model.js b/app/models/region.model.js similarity index 74% rename from app/models/regions.model.js rename to app/models/region.model.js index 443a410..a5980bd 100644 --- a/app/models/regions.model.js +++ b/app/models/region.model.js @@ -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; }; diff --git a/app/routes/donor.route.js b/app/routes/donor.route.js new file mode 100644 index 0000000..93c958c --- /dev/null +++ b/app/routes/donor.route.js @@ -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; diff --git a/app/routes/donors.route.js b/app/routes/donors.route.js deleted file mode 100644 index 0ac6d9a..0000000 --- a/app/routes/donors.route.js +++ /dev/null @@ -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; diff --git a/app/routes/general.route.js b/app/routes/general.route.js new file mode 100644 index 0000000..46fbd9a --- /dev/null +++ b/app/routes/general.route.js @@ -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; diff --git a/server.js b/server.js index e7f7984..d0cc6a3 100644 --- a/server.js +++ b/server.js @@ -48,8 +48,11 @@ app.get('/', function(req, res, next) { }); //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')); +//general routes +app.use('/api', require('./app/routes/general.route.js')); //404 not found error handling on any other routes app.use((req, res, next) => {