diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index a8f3bfe3..24f68fe3 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -1,7 +1,7 @@ import db from '../database/db.js' const captainController = { - allCaptainsInfo: async (req, res) => { + getAllCaptains: async (req, res) => { try { // Query on the database to get the captains info const result = await db.query(`SELECT * FROM "Captain"`) @@ -10,6 +10,7 @@ const captainController = { res.status(200).json({ message: 'Successful retrieval', body: result.rows, + count: result.rowCount, }) } catch (error) { console.log(error) @@ -19,42 +20,22 @@ const captainController = { }) } }, - allCaptainsCount: async (req, res) => { + getCaptainsInSector: async (req, res) => { try { - // Query on the database to get the captains info - const result = await db.query( - `SELECT COUNT(*) AS count FROM "Captain"` - ) - - // Respond with the data retrieved and a successful retrieval message - res.status(200).json({ - message: 'Successful retrieval', - body: result.rows[0].count, - }) - } catch (error) { - console.log(error) - res.status(500).json({ - error: 'An error occured while retrieving the captains count', - body: error, - }) - } - }, - captainsInSectorInfo: async (req, res) => { - try { - // Extract sector base name and suffix name from the request body - const { rSectorBaseName, rSectorSuffixName } = req.body + const { baseName, suffixName } = req.params // Query on the database to get all the captains info in a specific sector const result = await db.query( `SELECT * FROM "Captain" WHERE "rSectorBaseName" = $1 AND "rSectorSuffixName" = $2`, - [rSectorBaseName, rSectorSuffixName] + [baseName, suffixName] ) res.status(200).json({ message: 'Successful retrieval', body: result.rows, + count: result.rowCount, }) } catch (error) { console.log(error) @@ -64,22 +45,25 @@ const captainController = { }) } }, - captainsInSectorCount: async (req, res) => { + getCaptainsInUnit: async (req, res) => { try { - // Extract sector base name and suffix name from the request body - const { rSectorBaseName, rSectorSuffixName } = req.body + const { unitCaptainId } = req.params - // Query on the database to get all the captains count in a specific sector + // Query to get the id const result = await db.query( - `SELECT COUNT(*) AS count - FROM "Captain" - WHERE "rSectorBaseName" = $1 AND "rSectorSuffixName" = $2`, - [rSectorBaseName, rSectorSuffixName] + `SELECT C.* + 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.rows[0].count, + body: result.rows, + count: result.rowCount, }) } catch (error) { console.log(error) @@ -89,7 +73,7 @@ const captainController = { }) } }, - captainInfo: async (req, res) => { + getCaptain: async (req, res) => { try { // Extract the captain ID from the request params const { captainId } = req.params @@ -122,58 +106,6 @@ const captainController = { }) } }, - allCaptainsInUnitInfo: async (req, res) => { - try { - const { unitCaptainId } = req.params - - // 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] - ) - - // Return the data - res.status(200).json({ - message: 'Successful retrieval', - body: result.rows, - }) - } catch (error) { - console.log(error) - res.status(500).json({ - message: 'An error occured while retrieving data', - body: error, - }) - } - }, - allCaptainsInUnitCount: async (req, res) => { - try { - const { unitCaptainId } = req.params - - // Query to get the id - const result = await db.query( - `SELECT COUNT(*) AS 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.rows[0].count, - }) - } catch (error) { - console.log(error) - res.status(500).json({ - message: 'An error occured while retrieving data', - body: error, - }) - } - }, } export default captainController diff --git a/server/controllers/scout.controller.js b/server/controllers/scout.controller.js index 582e6fa1..e2dd91d6 100644 --- a/server/controllers/scout.controller.js +++ b/server/controllers/scout.controller.js @@ -1,31 +1,14 @@ import db from '../database/db.js' const scoutController = { - allScoutsCount: async (req, res) => { - try { - const result = await db.query( - `SELECT COUNT(*) AS count FROM "Scout";` - ) - - res.status(200).json({ - message: 'Successful retrieval', - body: result.rows[0].count, - }) - } catch (error) { - console.log(error) - res.status(500).json({ - message: 'An error occured while retrieving data', - body: error, - }) - } - }, - allScoutsInfo: async (req, res) => { + getAllScouts: async (req, res) => { try { const result = await db.query(`SELECT * FROM "Scout";`) res.status(200).json({ message: 'Successful retrieval', body: result.rows, + count: result.rowCount, }) } catch (error) { console.log(error) @@ -35,20 +18,21 @@ const scoutController = { }) } }, - scoutsInSectorInfo: async (req, res) => { + getScoutsInSector: async (req, res) => { try { - const { sectorBaseName, sectorSuffixName } = req.body + const { baseName, suffixName } = req.params const result = await db.query( `SELECT * FROM "Scout" WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2;`, - [sectorBaseName, sectorSuffixName] + [baseName, suffixName] ) res.status(200).json({ message: 'Successful retrieval', body: result.rows, + count: result.rowCount, }) } catch (error) { console.log(error) @@ -58,30 +42,7 @@ const scoutController = { }) } }, - scoutsInSectorCount: async (req, res) => { - try { - const { sectorBaseName, sectorSuffixName } = req.body - - const result = await db.query( - `SELECT COUNT(*) AS count - FROM "Scout" - WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2;`, - [sectorBaseName, sectorSuffixName] - ) - - res.status(200).json({ - message: 'Successful retrieval', - body: result.rows[0].count, - }) - } catch (error) { - console.log(error) - res.status(500).json({ - message: 'An error occured while retrieving data', - body: error, - }) - } - }, - allScoutsInUnitInfo: async (req, res) => { + getScoutsInUnit: async (req, res) => { try { const { unitCaptainId } = req.params @@ -97,6 +58,7 @@ const scoutController = { res.status(200).json({ message: 'Successful retrieval', body: result.rows, + count: result.rowCount, }) } catch (error) { console.log(error) @@ -106,32 +68,7 @@ const scoutController = { }) } }, - allScoutsInUnitCount: async (req, res) => { - try { - const { unitCaptainId } = req.params - - const result = await db.query( - `SELECT Count(*) - FROM "Scout" AS scout, "Sector" AS sector - WHERE sector."unitCaptainId" = $1 AND - scout."sectorBaseName" = sector."baseName" AND - scout."sectorSuffixName" = sector."suffixName";`, - [unitCaptainId] - ) - - res.status(200).json({ - message: 'Successful retrieval', - body: result.rows[0].count, - }) - } catch (error) { - console.log(error) - res.status(500).json({ - message: 'An error occured while retrieving data', - body: error, - }) - } - }, - certainScoutInfo: async (req, res) => { + getScout: async (req, res) => { try { const { scoutId } = req.params diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index b5f14ebf..3cedbead 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -1,17 +1,14 @@ -import { Router } from "express" -import captainController from "../controllers/captain.controller.js"; +import { Router } from 'express' +import captainController from '../controllers/captain.controller.js' +const captainRouter = Router() -const captainRouter = Router(); +captainRouter.get('/:id', captainController.getCaptain) +captainRouter.get('/all', captainController.getAllCaptains) +captainRouter.get('/unit/:unitCaptainId', captainController.getCaptainsInUnit) +captainRouter.get( + '/sector/:baseName/:suffixName', + captainController.getCaptainsInSector +) - -captainRouter.get('/:id', captainController.captainInfo) -captainRouter.get('/allCaptains/info', captainController.allCaptainsInfo) -captainRouter.get('/allCaptains/count', captainController.allCaptainsCount) -captainRouter.get('/captainsInSector/info', captainController.captainsInSectorInfo) -captainRouter.get('/captainsInSector/count', captainController.captainsInSectorCount) -captainRouter.get('/allCaptainsInUnit/info/:id', captainController.allCaptainsInUnitInfo) -captainRouter.get('/allCaptainsInUnit/count/:id', captainController.allCaptainsInUnitCount) - - -export default captainRouter \ No newline at end of file +export default captainRouter diff --git a/server/routes/finance.route.js b/server/routes/finance.route.js index c3f265d5..9323dc06 100644 --- a/server/routes/finance.route.js +++ b/server/routes/finance.route.js @@ -3,6 +3,10 @@ import financeController from '../controllers/finance.controller.js' import checkRankMiddleware from '../middlewares/checkRank.middleware.js' const financeRouter = Router() -financeRouter.get('/budget', checkRankMiddleware('general'), financeController.getBudget) +financeRouter.get( + '/', + checkRankMiddleware('general'), + financeController.getBudget +) -export default financeRouter \ No newline at end of file +export default financeRouter diff --git a/server/routes/scout.route.js b/server/routes/scout.route.js index 2c9788c1..acaf0687 100644 --- a/server/routes/scout.route.js +++ b/server/routes/scout.route.js @@ -1,17 +1,16 @@ -import { Router } from "express" -import scoutController from "../controllers/scout.controller.js"; +import { Router } from 'express' +import scoutController from '../controllers/scout.controller.js' - -const scoutRouter = Router(); +const scoutRouter = Router() scoutRouter.post('/', scoutController.insertScout) -scoutRouter.get('/:id', scoutController.certainScoutInfo) -scoutRouter.put('/:id', scoutController.updateScout) -scoutRouter.get('/allScouts/count', scoutController.allScoutsCount) -scoutRouter.get('/allScouts/info', scoutController.allScoutsInfo) -scoutRouter.get('/allScoutsInSector/count', scoutController.scoutsInSectorCount) -scoutRouter.get('/allScoutsInSector/info', scoutController.scoutsInSectorInfo) -scoutRouter.get('/allScoutsInUnit/count/:id', scoutController.allScoutsInUnitCount) -scoutRouter.get('/allScoutsInUnit/info/:id', scoutController.allScoutsInUnitInfo) +scoutRouter.get('/:id', scoutController.getScout) +scoutRouter.put('/:id', scoutController.updateScout) // or patch +scoutRouter.get('/all', scoutController.getAllScouts) +scoutRouter.get('/unit/:unitCaptainId', scoutController.getScoutsInUnit) +scoutRouter.get( + '/sector/:baseName/:suffixName', + scoutController.getScoutsInSector +) -export default scoutRouter \ No newline at end of file +export default scoutRouter