Skip to content
Permalink
84e7df6c30
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
26 lines (19 sloc) 1.01 KB
import { jsonError, jsonResponse } from "../../../common.js";
export async function onRequestGet(context: RequestContext) {
const url = new URL(context.request.url);
const month = parseInt(url.searchParams.get("month") ?? "");
const year = parseInt(url.searchParams.get("year") ?? "");
if (isNaN(month) || isNaN(year))
return jsonError("Invalid month or year", 400);
const currentYear = new Date().getUTCFullYear();
const currentMonth = new Date().getUTCMonth() + 1;
if (currentYear < year || (currentYear === year && currentMonth < month))
return jsonError("Cannot get events for a time in the future", 400);
const eventRecords = await context.env.D1.prepare(
"SELECT answer, approved, created_by, day, details, month, pending, performed_at, type, year FROM events WHERE month = ? AND year = ? ORDER BY day ASC;",
)
.bind(month, year)
.all();
if (!eventRecords.success) return jsonError("Failed to retrieve events", 400);
return jsonResponse(JSON.stringify(eventRecords.results));
}