Skip to content

Commit

Permalink
Merge pull request #18 from CMP26Projects/backend-captain
Browse files Browse the repository at this point in the history
Backend captain
  • Loading branch information
AbdelruhmanSamy authored Dec 24, 2023
2 parents f81277d + da7c58d commit c2f093e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
93 changes: 93 additions & 0 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,99 @@ const captainController = {
error: 'An error occured while retrieving the captains count'
})
}
},
captainsInSectorInfo: async (req, res) => {
try {
// Extract sector base name and suffix name from the request body
const { sectorBaseName, sectorSuffixName } = req.body;

// Query on the database to get all the captains info in a specific sector
const result = await db.query(`
SELECT *
FROM "Captain"
WHERE "rSectorBaseName" = $1 AND "rSectorSuffixName" = $2`,
[sectorBaseName, sectorSuffixName]
);

// If the query returned nothing, return a message that says that
if (!result.rows.length) {
return res.status(200).json({
message: "Count of rows returned is 0",
});
}

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
})
}
},
captainsInSectorCount: async (req, res) => {
try {
// Extract sector base name and suffix name from the request body
const { sectorBaseName, sectorSuffixName } = req.body;

// Query on the database to get all the captains count in a specific sector
const result = await db.query(`
SELECT COUNT(*)
FROM "Captain"
WHERE "rSectorBaseName" = $1 AND "rSectorSuffixName" = $2`,
[sectorBaseName, sectorSuffixName]
);

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
})
}
},
captainInfo: async (req, res) => {
try {
// Extract the captain ID from the request body
const { captainId } = req.body;

// Query on the database to get that captain info
const result = await db.query(`
SELECT *
FROM "Captain"
WHERE "captainId" = $1`,
[captainId]
);

// If captain doesn't exist return an error message
if (!result.rows.length) {
return res.status(404).json({
error: "Captain not found!"
})
}

// Return the data of the captain
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
})
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions server/routes/captain.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const captainRouter = Router();

captainRouter.get('/allCaptains/info', captainController.allCaptainsInfo)
captainRouter.get('/allCaptains/count', captainController.allCaptainsCount)
captainRouter.get('/captainsInSector/info', captainController.captainsInSectorInfo)
captainRouter.get('/captainsInSector/count', captainController.captainsInSectorCount)
captainRouter.get('/ceratinCaptain/info', captainController.captainInfo)


export default captainRouter

0 comments on commit c2f093e

Please sign in to comment.