Skip to content

Commit

Permalink
🔥 chore: update routes to be RESTful
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 committed Dec 25, 2023
1 parent 215ff72 commit 6ce843f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 188 deletions.
106 changes: 19 additions & 87 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import db from '../database/db.js'

const captainController = {
allCaptainsInfo: async (req, res) => {
getAllCaptains: async (req, res) => {
try {
// Query on the database to get the captains info
const result = await db.query(`SELECT * FROM "Captain"`)
Expand All @@ -10,6 +10,7 @@ const captainController = {
res.status(200).json({
message: 'Successful retrieval',
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
Expand All @@ -19,42 +20,22 @@ const captainController = {
})
}
},
allCaptainsCount: async (req, res) => {
getCaptainsInSector: async (req, res) => {
try {
// Query on the database to get the captains info
const result = await db.query(
`SELECT COUNT(*) AS count FROM "Captain"`
)

// Respond with the data retrieved and a successful retrieval message
res.status(200).json({
message: 'Successful retrieval',
body: result.rows[0].count,
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while retrieving the captains count',
body: error,
})
}
},
captainsInSectorInfo: async (req, res) => {
try {
// Extract sector base name and suffix name from the request body
const { rSectorBaseName, rSectorSuffixName } = req.body
const { baseName, suffixName } = req.params

// 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`,
[rSectorBaseName, rSectorSuffixName]
[baseName, suffixName]
)

res.status(200).json({
message: 'Successful retrieval',
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
Expand All @@ -64,22 +45,25 @@ const captainController = {
})
}
},
captainsInSectorCount: async (req, res) => {
getCaptainsInUnit: async (req, res) => {
try {
// Extract sector base name and suffix name from the request body
const { rSectorBaseName, rSectorSuffixName } = req.body
const { unitCaptainId } = req.params

// Query on the database to get all the captains count in a specific sector
// Query to get the id
const result = await db.query(
`SELECT COUNT(*) AS count
FROM "Captain"
WHERE "rSectorBaseName" = $1 AND "rSectorSuffixName" = $2`,
[rSectorBaseName, rSectorSuffixName]
`SELECT C.*
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.rows[0].count,
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
Expand All @@ -89,7 +73,7 @@ const captainController = {
})
}
},
captainInfo: async (req, res) => {
getCaptain: async (req, res) => {
try {
// Extract the captain ID from the request params
const { captainId } = req.params
Expand Down Expand Up @@ -122,58 +106,6 @@ const captainController = {
})
}
},
allCaptainsInUnitInfo: async (req, res) => {
try {
const { unitCaptainId } = req.params

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

// Return the data
res.status(200).json({
message: 'Successful retrieval',
body: result.rows,
})
} catch (error) {
console.log(error)
res.status(500).json({
message: 'An error occured while retrieving data',
body: error,
})
}
},
allCaptainsInUnitCount: async (req, res) => {
try {
const { unitCaptainId } = req.params

// Query to get the id
const result = await db.query(
`SELECT COUNT(*) AS 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.rows[0].count,
})
} catch (error) {
console.log(error)
res.status(500).json({
message: 'An error occured while retrieving data',
body: error,
})
}
},
}

export default captainController
81 changes: 9 additions & 72 deletions server/controllers/scout.controller.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import db from '../database/db.js'

const scoutController = {
allScoutsCount: async (req, res) => {
try {
const result = await db.query(
`SELECT COUNT(*) AS count FROM "Scout";`
)

res.status(200).json({
message: 'Successful retrieval',
body: result.rows[0].count,
})
} catch (error) {
console.log(error)
res.status(500).json({
message: 'An error occured while retrieving data',
body: error,
})
}
},
allScoutsInfo: async (req, res) => {
getAllScouts: async (req, res) => {
try {
const result = await db.query(`SELECT * FROM "Scout";`)

res.status(200).json({
message: 'Successful retrieval',
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
Expand All @@ -35,20 +18,21 @@ const scoutController = {
})
}
},
scoutsInSectorInfo: async (req, res) => {
getScoutsInSector: async (req, res) => {
try {
const { sectorBaseName, sectorSuffixName } = req.body
const { baseName, suffixName } = req.params

const result = await db.query(
`SELECT *
FROM "Scout"
WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2;`,
[sectorBaseName, sectorSuffixName]
[baseName, suffixName]
)

res.status(200).json({
message: 'Successful retrieval',
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
Expand All @@ -58,30 +42,7 @@ const scoutController = {
})
}
},
scoutsInSectorCount: async (req, res) => {
try {
const { sectorBaseName, sectorSuffixName } = req.body

const result = await db.query(
`SELECT COUNT(*) AS count
FROM "Scout"
WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2;`,
[sectorBaseName, sectorSuffixName]
)

res.status(200).json({
message: 'Successful retrieval',
body: result.rows[0].count,
})
} catch (error) {
console.log(error)
res.status(500).json({
message: 'An error occured while retrieving data',
body: error,
})
}
},
allScoutsInUnitInfo: async (req, res) => {
getScoutsInUnit: async (req, res) => {
try {
const { unitCaptainId } = req.params

Expand All @@ -97,6 +58,7 @@ const scoutController = {
res.status(200).json({
message: 'Successful retrieval',
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
Expand All @@ -106,32 +68,7 @@ const scoutController = {
})
}
},
allScoutsInUnitCount: async (req, res) => {
try {
const { unitCaptainId } = req.params

const result = await db.query(
`SELECT Count(*)
FROM "Scout" AS scout, "Sector" AS sector
WHERE sector."unitCaptainId" = $1 AND
scout."sectorBaseName" = sector."baseName" AND
scout."sectorSuffixName" = sector."suffixName";`,
[unitCaptainId]
)

res.status(200).json({
message: 'Successful retrieval',
body: result.rows[0].count,
})
} catch (error) {
console.log(error)
res.status(500).json({
message: 'An error occured while retrieving data',
body: error,
})
}
},
certainScoutInfo: async (req, res) => {
getScout: async (req, res) => {
try {
const { scoutId } = req.params

Expand Down
25 changes: 11 additions & 14 deletions server/routes/captain.route.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Router } from "express"
import captainController from "../controllers/captain.controller.js";
import { Router } from 'express'
import captainController from '../controllers/captain.controller.js'

const captainRouter = Router()

const captainRouter = Router();
captainRouter.get('/:id', captainController.getCaptain)
captainRouter.get('/all', captainController.getAllCaptains)
captainRouter.get('/unit/:unitCaptainId', captainController.getCaptainsInUnit)
captainRouter.get(
'/sector/:baseName/:suffixName',
captainController.getCaptainsInSector
)


captainRouter.get('/:id', captainController.captainInfo)
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('/allCaptainsInUnit/info/:id', captainController.allCaptainsInUnitInfo)
captainRouter.get('/allCaptainsInUnit/count/:id', captainController.allCaptainsInUnitCount)


export default captainRouter
export default captainRouter
8 changes: 6 additions & 2 deletions server/routes/finance.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import financeController from '../controllers/finance.controller.js'
import checkRankMiddleware from '../middlewares/checkRank.middleware.js'
const financeRouter = Router()

financeRouter.get('/budget', checkRankMiddleware('general'), financeController.getBudget)
financeRouter.get(
'/',
checkRankMiddleware('general'),
financeController.getBudget
)

export default financeRouter
export default financeRouter
25 changes: 12 additions & 13 deletions server/routes/scout.route.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Router } from "express"
import scoutController from "../controllers/scout.controller.js";
import { Router } from 'express'
import scoutController from '../controllers/scout.controller.js'


const scoutRouter = Router();
const scoutRouter = Router()

scoutRouter.post('/', scoutController.insertScout)
scoutRouter.get('/:id', scoutController.certainScoutInfo)
scoutRouter.put('/:id', scoutController.updateScout)
scoutRouter.get('/allScouts/count', scoutController.allScoutsCount)
scoutRouter.get('/allScouts/info', scoutController.allScoutsInfo)
scoutRouter.get('/allScoutsInSector/count', scoutController.scoutsInSectorCount)
scoutRouter.get('/allScoutsInSector/info', scoutController.scoutsInSectorInfo)
scoutRouter.get('/allScoutsInUnit/count/:id', scoutController.allScoutsInUnitCount)
scoutRouter.get('/allScoutsInUnit/info/:id', scoutController.allScoutsInUnitInfo)
scoutRouter.get('/:id', scoutController.getScout)
scoutRouter.put('/:id', scoutController.updateScout) // or patch
scoutRouter.get('/all', scoutController.getAllScouts)
scoutRouter.get('/unit/:unitCaptainId', scoutController.getScoutsInUnit)
scoutRouter.get(
'/sector/:baseName/:suffixName',
scoutController.getScoutsInSector
)

export default scoutRouter
export default scoutRouter

0 comments on commit 6ce843f

Please sign in to comment.