Skip to content

Commit

Permalink
Merge pull request #36 from CMP26Projects/backend-functionalities
Browse files Browse the repository at this point in the history
Backend functionalities
  • Loading branch information
amir-kedis authored Dec 26, 2023
2 parents 1d48a3d + e1d3358 commit 8e7402e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
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

0 comments on commit 8e7402e

Please sign in to comment.