Skip to content

Commit

Permalink
Merge pull request #16 from CMP26Projects/backend-functionalities
Browse files Browse the repository at this point in the history
Backend-functionalities (captain)
  • Loading branch information
AbdelruhmanSamy authored Dec 23, 2023
2 parents 243ebf3 + 2eab5d9 commit 85a41f4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
48 changes: 48 additions & 0 deletions server/controllers/captain.controller.js
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
4 changes: 4 additions & 0 deletions server/routes/api.route.js
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
15 changes: 15 additions & 0 deletions server/routes/captain.route.js
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

0 comments on commit 85a41f4

Please sign in to comment.