Support real-time point updates
This commit is contained in:
parent
5f3ffc0fdf
commit
d1127968c8
@ -4,11 +4,35 @@ export async function onRequestPost(context: RequestContext) {
|
|||||||
if (context.data.event.reached_minimum_player_count)
|
if (context.data.event.reached_minimum_player_count)
|
||||||
return jsonError("This event is already certified", 400);
|
return jsonError("This event is already certified", 400);
|
||||||
|
|
||||||
await context.env.D1.prepare(
|
const { D1 } = context.env;
|
||||||
"UPDATE events SET reached_minimum_player_count = 1 WHERE id = ?;",
|
|
||||||
)
|
await D1.exec("BEGIN TRANSACTION;");
|
||||||
.bind(context.data.event.id)
|
|
||||||
.run();
|
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;");
|
||||||
|
} 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 new Response(null, {
|
return new Response(null, {
|
||||||
status: 204,
|
status: 204,
|
||||||
|
@ -1,11 +1,34 @@
|
|||||||
|
import { jsonError } from "../../../../common.js";
|
||||||
|
|
||||||
export async function onRequestPost(context: RequestContext) {
|
export async function onRequestPost(context: RequestContext) {
|
||||||
const id = context.params.id as string;
|
const id = context.params.id as string;
|
||||||
|
const { D1 } = context.env;
|
||||||
|
|
||||||
await context.env.D1.prepare(
|
await D1.exec("BEGIN TRANSACTION;");
|
||||||
"UPDATE events SET performed_at = ? WHERE id = ?;",
|
|
||||||
)
|
try {
|
||||||
.bind(Date.now(), id)
|
const row = await D1.prepare(
|
||||||
.run();
|
"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;");
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
await D1.exec("ROLLBACK;");
|
||||||
|
|
||||||
|
return jsonError("Failed to update event data", 500);
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 204,
|
status: 204,
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
|
import { jsonError } from "../../../../common.js";
|
||||||
|
|
||||||
export async function onRequestPost(context: RequestContext) {
|
export async function onRequestPost(context: RequestContext) {
|
||||||
const id = context.params.id as string;
|
const id = context.params.id as string;
|
||||||
|
const { D1 } = context.env;
|
||||||
|
|
||||||
await context.env.D1.prepare(
|
await D1.exec("BEGIN TRANSACTION;");
|
||||||
"UPDATE events SET performed_at = 0 WHERE id = ?;",
|
|
||||||
)
|
try {
|
||||||
.bind(id)
|
const row = await D1.prepare(
|
||||||
.run();
|
"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();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
await D1.exec("ROLLBACK;");
|
||||||
|
|
||||||
|
return jsonError(
|
||||||
|
"Failed to mark event as forgotten, points were not changed.",
|
||||||
|
500,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 204,
|
status: 204,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user