diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index 569b443d..2877f2e0 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -33,6 +33,7 @@ const authController = { const result = await db.query( `INSERT INTO "Captain"("firstName", "middleName", "lastName", "phoneNumber", "email", "password", "gender", "type") VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;`, + params.concat(["regular"]) ); const newCaptain = result.rows[0]; diff --git a/server/controllers/captain.controller.js b/server/controllers/captain.controller.js new file mode 100644 index 00000000..a06b30f0 --- /dev/null +++ b/server/controllers/captain.controller.js @@ -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 \ No newline at end of file diff --git a/server/routes/api.route.js b/server/routes/api.route.js index da9b32f2..8f574d29 100644 --- a/server/routes/api.route.js +++ b/server/routes/api.route.js @@ -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 diff --git a/server/routes/captain.route.js b/server/routes/captain.route.js new file mode 100644 index 00000000..9d27c6c5 --- /dev/null +++ b/server/routes/captain.route.js @@ -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 \ No newline at end of file