Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add historical events list endpoint
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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)); | ||
} |