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 #36

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
17 changes: 15 additions & 2 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@ import db from '../database/db.js'
const captainController = {
getAllCaptains: async (req, res) => {
try {
// Query on the database to get the captains info
const result = await db.query(`SELECT * FROM "Captain"`)
const { type } = req.body

let result;
if (type === 'regular') {
result = await db.query(`SELECT * FROM "Captain" WHERE "type" = 'regular'`)
}
else if (type === 'unit') {
result = await db.query(`SELECT * FROM "Captain" WHERE "type" = 'unit'`)
}
else if (type === 'general') {
result = await db.query(`SELECT * FROM "Captain" WHERE "type" = 'general'`)
}
else {
result = await db.query(`SELECT * FROM "Captain"`)
}

// Respond with the data retrieved and a successful retrieval message
res.status(200).json({
Expand Down
34 changes: 33 additions & 1 deletion server/controllers/sector.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ const sectorController = {
})
}

const sectorInfo = await db.query(`
SELECT *
FROM "Sector"
WHERE "baseName" = $1 AND "suffixName" = $2
`,
[baseName, suffixName])

if (sectorInfo.rowCount === 0) {
return res.status(404).json({
error: "No sector exists with these ids"
})
}

const captainInfo = await db.query(`
SELECT *
FROM "Captain"
WHERE "captainId" = $1
`,
[unitCaptainId])

if (captainInfo.rowCount === 0) {
return res.status(404).json({
error: "No captain exist with this id"
})
}

if (captainInfo.rows[0].type !== 'unit') {
return res.status(401).json({
error: "The provided captain id is not for a unit captain"
})
}

const result = await db.query(
`
UPDATE "Sector"
Expand All @@ -118,7 +150,7 @@ const sectorController = {
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while retrieving the captains info',
error: 'An error occured while updating the sector info',
body: error,
})
}
Expand Down