Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Further add to new event endpoint
  • Loading branch information
regalijan committed Jan 10, 2024
1 parent 4072ad2 commit cc7f8db
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions functions/api/events-team/events/new.ts
Expand Up @@ -15,9 +15,57 @@ export async function onRequestPost(context: RequestContext) {
parseInt(day.toString()) !== day ||
typeof details !== "string" ||
!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);


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,
});
}

0 comments on commit cc7f8db

Please sign in to comment.