-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from CMP26Projects/backend-functionalities
Backend functionalities
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |