commit d8d967656b07f8fa2b1f2f37627ead006f5df957
parent c66645a914d70b1090625c7aeee6dafd1c12425b
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date: Mon, 23 Sep 2024 23:18:32 +0200
fix: remove duplicat removeAthlete
Diffstat:
2 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/src/api/db.cjs b/src/api/db.cjs
@@ -225,6 +225,7 @@ async function removeAthlete(id) {
try {
const athlete = await sql`
delete from athletes where id = ${id}
+ returning *
`
return athlete
} catch (error) {
@@ -238,6 +239,7 @@ async function removeAthleteFromHeat(startlistId) {
const startlist = await sql`
delete from startlist
where id = ${startlistId}
+ returning *
`
return startlist
} catch (error) {
@@ -246,19 +248,6 @@ async function removeAthleteFromHeat(startlistId) {
}
}
-async function removeAthlete(athleteId) {
- try {
- const athlete = await sql`
- delete from athletes
- where id = ${athleteId}
- `
- return athlete
- } catch (error) {
- console.log('Error occurred in removeAthlete:', error);
- throw error
- }
-}
-
async function newHeat(name, heatLocation, plannedStart) {
try {
const heat = await sql`
@@ -285,6 +274,7 @@ async function removeHeat(heatId) {
try {
const heat = await sql`
delete from heats where id = ${heatId}
+ returning *
`
return heat
} catch (error) {
diff --git a/src/api/server.cjs b/src/api/server.cjs
@@ -483,7 +483,7 @@ server.on('request', async (req, res) => {
input.heat_id,
);
if (heats.length < 1) {
- throw new Error("Heat not created")
+ throw new Error("Heat not found")
}
res.end(JSON.stringify({
@@ -507,9 +507,13 @@ server.on('request', async (req, res) => {
input = JSON.parse(b);
console.log(' removeHeat request for:', input);
- await db.removeHeat(input.heat_id)
+ const heats = await db.removeHeat(input.heat_id)
+ if (heats.length < 1) {
+ throw new Error("Heat not removed")
+ }
res.end(JSON.stringify({
message: 'Heat removed',
+ data: heats[0],
}));
} catch (error) {
serverError(res, error);
@@ -557,9 +561,14 @@ server.on('request', async (req, res) => {
input = JSON.parse(b);
console.log(' removeAthleteFromHeat request for:', input);
- await db.removeAthleteFromHeat(input.startlist_id)
+ const startlist = await db.removeAthleteFromHeat(input.startlist_id)
+ if (startlist.length < 1) {
+ throw new Error("Startlist not updated")
+ }
+
res.end(JSON.stringify({
- message: 'Athlete removed from startlist'
+ message: 'Athlete removed from startlist',
+ data: startlist[0],
}));
} catch (error) {
serverError(res, error);
@@ -610,9 +619,13 @@ server.on('request', async (req, res) => {
input = JSON.parse(b);
console.log(' removeAthlete request for:', input);
- await db.removeAthlete(input.athlete_id)
+ const athlete = await db.removeAthlete(input.athlete_id)
+ if (athlete.length < 1) {
+ throw new Error("Athlete not removed")
+ }
res.end(JSON.stringify({
- message: 'Athlete removed'
+ message: 'Athlete removed',
+ data: athlete[0],
}));
} catch (error) {
serverError(res, error);