Files
17th-Battalion-Tracker/api/db/routes/Rank.route.js
IndigoFox 9f2473801c Initial commit
TODO: change api.conf URL references to use environment variables and add these variables to the docker-compose configuration for host domain
2023-03-28 00:08:50 -07:00

95 lines
2.0 KiB
JavaScript

const rank = require("../controllers/Rank.controller.js");
const db = require("..");
var router = require("express").Router();
// Create a new Rank
router.post("/", rank.create);
// GET RANK
router.get("/", async (req, res) => {
const id = req.query.id;
if (!id) {
return db.Rank.findAll()
.then(results => res.send(results))
}
return db.Rank.findByPk(id)
.then(async (rank) => {
if (rank === null) {
res.status(404).send({
message: `Rank with id=${id} was not found!`
});
return
}
res.send(rank)
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving ranks."
})
})
});
// GET RANK DETAILS
router.get("/details", async (req, res) => {
const id = req.query.id;
if (!id) {
res.status(400).send({
message: "Rank id cannot be empty!"
});
return
}
return db.Rank.findByPk(id, {
include: [
'members',
]
})
.then(async (rank) => {
if (rank === null) {
res.status(404).send({
message: `Rank with id=${id} was not found!`
});
return
}
res.send(rank)
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving ranks."
})
})
});
// GET CATEGORIES
router.get("/categories", async (req, res) => {
return db.Rank.findAll({
attributes: ['category'],
group: ['category']
})
.then(async (rankCategories) => {
if (rankCategories === null) {
res.status(404).send({
message: `Rank categories were not found!`
});
return
}
res.send(rankCategories.map(rankCategory => rankCategory.category))
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving rank categories."
})
})
});
module.exports = {
apiPath: "/api/ranks",
apiRouter: router
};