Skip to content

Commit

Permalink
Merge pull request #19 from CMP26Projects/backend-captain
Browse files Browse the repository at this point in the history
Backend captain
  • Loading branch information
amir-kedis authored Dec 24, 2023
2 parents e199d81 + 3c12b98 commit 6cf0495
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
74 changes: 74 additions & 0 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,80 @@ const captainController = {
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
allCaptainsInUnitInfo: async (req, res) => {
try {
const { unitCaptainId } = req.body;

// Make sure that the id is provided (not undefined)
if (!unitCaptainId){
return res.status(404).json({
error: "Enter a valid unit captain id"
})
}

// 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]
)

// If there is no result found, return 404 not found error (This might not be an error)
if (!result.rows.length)
{
return res.status(404).json({
error: "Captains Not Found"
})
}

// Return the data
res.status(200).json({
message: "Successful retrieval",
body: result,
});

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
allCaptainsInUnitCount: async (req, res) => {
try {
const { unitCaptainId } = req.body;

// Make sure that the id is provided (not undefined)
if (!unitCaptainId){
return res.status(404).json({
error: "Enter a valid unit captain id"
})
}

// Query to get the id
const result = await db.query(`
SELECT 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,
});

} catch (error) {
console.log(error);
res.status(500).json({
Expand Down
2 changes: 2 additions & 0 deletions server/routes/captain.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ captainRouter.get('/allCaptains/count', captainController.allCaptainsCount)
captainRouter.get('/captainsInSector/info', captainController.captainsInSectorInfo)
captainRouter.get('/captainsInSector/count', captainController.captainsInSectorCount)
captainRouter.get('/ceratinCaptain/info', captainController.captainInfo)
captainRouter.get('/allCaptainsInUnit/info', captainController.allCaptainsInUnitInfo)
captainRouter.get('/allCaptainsInUnit/count', captainController.allCaptainsInUnitCount)


export default captainRouter

0 comments on commit 6cf0495

Please sign in to comment.