diff --git a/src/pages/api/entry/index.ts b/src/pages/api/entry/index.ts index b8c67b8..f387c5f 100644 --- a/src/pages/api/entry/index.ts +++ b/src/pages/api/entry/index.ts @@ -6,7 +6,7 @@ import { db } from '@util/db'; import { entryTable } from '@db/schema'; export async function GET({ request }: APIContext) { - const { id, date } = getParams(request) + const { id, date, month } = getParams(request) if (id && !isNaN(Number(id))) { return getEntryByID(Number(id)) @@ -14,7 +14,11 @@ export async function GET({ request }: APIContext) { if (date) { return getEntryByDate(date) - } + } + + if (month) { + return getEntryByMonth(month) + } return httpResponse({ error: 'Failed to retrieve entry' }, 500); } @@ -43,13 +47,26 @@ async function getEntryByDate(dateString: string) { const entry = await db.select().from(entryTable).where( sql`${entryTable.date} >= ${startDate.toISOString()}::timestamp AND ${entryTable.date} <= ${endDate.toISOString()}::timestamp` ) - - if (entry.length == 0) { - return httpResponse({'error': 'entry not found'}, 404) - } return httpResponse(entry, 200) } catch(error) { return httpResponse({'error': error}, 400) } +} + +async function getEntryByMonth(monthString: string) { + try { + const [year, month] = monthString.split('-').map(Number) + + const startDate = new Date(year, month - 1, 1, 0, 0, 0, 0) + const endDate = new Date(year, month, 1, 0, 0, 0, 0) + + const entries = await db.select().from(entryTable).where( + sql`${entryTable.date} >= ${startDate.toISOString()}::timestamp AND ${entryTable.date} < ${endDate.toISOString()}::timestamp` + ) + + return httpResponse(entries, 200) + } catch(error) { + return httpResponse({'error': error}, 400) + } } \ No newline at end of file