Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend functionalities #52

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions server/controllers/captainAttendance.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import db from "../database/db.js"

const captainAttendanceController = {
upsertAttendance: async (req, res) => {
try {
// NOTE: attendanceRecords must be sent as an array of objects in the request body
const { attendanceRecords } = req.body

if (attendanceRecords === 0) {
return res.status(404).json({
error: "No records were found"
})
}

const weekNumber = attendanceRecords[0].weekNumber;
const termNumber = attendanceRecords[0].termNumber;

// Query to check if there are an exisiting records for this week & term or not
const prevRecords = await db.query(`
SELECT *
FROM "CaptainAttendance"
WHERE "weekNumber" = $1 AND "termNumber" = $2;
`,
[weekNumber, termNumber])

let result = [];

// If the attendance records already exists then update them, if not insert a new records
if (prevRecords.rowCount === 0)
{
for (let i = 0; i < attendanceRecords.length; i++) {

// Insert a new record from the attendance array into the databse
const queryResult = await db.query(`
INSERT INTO "CaptainAttendance" VALUES ($1, $2, $3, $4)
RETURNING *;
`,
[attendanceRecords[i].captainId, attendanceRecords[i].weekNumber, attendanceRecords[i].termNumber, attendanceRecords[i].attendanceStatus]
)

// Add the newly inserted record to the result array to return back
result.push(queryResult.rows[0]);
}
}
else {
for (let i = 0; i < attendanceRecords.length; i++) {

// Update the current record from the array
const queryResult = await db.query(`
UPDATE "CaptainAttendance" SET "attendanceStatus" = $4
WHERE "captainId" = $1 AND "weekNumber" = $2 AND "termNumber" = $3
RETURNING *;
`,
[attendanceRecords[i].captainId, attendanceRecords[i].weekNumber, attendanceRecords[i].termNumber, attendanceRecords[i].attendanceStatus]
)

// Add the info about the updated record into the result array
result.push(queryResult.rows[0]);
}
}

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

} catch (error) {
console.log(error)
res.status(500).json({
error: 'An error occured while inserting/updating captain Attendance',
body: error,
})
}
},
getSectorAttendance: async (req, res) => {
try {
const { baseName, suffixName, weekNumber, termNumber } = req.query

const result = await db.query(`
SELECT "Captain".*, "CaptainAttendance".* FROM "Captain" LEFT JOIN "CaptainAttendance" ON "Captain"."captainId" = "CaptainAttendance"."captainId"
AND "CaptainAttendance"."weekNumber" = $3 AND "CaptainAttendance"."termNumber" = $4
INNER JOIN "Sector" ON "Sector"."baseName" = "Captain"."rSectorBaseName" AND "Sector"."suffixName" = "Captain"."rSectorSuffixName"
WHERE "Sector"."baseName" = $1 AND "Sector"."suffixName" = $2;
`,
[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 captain Attendance',
body: error,
})
}
},
getCaptainAttendance: async (req, res) => {
try {
const { captainId, weekNumber, termNumber } = req.params

const result = await db.query(`
SELECT *
FROM "CaptainAttendance"
WHERE "captainId" = $1 AND "weekNumber" = $2 AND "termNumber" = $3
`,
[captainId, 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 captain Attendance',
body: error,
})
}
}
}

export default captainAttendanceController;
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
import db from "../database/db.js"

const attendanceController = {
const scoutAttendanceController = {
// @desc Insert a new attendance record for a scout in a certain sector
// @route POST /api/attendance/
// @route POST /api/sectorAttendance/
// @access Private
upsertAttendance: async (req, res) => {
try {
const { attendanceRecords } = req.body
// NOTE: attendanceRecords must be sent as an array of objects in the request body
const { attendanceRecords } = req.body

if (attendanceRecords === 0) {
return res.status(404).json({
error: "No records were found"
})
}

const weekNumber = attendanceRecords[0].weekNumber;
const termNumber = attendanceRecords[0].termNumber;

// Query to check if there are an exisiting records for this week & term or not
const prevRecords = await db.query(`
SELECT *
FROM "ScoutAttendance"
WHERE "weekNumber" = $1 AND "termNumber" = $2;
`,
[weekNumber, termNumber])

let result = [];
if (!attendanceRecords[0].attendanceStatus) //insert

// If the attendance records already exists then update them, if not insert a new records
if (prevRecords.rowCount === 0)
{
for (let i = 0; i < attendanceRecords.length; i++) {
result.push(
await db.query(`
INSERT INTO attendanceStatus VALUES ($1, $2, $3, $4)
RETURNING *;
`,
[attendanceRecords[i].scoutId, attendanceRecords[i].weekNumber, attendanceRecords[i].termNumber, attendanceRecords[i].attendanceStatus]
)
);
for (let i = 0; i < attendanceRecords.length; i++) {

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

// Add the newly inserted record to the result array to return back
result.push(queryResult.rows[0]);
}
}
else { //update
else {
for (let i = 0; i < attendanceRecords.length; i++) {
result.push(
await db.query(`
UPDATE attendanceStatus SET "attendanceStatus" = $4
WHERE "scoutId" = $1 AND "weekNumber" = $2 AND "termNumber" = $3
RETURNING *;
`,
[attendanceRecords[i].scoutId, attendanceRecords[i].weekNumber, attendanceRecords[i].termNumber, attendanceRecords[i].attendanceStatus]
)
);

// Update the current record from the array
const queryResult = await db.query(`
UPDATE "ScoutAttendance" SET "attendanceStatus" = $4
WHERE "scoutId" = $1 AND "weekNumber" = $2 AND "termNumber" = $3
RETURNING *;
`,
[attendanceRecords[i].scoutId, attendanceRecords[i].weekNumber, attendanceRecords[i].termNumber, attendanceRecords[i].attendanceStatus]
)

// Add the info about the updated record into the result array
result.push(queryResult.rows[0]);
}
}

Expand All @@ -58,7 +79,7 @@ const attendanceController = {
},

// @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
// @route GET /api/sectorAttendance/sector/all
// @access Private
getSectorAttendance: async (req, res) => {
try {
Expand Down Expand Up @@ -94,7 +115,7 @@ const attendanceController = {
},

// @desc Get attendance records for a certain scout in a certain week & term
// @route GET /api/attendance/:scoutId/:weekNumber/:termNumber
// @route GET /api/sectorAttendance/:scoutId/:weekNumber/:termNumber
// @access Private
getScoutAttendance: async (req, res) => {
try {
Expand Down Expand Up @@ -123,4 +144,4 @@ const attendanceController = {
}
}

export default attendanceController;
export default scoutAttendanceController;
6 changes: 4 additions & 2 deletions server/routes/api.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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'
import scoutAttendanceRouter from './scoutAttendance.route.js'
import captainAttendanceRouter from './captainAttendance.route.js'
const apiRouter = Router()

apiRouter.use('/auth', authRouter)
Expand All @@ -25,6 +26,7 @@ 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)
apiRouter.use('/scoutAttendance', authMiddleware, scoutAttendanceRouter)
apiRouter.use('/captainAttendance', authMiddleware, captainAttendanceRouter)

export default apiRouter
11 changes: 0 additions & 11 deletions server/routes/attendance.route.js

This file was deleted.

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

const captainAttendanceRouter = Router()

// Insert a new attendance for a captain
captainAttendanceRouter.post('/', captainAttendanceController.upsertAttendance)
captainAttendanceRouter.get('/sector/all', captainAttendanceController.getSectorAttendance)
captainAttendanceRouter.get('/:captainId/:weekNumber/:termNumber', captainAttendanceController.getCaptainAttendance)

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

const scoutAttendanceRouter = Router()

// Insert a new attendance for a scout
scoutAttendanceRouter.post('/', scoutAttendanceController.upsertAttendance)
scoutAttendanceRouter.get('/sector/all', scoutAttendanceController.getSectorAttendance)
scoutAttendanceRouter.get('/:scoutId/:weekNumber/:termNumber', scoutAttendanceController.getScoutAttendance)

export default scoutAttendanceRouter;