working create grants, getall, and delete with clean methods

This commit is contained in:
Arjun Patel
2019-01-18 18:02:04 -08:00
parent f046a9f605
commit b0597a99e2
7 changed files with 74 additions and 78 deletions
-3
View File
@@ -4,21 +4,18 @@ module.exports = models => {
Donors.hasMany(Grants, { foreignKey: 'donor_id' });
Grants.belongsToMany(Causes, {
as: 'GrantsCauses',
through: 'grants_causes',
foreignKey: 'grant_id',
otherKey: 'cause_id'
});
Grants.belongsToMany(Regions, {
as: 'GrantsRegions',
through: 'grants_regions',
foreignKey: 'grant_id',
otherKey: 'region_id'
});
Grants.belongsToMany(Organizations, {
as: 'GrantsOrganizations',
through: 'grants_organizations',
foreignKey: 'grant_id',
otherKey: 'organization_id'
+1 -1
View File
@@ -38,7 +38,7 @@ exports.create = (req, res, next) => {
.then(donor => {
// Send created donor to client
return res.status(201).json({
message: 'User created',
message: 'Donor created',
donor
});
})
+40 -73
View File
@@ -6,94 +6,61 @@ const { Grants, Causes, Regions, Organizations } = db;
// Create a grant for certain donor
exports.create = (req, res, next) => {
const donor_id = req.params.donor_id;
const {
name,
amount,
monthly,
causes_ids,
regions_ids,
organizations_ids
} = req.body;
const { name, amount, monthly, causes, regions, organizations } = req.body;
Grants.create({
donor_id,
name,
amount,
monthly,
num_causes: causes_ids.length,
num_regions: regions_ids.length
num_causes: causes.length,
num_regions: regions.length
})
.then(grant => {
const grants_causes_rows = causes_ids.map(cause_id => {
return { cause_id, grant_id: grant.id };
return Promise.all([
grant.addCauses(causes),
grant.addRegions(regions),
grant.addOrganizations(organizations)
]).then(result => result);
})
.then(result => {
res.status(201).json({
message: 'Grant Created'
});
const grants_regions_rows = regions_ids.map(region_id => {
return { region_id, grant_id: grant.id };
});
const grants_organizations_rows = organizations_ids.map(
organization_id => {
return { organization_id, grant_id: grant.id };
}
);
Promise.all([
grants_causes_rows,
grants_regions_rows,
grants_organizations_rows
])
.then(results => {
Causes.bulkCreate(results[0], { raw: true }).then(causes => {
grant.addCauses(causes);
// console.log(causes);
});
console.log(results[1]);
Regions.bulkCreate(results[1], { raw: true }).then(regions => {
grant.addCauses(regions);
// console.log(regions);
});
Organizations.bulkCreate(results[2], { raw: true }).then(
organizations => {
grant.addCauses(organizations);
// console.log(organizations);
}
);
})
.catch(error => next(error));
})
.catch(error => next(error));
};
// Find grants with cause and region and charity details by donor_id
// Find grants with causes, regions, and organizations by donor_id
exports.findByDonorId = (req, res, next) => {
Grants.findAll({
where: {
donor_id: req.params.donor_id
}
}).then(grants => {
Promise.all(
grants.map(grant => {
const causes = grant.getCauses().then(causes => causes),
regions = grant.getRegions().then(regions => regions),
organizations = grant
.getOrganizations()
.then(organizations => organizations);
return Promise.all([causes, regions, organizations]).then(data => {
return {
grant,
causes: data[0],
regions: data[1],
organizations: data[2]
};
});
})
)
.then(list_grants => {
res.status(200).json({
grants: list_grants,
number_grants: list_grants.length
});
})
.catch(error => next(error));
});
},
include: [Causes, Regions, Organizations]
})
.then(grants => {
res.status(200).json({
grants,
number_grants: grants.length
});
})
.catch(error => next(error));
};
exports.delete = (req, res, next) => {
const grant_id = req.params.grant_id;
Grants.destroy({ where: { id: grant_id } })
.then(result => {
var message = 'Already Deleted';
if (result) {
message = 'Grant Deleted';
}
res.status(201).json({
message,
result
});
})
.catch(error => next(error));
};
+3
View File
@@ -21,6 +21,9 @@ router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
* */
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);