-
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 #16 from CMP26Projects/backend-functionalities
Backend-functionalities (captain)
- Loading branch information
Showing
4 changed files
with
68 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,48 @@ | ||
import db from '../database/db.js' | ||
|
||
const captainController = { | ||
allCaptainsInfo: async (req, res) => { | ||
try { | ||
// Query on the database to get the captains info | ||
const result = await db.query(` | ||
SELECT * | ||
FROM "Captain" | ||
`) | ||
|
||
// Respond with the data retrieved and a successful retrieval message | ||
res.status(200).json({ | ||
message: 'Successful retrieval', | ||
body: result, | ||
}) | ||
|
||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
error: 'An error occured while retrieving the captains info' | ||
}) | ||
} | ||
}, | ||
allCaptainsCount: async (req, res) => { | ||
try { | ||
// Query on the database to get the captains info | ||
const result = await db.query(` | ||
SELECT COUNT(*) | ||
FROM "Captain" | ||
`); | ||
|
||
// Respond with the data retrieved and a successful retrieval message | ||
res.status(200).json({ | ||
message: 'Successful retrieval', | ||
body: result, | ||
}) | ||
|
||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
error: 'An error occured while retrieving the captains count' | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export default captainController |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { Router } from 'express' | ||
import authRouter from './auth.route.js' | ||
import captainRouter from './captain.route.js' | ||
import authMiddleware from '../middlewares/auth.middleware.js' | ||
|
||
const apiRouter = Router() | ||
|
||
apiRouter.use('/auth', authRouter) | ||
apiRouter.use('/captain', authMiddleware, captainRouter) | ||
|
||
export default apiRouter |
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,15 @@ | ||
import { Router } from "express" | ||
import authMiddleware from "../middlewares/auth.middleware.js"; | ||
import captainController from "../controllers/captain.controller.js"; | ||
|
||
|
||
const captainRouter = Router(); | ||
|
||
// Check that the user is authorized | ||
|
||
|
||
captainRouter.get('/allCaptains/info', captainController.allCaptainsInfo) | ||
captainRouter.get('/allCaptains/count', captainController.allCaptainsCount) | ||
|
||
|
||
export default captainRouter |