Skip to content
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
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