diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 04e10548..b8863f33 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -128,6 +128,80 @@ const captainController = { body: result, }) + } catch (error) { + console.log(error); + res.status(500).json({ + message: 'An error occured while retrieving data', + error + }) + } + }, + allCaptainsInUnitInfo: async (req, res) => { + try { + const { unitCaptainId } = req.body; + + // Make sure that the id is provided (not undefined) + if (!unitCaptainId){ + return res.status(404).json({ + error: "Enter a valid unit captain id" + }) + } + + // Query to get the id + const result = await db.query(` + SELECT C.* + FROM "Captain" AS C, "Sector" AS S + WHERE S."unitCaptainId" = $1 AND C."rSectorBaseName" = S."baseName" AND C."rSectorSuffixName" = S."suffixName";`, + [unitCaptainId] + ) + + // If there is no result found, return 404 not found error (This might not be an error) + if (!result.rows.length) + { + return res.status(404).json({ + error: "Captains Not Found" + }) + } + + // Return the data + res.status(200).json({ + message: "Successful retrieval", + body: result, + }); + + } catch (error) { + console.log(error); + res.status(500).json({ + message: 'An error occured while retrieving data', + error + }) + } + }, + allCaptainsInUnitCount: async (req, res) => { + try { + const { unitCaptainId } = req.body; + + // Make sure that the id is provided (not undefined) + if (!unitCaptainId){ + return res.status(404).json({ + error: "Enter a valid unit captain id" + }) + } + + // Query to get the id + const result = await db.query(` + SELECT COUNT(*) + FROM "Captain" AS C, "Sector" AS S + WHERE S."unitCaptainId" = $1 AND C."rSectorBaseName" = S."baseName" AND C."rSectorSuffixName" = S."suffixName";`, + [unitCaptainId] + ) + + // Return the data + res.status(200).json({ + message: "Successful retrieval", + body: result, + }); + } catch (error) { console.log(error); res.status(500).json({ diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index 9173cb48..202b44b4 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -10,6 +10,8 @@ captainRouter.get('/allCaptains/count', captainController.allCaptainsCount) captainRouter.get('/captainsInSector/info', captainController.captainsInSectorInfo) captainRouter.get('/captainsInSector/count', captainController.captainsInSectorCount) captainRouter.get('/ceratinCaptain/info', captainController.captainInfo) +captainRouter.get('/allCaptainsInUnit/info', captainController.allCaptainsInUnitInfo) +captainRouter.get('/allCaptainsInUnit/count', captainController.allCaptainsInUnitCount) export default captainRouter \ No newline at end of file