From 03f4c09656548f733f5d5c266364f17e87905b42 Mon Sep 17 00:00:00 2001 From: akram Date: Mon, 25 Dec 2023 23:07:07 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=94=A5=20feat:=20Create=20sector=20ro?= =?UTF-8?q?ute=20&=20controller=20&=20getAllSectors=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/sector.controller.js | 26 +++++++++++++++++++++++++ server/routes/api.route.js | 2 ++ server/routes/sector.route.js | 8 ++++++++ 3 files changed, 36 insertions(+) create mode 100644 server/controllers/sector.controller.js create mode 100644 server/routes/sector.route.js diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js new file mode 100644 index 00000000..07e52282 --- /dev/null +++ b/server/controllers/sector.controller.js @@ -0,0 +1,26 @@ +import db from '../database/db.js' + +const sectorController = { + 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, + }) + } + } +} + +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/sector.route.js b/server/routes/sector.route.js new file mode 100644 index 00000000..1aa955dc --- /dev/null +++ b/server/routes/sector.route.js @@ -0,0 +1,8 @@ +import {Router} from "express" +import sectorController from "../controllers/sector.controller.js" + +const sectorRouter = Router(); + +sectorRouter.get('/all', sectorController.getAllSectors) + +export default sectorRouter; \ No newline at end of file From b5a9a9beda427c6ba354d201ad0d71d79be8a79f Mon Sep 17 00:00:00 2001 From: akram Date: Mon, 25 Dec 2023 23:27:15 +0200 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=94=A5=20feat:=20Implement=20getting?= =?UTF-8?q?=20a=20sector=20by=20its=20name.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/sector.controller.js | 39 ++++++++++++++++++++++++- server/routes/sector.route.js | 2 ++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index 07e52282..532f4616 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -1,6 +1,9 @@ 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(` @@ -20,7 +23,41 @@ const sectorController = { 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, + }) + } + }, } export default sectorController; \ No newline at end of file diff --git a/server/routes/sector.route.js b/server/routes/sector.route.js index 1aa955dc..3c89c0bc 100644 --- a/server/routes/sector.route.js +++ b/server/routes/sector.route.js @@ -4,5 +4,7 @@ import sectorController from "../controllers/sector.controller.js" const sectorRouter = Router(); sectorRouter.get('/all', sectorController.getAllSectors) +sectorRouter.get('/:baseName/:suffixName', sectorController.getSector) + export default sectorRouter; \ No newline at end of file From 6b8ea9f1d74f336c5b9afb3a8245759cf61c48a7 Mon Sep 17 00:00:00 2001 From: akram Date: Tue, 26 Dec 2023 00:22:34 +0200 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20feat:=20Implement=20inserting?= =?UTF-8?q?=20a=20new=20sector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/sector.controller.js | 26 +++++++++++++++++++++++++ server/routes/sector.route.js | 1 + 2 files changed, 27 insertions(+) diff --git a/server/controllers/sector.controller.js b/server/controllers/sector.controller.js index 532f4616..2d389d0d 100644 --- a/server/controllers/sector.controller.js +++ b/server/controllers/sector.controller.js @@ -58,6 +58,32 @@ const sectorController = { }) } }, + // @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/sector.route.js b/server/routes/sector.route.js index 3c89c0bc..795ac058 100644 --- a/server/routes/sector.route.js +++ b/server/routes/sector.route.js @@ -5,6 +5,7 @@ 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 From eb3a51b60e6be23639182fb45d92db8d1f6545b7 Mon Sep 17 00:00:00 2001 From: akram Date: Tue, 26 Dec 2023 01:13:23 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=A5=20feat:=20Implement=20updating?= =?UTF-8?q?=20a=20captain=20rank=20(type)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/captain.controller.js | 30 ++++++++++++++++++++++++ server/routes/captain.route.js | 1 + 2 files changed, 31 insertions(+) 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/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