From 86dd4c8cdb1f33d9272258c31b0f53c3a9846447 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Tue, 15 Jan 2019 23:14:05 -0800 Subject: [PATCH] model set up and associations --- app/config/associate.js | 20 +++++++++++++++++++- app/config/db.config.js | 9 +++++++++ app/controllers/donors/grants.controller.js | 1 + app/models/donors.model.js | 4 +++- app/models/grants.model.js | 15 ++++++++++++--- app/models/organization.model.js | 0 app/models/organizations.model.js | 14 ++++++++++++++ app/routes/donors.route.js | 2 +- 8 files changed, 59 insertions(+), 6 deletions(-) delete mode 100644 app/models/organization.model.js create mode 100644 app/models/organizations.model.js diff --git a/app/config/associate.js b/app/config/associate.js index 84b4bb8..259dec6 100644 --- a/app/config/associate.js +++ b/app/config/associate.js @@ -1,5 +1,23 @@ module.exports = models => { - const { Donors, Grants } = models; + const { Donors, Grants, Causes, Regions, Organizations } = models; Donors.hasMany(Grants, { foreignKey: 'donor_id' }); + + Grants.belongsToMany(Causes, { + through: 'grants_causes', + foreignKey: 'grant_id', + otherKey: 'cause_id' + }); + + Grants.belongsToMany(Regions, { + through: 'grants_regions', + foreignKey: 'grant_id', + otherKey: 'region_id' + }); + + Grants.belongsToMany(Organizations, { + through: 'grants_organizations', + foreignKey: 'grant_id', + otherKey: 'organization_id' + }); }; diff --git a/app/config/db.config.js b/app/config/db.config.js index 6d5ab3c..8c3c1c7 100644 --- a/app/config/db.config.js +++ b/app/config/db.config.js @@ -1,6 +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'), associate = require('./associate'); const connection_uri = process.env.JAWSDB_MARIA_URL; @@ -32,8 +35,14 @@ 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; // make associations with created models associate(db); diff --git a/app/controllers/donors/grants.controller.js b/app/controllers/donors/grants.controller.js index 7a06e8a..41c169e 100644 --- a/app/controllers/donors/grants.controller.js +++ b/app/controllers/donors/grants.controller.js @@ -22,6 +22,7 @@ exports.findByDonorId = (req, res, next) => { } }) .then(grants => { + const grants_full_info = grants.map(grant => {}); res.status(200).json({ grants, number_grants: grants.length diff --git a/app/models/donors.model.js b/app/models/donors.model.js index fc44809..57520a9 100644 --- a/app/models/donors.model.js +++ b/app/models/donors.model.js @@ -16,7 +16,9 @@ module.exports = (sequelize, DataTypes) => { }, email: { type: DataTypes.STRING, - isEmail: true, + validate: { + isEmail: true + }, allowNull: false }, password: { diff --git a/app/models/grants.model.js b/app/models/grants.model.js index 75bdc6c..6817128 100644 --- a/app/models/grants.model.js +++ b/app/models/grants.model.js @@ -18,9 +18,18 @@ module.exports = (sequelize, DataTypes) => { allowNull: false, default: new Date() }, - monthly: DataTypes.BOOLEAN, - num_causes: DataTypes.INTEGER, - num_regions: DataTypes.INTEGER + monthly: { + type: DataTypes.BOOLEAN, + allowNull: false + }, + num_causes: { + type: DataTypes.INTEGER, + allowNull: false + }, + num_regions: { + type: DataTypes.INTEGER, + allowNull: false + } }); return Grants; }; diff --git a/app/models/organization.model.js b/app/models/organization.model.js deleted file mode 100644 index e69de29..0000000 diff --git a/app/models/organizations.model.js b/app/models/organizations.model.js new file mode 100644 index 0000000..cd153a4 --- /dev/null +++ b/app/models/organizations.model.js @@ -0,0 +1,14 @@ +module.exports = (sequelize, DataTypes) => { + const Organizations = sequelize.define('organizations', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + name: { type: DataTypes.STRING, allowNull: false }, + short_description: DataTypes.STRING, + primary_cause: { type: DataTypes.STRING, allowNull: false }, + primary_region: { type: DataTypes.STRING, allowNull: false } + }); + return Organizations; +}; diff --git a/app/routes/donors.route.js b/app/routes/donors.route.js index 08eb559..fd2499a 100644 --- a/app/routes/donors.route.js +++ b/app/routes/donors.route.js @@ -11,7 +11,7 @@ router.post('/', donors.create); // Retrieve all Donors router.get('/', checkAuth, donors.findAll); -// Retrieve grants with cause and region and charity details by donor_id +// 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: