From 53e8f2058dabebfd558fb4be9298922077421d5d Mon Sep 17 00:00:00 2001 From: akram Date: Sun, 24 Dec 2023 12:34:29 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8feat:=20Implement=20getting=20all?= =?UTF-8?q?=20regular=20captains=20info=20in=20a=20certain=20unit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/captain.controller.js | 41 ++++++++++++++++++++++++ server/routes/captain.route.js | 1 + 2 files changed, 42 insertions(+) diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 04e10548..84d6467a 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -128,6 +128,47 @@ 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({ diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index 9173cb48..572b2b55 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -10,6 +10,7 @@ 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) export default captainRouter \ No newline at end of file From 3c12b98a8949a17988f39067695b55aa4f90268e Mon Sep 17 00:00:00 2001 From: akram Date: Sun, 24 Dec 2023 12:37:22 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8feat:=20Implement=20getting=20all?= =?UTF-8?q?=20regular=20captains=20=20ount=20in=20a=20certain=20unit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/captain.controller.js | 33 ++++++++++++++++++++++++ server/routes/captain.route.js | 1 + 2 files changed, 34 insertions(+) diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 84d6467a..b8863f33 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -169,6 +169,39 @@ const captainController = { 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 572b2b55..202b44b4 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -11,6 +11,7 @@ captainRouter.get('/captainsInSector/info', captainController.captainsInSectorIn 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