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

Merged
merged 4 commits into from
Dec 25, 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
30 changes: 30 additions & 0 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ const captainController = {
})
}
},
setCaptainType: async (req, res) => {
try {
const { captainId } = req.params
const { type } = req.body

//if (type != 'regular' && type != 'unit' && type != 'general') {
//
//}

const result = await db.query(`
UPDATE "Captain"
SET "type" = $2
WHERE "captainId" = $1
RETURNING *
`,
[captainId, type])

res.status(200).json({
message: "Successful update",
body: result,
})

} catch (error) {
console.log(error)
res.status(500).json({
message: 'An error occured while retrieving data',
body: error,
})
}
}
}

export default captainController
89 changes: 89 additions & 0 deletions server/controllers/sector.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import db from '../database/db.js'

const sectorController = {
// @desc Get all sectors (info and count)
// @route GET /api/sector/all
// @access Private
getAllSectors: async (req, res) => {
try {
const result = await db.query(`
SELECT *
FROM "Sector"
`)

res.status(200).json({
message: "Successful retrieval",
body: result.rows,
count: result.rowCount,
})
} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while retrieving the captains info',
body: error,
})
}
},

// @desc Get sector by id (baseName and suffixName send as params)
// @route GET /api/sector/:baseName/:suffixName
// @access Private
getSector: async (req, res) => {
try {
const { baseName, suffixName } = req.params

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

if (result.rowCount === 0) {
return res.status(404).json({
error: "No sector found with this name"
})
}

res.status(200).json({
message: "Successful retrieval",
body: result.rows,
})

} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while retrieving the captains info',
body: error,
})
}
},
// @desc Insert a new sector given its baseName, suffixName and unitCaptainId(optional)
// @route POST /api/sector/add
// @access Private
insertSector: async (req, res) => {
try {
const { baseName, suffixName, unitCaptainId } = req.body

const result = await db.query(`
INSERT INTO "Sector" VALUES ($1, $2, $3)
RETURNING *
`,
[baseName, suffixName, unitCaptainId])

res.status(200).json({
message: "Successful insertion",
body: result.rows,
})

} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while retrieving the captains info',
body: error,
})
}
},
}

export default sectorController;
2 changes: 2 additions & 0 deletions server/routes/api.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import termRouter from './term.route.js'
import captainRouter from './captain.route.js'
import alertRouter from './alert.route.js'
import scoutRouter from './scout.route.js'
import sectorRouter from './sector.route.js'
const apiRouter = Router()

apiRouter.use('/auth', authRouter)
Expand All @@ -16,5 +17,6 @@ apiRouter.use('/term', authMiddleware, termRouter)
apiRouter.use('/captain', authMiddleware, captainRouter)
apiRouter.use('/alert', alertRouter)
apiRouter.use('/scout', authMiddleware, scoutRouter)
apiRouter.use('/sector', authMiddleware, sectorRouter)

export default apiRouter
1 change: 1 addition & 0 deletions server/routes/captain.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ captainRouter.get(
'/sector/:baseName/:suffixName',
captainController.getCaptainsInSector
)
captainRouter.patch('/type/change/:captainId', captainController.setCaptainType)

export default captainRouter
11 changes: 11 additions & 0 deletions server/routes/sector.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Router} from "express"
import sectorController from "../controllers/sector.controller.js"

const sectorRouter = Router();

sectorRouter.get('/all', sectorController.getAllSectors)
sectorRouter.get('/:baseName/:suffixName', sectorController.getSector)
sectorRouter.post('/add', sectorController.insertSector)


export default sectorRouter;