Skip to content

Commit

Permalink
⚡️ feat: Implement upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
akramhany committed Dec 28, 2023
1 parent 192a640 commit 6b9f91b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
71 changes: 31 additions & 40 deletions server/controllers/attendance.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/attendance.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 6b9f91b

Please sign in to comment.