Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make batch transactions actually work
  • Loading branch information
regalijan committed Nov 20, 2024
1 parent 0829e66 commit c9dcbb3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 66 deletions.
34 changes: 11 additions & 23 deletions functions/api/events-team/events/[id]/certify.ts
@@ -1,37 +1,25 @@
import { jsonError } from "../../../../common.js";

export async function onRequestPost(context: RequestContext) {
if (context.data.event.reached_minimum_player_count)
const { event } = context.data;
if (event.reached_minimum_player_count)
return jsonError("This event is already certified", 400);

const { D1 } = context.env;

await D1.exec("BEGIN TRANSACTION;");

try {
const row = await D1.prepare(
"UPDATE events SET reached_minimum_player_count = 1 WHERE id = ? RETURNING created_by;",
)
.bind(context.data.event.id)
.first();

if (!row) {
await D1.exec("ROLLBACK;");
return jsonError("Event does not exist", 404);
}

await D1.prepare("UPDATE et_members SET points = points + 10 WHERE id = ?;")
.bind(row.created_by)
.run();
await D1.exec("COMMIT;");
await D1.batch([
D1.prepare(
"UPDATE events SET reached_minimum_player_count = 1 WHERE id = ?",
).bind(event.id),
D1.prepare(
"UPDATE et_members SET points = points + 10 WHERE id = ?;",
).bind(event.created_by),
]);
} catch (e) {
console.log(e);
await D1.exec("ROLLBACK;");

return jsonError(
"Failed to certify event because the event or points could not be updated",
500,
);
return jsonError("Failed to complete batch transaction", 500);
}

return new Response(null, {
Expand Down
31 changes: 10 additions & 21 deletions functions/api/events-team/events/[id]/complete.ts
@@ -1,33 +1,22 @@
import { jsonError } from "../../../../common.js";

export async function onRequestPost(context: RequestContext) {
const id = context.params.id as string;
const { D1 } = context.env;

await D1.exec("BEGIN TRANSACTION;");
const { event } = context.data;

try {
const row = await D1.prepare(
"UPDATE events SET performed_at = ? WHERE id = ? RETURNING created_by;",
)
.bind(Date.now(), id)
.first();

if (!row) {
await D1.exec("ROLLBACK;");
return jsonError("Event does not exist", 404);
}

await D1.prepare("UPDATE et_members SET points = points + 10 WHERE id = ?;")
.bind(row.created_by)
.run();

await D1.exec("COMMIT;");
await D1.batch([
D1.prepare("UPDATE events SET performed_at = ? WHERE id = ?;").bind(
event.id,
),
D1.prepare(
"UPDATE et_members SET points = points + 10 WHERE id = ?;",
).bind(event.created_by),
]);
} catch (e) {
console.log(e);
await D1.exec("ROLLBACK;");

return jsonError("Failed to update event data", 500);
return jsonError("Failed to complete batch transaction", 500);
}

return new Response(null, {
Expand Down
32 changes: 10 additions & 22 deletions functions/api/events-team/events/[id]/forgotten.ts
@@ -1,34 +1,22 @@
import { jsonError } from "../../../../common.js";

export async function onRequestPost(context: RequestContext) {
const id = context.params.id as string;
const { D1 } = context.env;

await D1.exec("BEGIN TRANSACTION;");
const { event } = context.data;

try {
const row = await D1.prepare(
"UPDATE events SET performed_at = 0 WHERE id = ? RETURNING created_by;",
)
.bind(id)
.first();

if (!row) {
await D1.exec("ROLLBACK;");
return jsonError("Nonexistent event cannot be marked as forgotten.", 404);
}

await D1.prepare("UPDATE et_members SET points = points - 5 WHERE id = ?;")
.bind(row.created_by)
.run();
await D1.batch([
D1.prepare("UPDATE events SET performed_at = 0 WHERE id = ?;").bind(
event.id,
),
D1.prepare(
"UPDATE et_members SET points = points - 5 WHERE id = ?;",
).bind(event.created_by),
]);
} catch (e) {
console.log(e);
await D1.exec("ROLLBACK;");

return jsonError(
"Failed to mark event as forgotten, points were not changed.",
500,
);
return jsonError("Failed to complete batch transaction", 500);
}

return new Response(null, {
Expand Down

0 comments on commit c9dcbb3

Please sign in to comment.