Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend functionalities #31

Merged
merged 3 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,38 @@ 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"
SET "type" = $2
WHERE "captainId" = $1
RETURNING *
`,
[captainId, type])
[id, type])

res.status(200).json({
message: "Successful update",
body: result,
body: result.rows,
})

} catch (error) {
Expand Down
35 changes: 35 additions & 0 deletions server/controllers/sector.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion server/routes/captain.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions server/routes/sector.route.js
Original file line number Diff line number Diff line change
@@ -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;