diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js index 24f68fe3..66924702 100644 --- a/server/controllers/captain.controller.js +++ b/server/controllers/captain.controller.js @@ -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 diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js new file mode 100644 index 00000000..2d389d0d --- /dev/null +++ b/server/controllers/sector.controller.js @@ -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; \ No newline at end of file diff --git a/server/routes/api.route.js b/server/routes/api.route.js index d42aab0a..1a0b0550 100644 --- a/server/routes/api.route.js +++ b/server/routes/api.route.js @@ -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) @@ -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 diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js index 3cedbead..78b2c8f6 100644 --- a/server/routes/captain.route.js +++ b/server/routes/captain.route.js @@ -10,5 +10,6 @@ captainRouter.get( '/sector/:baseName/:suffixName', captainController.getCaptainsInSector ) +captainRouter.patch('/type/change/:captainId', captainController.setCaptainType) export default captainRouter diff --git a/server/routes/sector.route.js b/server/routes/sector.route.js new file mode 100644 index 00000000..795ac058 --- /dev/null +++ b/server/routes/sector.route.js @@ -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; \ No newline at end of file