Skip to content

Commit

Permalink
Merge pull request #39 from CMP26Projects/backend-functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 authored Dec 27, 2023
2 parents 1a82ca5 + f26fa83 commit c6bded8
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 15 deletions.
136 changes: 136 additions & 0 deletions server/controllers/attendance.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import db from "../database/db.js"

const attendanceController = {
// @desc Insert a new attendance record for a scout in a certain sector
// @route POST /api/attendance/
// @access Private
insertAttendance: async (req, res) => {
try {
const { scoutId, weekNumber, termNumber, attendanceStatus } = req.body

// Get the scout info to check if the provided scout id exists or not
const scoutInfo = await db.query(`
SELECT *
FROM "Scout"
WHERE "scoutId" = $1;
`,
[scoutId]
)

if (scoutInfo.rowCount === 0) {
return res.status(404).json({
error: "No scout exist with this id"
})
}

// Get the week info to check if the provided week & term numbers exist or not
const weekInfo = await db.query(`
SELECT *
FROM "Week"
WHERE "weekNumber" = $1 AND "termNumber" = $2;
`,
[weekNumber, termNumber])

if (weekInfo.rowCount === 0) {
return res.status(404).json({
error: "Please enter an existing weekNumber & termNumber"
})
}

// Insert a new attendance record into the database
const result = await db.query(`
INSERT INTO "ScoutAttendance" VALUES ($1, $2, $3, $4) RETURNING *;
`,
[scoutId, weekNumber, termNumber, attendanceStatus])

// If insertion failed return an error
if (result.rowCount === 0) {
return res.status(500).json({
error: "Insertion failed"
})
}

// Return a success message
res.status(200).json({
message: "Successful insertion",
body: result.rows,
count: result.rowCount,
})

} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while inserting a new attendance',
body: error,
})
}
},

// @desc Get all attendance records for all the scouts in a certain sector in a certain week & term
// @route GET /api/attendance/sector/:baseName/:suffixName/:weekNumber/:termNumber
// @access Private
getSectorAttendance: async (req, res) => {
try {
const { baseName, suffixName, weekNumber, termNumber } = req.params

const result = await db.query(`
SELECT "ScoutAttendance".*
FROM "Scout", "ScoutAttendance"
WHERE "Scout"."sectorBaseName" = $1 AND "Scout"."sectorSuffixName" = $2
AND "ScoutAttendance"."weekNumber" = $3 AND "ScoutAttendance"."termNumber" = $4
AND "ScoutAttendance"."scoutId" = "Scout"."scoutId";
`,
[baseName, suffixName, weekNumber, termNumber])

if (result.rowCount === 0) {
return res.status(404).json({
error: "No data exists for the provided info"
})
}

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 sector attendance',
body: error,
})
}
},

// @desc Get attendance records for a certain scout in a certain week & term
// @route GET /api/attendance/:scoutId/:weekNumber/:termNumber
// @access Private
getScoutAttendance: async (req, res) => {
try {
const { scoutId, weekNumber, termNumber } = req.params

const result = await db.query(`
SELECT *
FROM "ScoutAttendance"
WHERE "scoutId" = $1 AND "weekNumber" = $2 AND "termNumber" = $3
`,
[scoutId, weekNumber, termNumber])

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 sector attendance',
body: error,
})
}
}
}

export default attendanceController;
6 changes: 3 additions & 3 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const authController = {
`SELECT "email", "password"
FROM "Captain"
WHERE "email" = $1;`,
[email]
[email.toLowerCase()]
)
if (captain.rows.length) {
return res.status(400).json({ error: 'Email is taken!!' })
Expand All @@ -42,7 +42,7 @@ const authController = {
middleName,
lastName,
phoneNumber,
email,
email.toLowerCase(),
hashedPassword,
gender,
'regular',
Expand Down Expand Up @@ -79,7 +79,7 @@ const authController = {
`SELECT *
FROM "Captain"
WHERE "email" = $1;`,
[email]
[email.toLowerCase()]
)
if (!result.rows.length) {
return res.status(400).json({
Expand Down
10 changes: 5 additions & 5 deletions server/controllers/captain.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ const captainController = {
getCaptain: async (req, res) => {
try {
// Extract the captain ID from the request params
const { id } = req.params
const { captainId } = req.params

// Query on the database to get that captain info
const result = await db.query(
`SELECT *
FROM "Captain"
WHERE "captainId" = $1`,
[id]
[captainId]
)

// If captain doesn't exist return an error message
Expand Down Expand Up @@ -124,10 +124,10 @@ const captainController = {
// @access Private
setCaptainType: async (req, res) => {
try {
const { id } = req.params
const { captainId } = req.params
const { type } = req.body

if (!id) {
if (!captainId) {
return res.status(400).json({
error: "Please enter a valid id",
})
Expand All @@ -146,7 +146,7 @@ const captainController = {
WHERE "captainId" = $1
RETURNING *
`,
[id, type])
[captainId, type])

res.status(200).json({
message: "Successful update",
Expand Down
21 changes: 18 additions & 3 deletions server/controllers/sector.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ const sectorController = {
// @access Private
getSector: async (req, res) => {
try {
const { baseName, suffixName } = req.params

let { baseName, suffixName } = req.params

// If the suffix name wasn't provided (meaning it would be :suffixName) then make it an empty string
if (suffixName === ':suffixName') {
suffixName = ""
}

const result = await db.query(
`
SELECT *
Expand Down Expand Up @@ -64,7 +69,17 @@ const sectorController = {
// @access Private
insertSector: async (req, res) => {
try {
const { baseName, suffixName, unitCaptainId } = req.body
let { baseName, suffixName, unitCaptainId } = req.body

if (!baseName) {
return res.status(404).json({
error: "You must insert a baseName for the sector"
})
}

if (!suffixName) {
suffixName = ""
}

const result = await db.query(
`
Expand Down
2 changes: 2 additions & 0 deletions server/routes/api.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import captainRouter from './captain.route.js'
import alertRouter from './alert.route.js'
import scoutRouter from './scout.route.js'
import sectorRouter from './sector.route.js'
import attendanceRouter from './attendance.route.js'
const apiRouter = Router()

apiRouter.use('/auth', authRouter)
Expand All @@ -18,5 +19,6 @@ apiRouter.use('/captain', authMiddleware, captainRouter)
apiRouter.use('/alert', authMiddleware, alertRouter)
apiRouter.use('/scout', authMiddleware, scoutRouter)
apiRouter.use('/sector', authMiddleware, sectorRouter)
apiRouter.use('/attendance', authMiddleware, attendanceRouter)

export default apiRouter
11 changes: 11 additions & 0 deletions server/routes/attendance.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Router } from "express"
import attendanceController from "../controllers/attendance.controller.js"

const attendanceRouter = Router()

// Insert a new attendance for a scout
attendanceRouter.post('/', attendanceController.insertAttendance)
attendanceRouter.get('/sector/:baseName/:suffixName/:weekNumber/:termNumber', attendanceController.getSectorAttendance)
attendanceRouter.get('/:scoutId/:weekNumber/:termNumber', attendanceController.getScoutAttendance)

export default attendanceRouter;
4 changes: 2 additions & 2 deletions server/routes/captain.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ captainRouter.get(
'/sector/:baseName/:suffixName',
captainController.getCaptainsInSector
)
captainRouter.get('/:id', captainController.getCaptain)
captainRouter.patch('/:id', captainController.setCaptainType)
captainRouter.get('/:captainId', captainController.getCaptain)
captainRouter.patch('/:captainId', captainController.setCaptainType)

export default captainRouter
4 changes: 2 additions & 2 deletions server/routes/scout.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import scoutController from '../controllers/scout.controller.js'
const scoutRouter = Router()

scoutRouter.post('/', scoutController.insertScout)
scoutRouter.get('/:id', scoutController.getScout)
scoutRouter.put('/:id', scoutController.updateScout)
scoutRouter.get('/:scoutId', scoutController.getScout)
scoutRouter.put('/:scoutId', scoutController.updateScout)
scoutRouter.get('/', scoutController.getAllScouts)
scoutRouter.get('/unit/:unitCaptainId', scoutController.getScoutsInUnit)
scoutRouter.get(
Expand Down

0 comments on commit c6bded8

Please sign in to comment.