Skip to content

Commit

Permalink
🚑 fix: Migrate all path params to query params for sector & modify th…
Browse files Browse the repository at this point in the history
…e api's
akramhany committed Dec 27, 2023

Verified

This commit was signed with the committer’s verified signature.
pdabelf5 Paul Abel
1 parent 6ce535b commit 47c8132
Showing 3 changed files with 17 additions and 21 deletions.
8 changes: 5 additions & 3 deletions client/src/redux/slices/sectorApiSlice.js
Original file line number Diff line number Diff line change
@@ -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"],
}),
21 changes: 8 additions & 13 deletions server/controllers/sector.controller.js
Original file line number Diff line number Diff line change
@@ -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,17 +64,17 @@ 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({
error: "You must insert a baseName for the sector"
})
}

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(`
9 changes: 4 additions & 5 deletions server/routes/sector.route.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 47c8132

Please sign in to comment.