Further add to new event endpoint
This commit is contained in:
parent
4072ad241c
commit
cc7f8db2bb
@ -15,9 +15,57 @@ export async function onRequestPost(context: RequestContext) {
|
|||||||
parseInt(day.toString()) !== day ||
|
parseInt(day.toString()) !== day ||
|
||||||
typeof details !== "string" ||
|
typeof details !== "string" ||
|
||||||
!details.length ||
|
!details.length ||
|
||||||
!["fotd", "gamenight", "rotw", "qotd"].includes(type)
|
!["fotd", "gamenight", "rotw", "qotd"].includes(type) ||
|
||||||
|
(type === "rotw" && !context.data.body.answer)
|
||||||
)
|
)
|
||||||
return jsonError("Invalid body", 400);
|
return jsonError("Invalid body", 400);
|
||||||
|
|
||||||
|
if (
|
||||||
|
await context.env.D1.prepare(
|
||||||
|
"SELECT * FROM events WHERE day = ? AND month = ? AND type = ? AND year = ?;",
|
||||||
|
)
|
||||||
|
.bind(day, currentMonth, type, currentYear)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
return jsonError(
|
||||||
|
"Event with that type already exists for the specified date",
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
|
||||||
|
const id = `${now.getTime()}${crypto.randomUUID().replaceAll("-", "")}`;
|
||||||
|
|
||||||
|
await context.env.D1.prepare(
|
||||||
|
"INSERT INTO events (created_at, created_by, day, id, month, type, year) VALUES (?, ?, ?, ?, ?, ?, ?);",
|
||||||
|
)
|
||||||
|
.bind(
|
||||||
|
now.getTime(),
|
||||||
|
context.data.current_user.id,
|
||||||
|
day,
|
||||||
|
id,
|
||||||
|
currentMonth,
|
||||||
|
type,
|
||||||
|
currentYear,
|
||||||
|
)
|
||||||
|
.run();
|
||||||
|
|
||||||
|
await context.env.DATA.put(
|
||||||
|
`event_${id}`,
|
||||||
|
JSON.stringify({
|
||||||
|
answer: context.data.body.answer,
|
||||||
|
created_at: now.getTime(),
|
||||||
|
created_by: context.data.current_user.id,
|
||||||
|
day,
|
||||||
|
details,
|
||||||
|
month: currentMonth,
|
||||||
|
pending: true,
|
||||||
|
year: currentYear,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
expirationTtl: 15552000,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return new Response(null, {
|
||||||
|
status: 204,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user