diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 3e6fded9..69f81c63 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -106,14 +106,26 @@ const captainController = { }) } }, + // @desc Update a captain type + // @route PATCH /api/captain/change/type/:id + // @access Private setCaptainType: async (req, res) => { try { - const { captainId } = req.params + const { id } = req.params const { type } = req.body - //if (type != 'regular' && type != 'unit' && type != 'general') { - // - //} + if (!id) { + return res.status(400).json({ + error: "Please enter a valid id", + }) + } + + // Maybe we can remove it as it is hard coded (and depened only on the catch error which is not very descriptive) + if (type != 'regular' && type != 'unit' && type != 'general') { + return res.status(400).json({ + error: "Please enter a valid captain type", + }) + } const result = await db.query(` UPDATE "Captain" @@ -121,11 +133,11 @@ const captainController = { WHERE "captainId" = $1 RETURNING * `, - [captainId, type]) + [id, type]) res.status(200).json({ message: "Successful update", - body: result, + body: result.rows, }) } catch (error) { diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index 2d389d0d..9c32559f 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -84,6 +84,41 @@ const sectorController = { }) } }, + + // @desc update a sector unit captain + // @route PATCH /api/sector/unit/set/:id/:baseName/:suffixName + // @access Private + setUnitCaptain: async (req, res) => { + try { + const { id, baseName, suffixName } = req.params + + if (!id || (!baseName && !suffixName)) { + res.status(400).json({ + error: "Please enter valid ids" + }) + } + + const result = await db.query(` + UPDATE "Sector" + SET "unitCaptainId" = $1 + WHERE "baseName" = $2 AND "suffixName" = $3 + RETURNING * + `, + [id, baseName, suffixName]) + + res.status(200).json({ + message: "Successful update", + body: result.rows, + }) + + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occured while retrieving the captains info', + body: error, + }) + } + } } export default sectorController; \ No newline at end of file diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index b240ecc5..455ecf94 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -10,6 +10,6 @@ captainRouter.get( '/sector/:baseName/:suffixName', captainController.getCaptainsInSector ) -captainRouter.patch('/type/change/:captainId', captainController.setCaptainType) +captainRouter.patch('/type/change/:id', captainController.setCaptainType) export default captainRouter diff --git a/server/routes/sector.route.js b/server/routes/sector.route.js index 795ac058..59cd2adc 100644 --- a/server/routes/sector.route.js +++ b/server/routes/sector.route.js @@ -1,11 +1,17 @@ import {Router} from "express" import sectorController from "../controllers/sector.controller.js" +import checkRankMiddleware from "../middlewares/checkRank.middleware.js"; const sectorRouter = Router(); sectorRouter.get('/all', sectorController.getAllSectors) sectorRouter.get('/:baseName/:suffixName', sectorController.getSector) sectorRouter.post('/add', sectorController.insertSector) +sectorRouter.patch( + '/unit/set/:id/:baseName/:suffixName', + //TODO: Check if the captain id is for a unit captain + sectorController.setUnitCaptain +) export default sectorRouter; \ No newline at end of file