From 47c8132f01e0b23ea7b096398e94d03a40bb08b0 Mon Sep 17 00:00:00 2001 From: akram Date: Wed, 27 Dec 2023 17:51:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20fix:=20Migrate=20all=20path=20pa?= =?UTF-8?q?rams=20to=20query=20params=20for=20sector=20&=20modify=20the=20?= =?UTF-8?q?api's?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/redux/slices/sectorApiSlice.js | 8 +++++--- server/controllers/sector.controller.js | 21 ++++++++------------- server/routes/sector.route.js | 9 ++++----- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/client/src/redux/slices/sectorApiSlice.js b/client/src/redux/slices/sectorApiSlice.js index d801b5aa..9c88ae2b 100644 --- a/client/src/redux/slices/sectorApiSlice.js +++ b/client/src/redux/slices/sectorApiSlice.js @@ -6,7 +6,7 @@ export const captainsApi = apiSlice.injectEndpoints({ endpoints: (builder) => ({ GetSectors: builder.query({ query: () => ({ - url: `${SECTOR_URL}/`, + url: `${SECTOR_URL}/all/`, method: "GET", }), providesTags: ["Sector"], @@ -21,17 +21,19 @@ export const captainsApi = apiSlice.injectEndpoints({ }), UpdateSectorUnitCaptain: builder.mutation({ query: (sector) => ({ - url: `${SECTOR_URL}/${sector.baseName}/${sector.suffixName}`, + url: `${SECTOR_URL}/unit/`, method: "PATCH", body: sector, + query: sector, }), invalidatesTags: ["Sector"], }), UpdateSectorRegularCaptain: builder.mutation({ query: (sector) => ({ - url: `${SECTOR_URL}/captain/${sector.baseName}/${sector.suffixName}`, + url: `${SECTOR_URL}/assign/`, method: "PATCH", body: sector, + query: sector, }), invalidatesTags: ["Sector"], }), diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index f25ef7d3..8ef0ff7b 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -25,17 +25,12 @@ const sectorController = { } }, - // @desc Get sector by id (baseName and suffixName send as params) + // @desc Get sector by id (baseName and suffixName send as query) // @route GET /api/sector/:baseName/:suffixName // @access Private getSector: async (req, res) => { try { - let { baseName, suffixName } = req.params - - // If the suffix name wasn't provided (meaning it would be :suffixName) then make it an empty string - if (suffixName === ':suffixName') { - suffixName = "" - } + const { baseName, suffixName } = req.query const result = await db.query( ` @@ -69,7 +64,7 @@ const sectorController = { // @access Private insertSector: async (req, res) => { try { - let { baseName, suffixName, unitCaptainId } = req.body + const { baseName, suffixName, unitCaptainId } = req.body if (!baseName) { return res.status(404).json({ @@ -77,9 +72,9 @@ const sectorController = { }) } - if (!suffixName) { - suffixName = "" - } +// if (!suffixName) { +// suffixName = "" +// } const result = await db.query( ` @@ -107,7 +102,7 @@ const sectorController = { // @access Private setUnitCaptain: async (req, res) => { try { - const { baseName, suffixName } = req.params + const { baseName, suffixName } = req.query const { unitCaptainId } = req.body if (!unitCaptainId) { @@ -172,7 +167,7 @@ const sectorController = { }, assignCaptain: async (req, res) => { try { - const { baseName, suffixName } = req.params + const { baseName, suffixName } = req.query const { captainId } = req.body const result = await db.query(` diff --git a/server/routes/sector.route.js b/server/routes/sector.route.js index 618805cc..a9175f2e 100644 --- a/server/routes/sector.route.js +++ b/server/routes/sector.route.js @@ -5,13 +5,12 @@ import checkRankMiddleware from '../middlewares/checkRank.middleware.js' const sectorRouter = Router() sectorRouter.post('/', sectorController.insertSector) -sectorRouter.get('/', sectorController.getAllSectors) -sectorRouter.get('/:baseName/:suffixName', sectorController.getSector) +sectorRouter.get('/all', sectorController.getAllSectors) +sectorRouter.get('/', sectorController.getSector) sectorRouter.patch( - '/:baseName/:suffixName', - //TODO: Check if the captain id is for a unit captain + '/unit', sectorController.setUnitCaptain ) -sectorRouter.patch('/captain/:baseName/:suffixName', sectorController.assignCaptain) +sectorRouter.patch('/assign', sectorController.assignCaptain) export default sectorRouter