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 };