Initial commit
TODO: change api.conf URL references to use environment variables and add these variables to the docker-compose configuration for host domain
This commit is contained in:
98
api/db/routes/Award.route.js
Normal file
98
api/db/routes/Award.route.js
Normal file
@@ -0,0 +1,98 @@
|
||||
|
||||
const award = require("../controllers/Award.controller.js");
|
||||
|
||||
const db = require("..");
|
||||
|
||||
var router = require("express").Router();
|
||||
|
||||
// Create a new Award
|
||||
router.post("/", award.create);
|
||||
|
||||
// GET AWARD
|
||||
router.get("/", async (req, res) => {
|
||||
const id = req.query.id;
|
||||
if (!id) {
|
||||
return db.Award.findAll()
|
||||
.then(results => res.send(results))
|
||||
}
|
||||
|
||||
return db.Award.findByPk(id)
|
||||
.then(async (award) => {
|
||||
if (award === null) {
|
||||
res.status(404).send({
|
||||
message: `Award with id=${id} was not found!`
|
||||
});
|
||||
return
|
||||
}
|
||||
res.send(award)
|
||||
})
|
||||
.catch(err => {
|
||||
res.status(500).send({
|
||||
message:
|
||||
err.message || "Some error occurred while retrieving awards."
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
// GET AWARD DETAILS
|
||||
router.get("/details", async (req, res) => {
|
||||
const id = req.query.id;
|
||||
if (!id) {
|
||||
res.status(400).send({
|
||||
message: "Award id cannot be empty!"
|
||||
});
|
||||
return
|
||||
}
|
||||
return db.Award.findByPk(id, {
|
||||
include: [
|
||||
'awardHolders',
|
||||
'coursesRequired'
|
||||
]
|
||||
})
|
||||
.then(async (award) => {
|
||||
if (award === null) {
|
||||
res.status(404).send({
|
||||
message: `Award with id=${id} was not found!`
|
||||
});
|
||||
return
|
||||
}
|
||||
res.send(award)
|
||||
})
|
||||
.catch(err => {
|
||||
res.status(500).send({
|
||||
message:
|
||||
err.message || "Some error occurred while retrieving awards."
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
// GET CATEGORIES
|
||||
router.get("/categories", async (req, res) => {
|
||||
return db.Award.findAll({
|
||||
attributes: ['category'],
|
||||
group: ['category']
|
||||
})
|
||||
.then(async (awardCategories) => {
|
||||
if (awardCategories === null) {
|
||||
res.status(404).send({
|
||||
message: `Award categories were not found!`
|
||||
});
|
||||
return
|
||||
}
|
||||
res.send(awardCategories.map(awardCategory => awardCategory.category))
|
||||
})
|
||||
.catch(err => {
|
||||
res.status(500).send({
|
||||
message:
|
||||
err.message || "Some error occurred while retrieving award categories."
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
apiPath: "/api/awards",
|
||||
apiRouter: router
|
||||
};
|
||||
Reference in New Issue
Block a user