From 192a640f862a4098d7aedc46306730070819d164 Mon Sep 17 00:00:00 2001 From: akram Date: Thu, 28 Dec 2023 23:48:27 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20feat:=20Modify=20the?= =?UTF-8?q?=20get=20sector=20attendance=20to=20match=20the=20required=20be?= =?UTF-8?q?haviour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/attendance.controller.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/controllers/attendance.controller.js b/server/controllers/attendance.controller.js index 0331c7a0..f9ebdec5 100644 --- a/server/controllers/attendance.controller.js +++ b/server/controllers/attendance.controller.js @@ -74,11 +74,10 @@ const attendanceController = { const { baseName, suffixName, weekNumber, termNumber } = req.query const result = await db.query(` - SELECT "ScoutAttendance".* - FROM "Scout", "ScoutAttendance" - WHERE "Scout"."sectorBaseName" = $1 AND "Scout"."sectorSuffixName" = $2 + SELECT "Scout".*, "ScoutAttendance".* FROM "Scout" LEFT JOIN "ScoutAttendance" ON "Scout"."scoutId" = "ScoutAttendance"."scoutId" AND "ScoutAttendance"."weekNumber" = $3 AND "ScoutAttendance"."termNumber" = $4 - AND "ScoutAttendance"."scoutId" = "Scout"."scoutId"; + INNER JOIN "Sector" ON "Sector"."baseName" = "Scout"."sectorBaseName" AND "Sector"."suffixName" = "Scout"."sectorSuffixName" + WHERE "Sector"."baseName" = $1 AND "Sector"."suffixName" = $2; `, [baseName, suffixName, weekNumber, termNumber]) From 6b9f91bbddc72ab03d9b12c325b93c40d0f82e63 Mon Sep 17 00:00:00 2001 From: akram Date: Fri, 29 Dec 2023 00:17:04 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20feat:=20Implement=20up?= =?UTF-8?q?sert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/attendance.controller.js | 71 +++++++++------------ server/routes/attendance.route.js | 2 +- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/server/controllers/attendance.controller.js b/server/controllers/attendance.controller.js index f9ebdec5..2815addc 100644 --- a/server/controllers/attendance.controller.js +++ b/server/controllers/attendance.controller.js @@ -4,57 +4,48 @@ 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) => { + upsertAttendance: async (req, res) => { try { - const { scoutId, weekNumber, termNumber, attendanceStatus } = req.body + const { attendanceRecords } = 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) { + if (attendanceRecords === 0) { return res.status(404).json({ - error: "No scout exist with this id" + error: "No records were found" }) } - - // 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" - }) + let result = []; + if (!attendanceRecords[0].attendanceStatus) //insert + { + 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] + ) + ); + } } - - // 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" - }) + else { //update + 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] + ) + ); + } } // Return a success message res.status(200).json({ message: "Successful insertion", - body: result.rows, - count: result.rowCount, + body: result, + count: result.length, }) } catch (error) { diff --git a/server/routes/attendance.route.js b/server/routes/attendance.route.js index 56bd2e54..24f0ff57 100644 --- a/server/routes/attendance.route.js +++ b/server/routes/attendance.route.js @@ -4,7 +4,7 @@ import attendanceController from "../controllers/attendance.controller.js" const attendanceRouter = Router() // Insert a new attendance for a scout -attendanceRouter.post('/', attendanceController.insertAttendance) +attendanceRouter.post('/', attendanceController.upsertAttendance) attendanceRouter.get('/sector/all', attendanceController.getSectorAttendance) attendanceRouter.get('/:scoutId/:weekNumber/:termNumber', attendanceController.getScoutAttendance)