Add query param for searching by month

This commit is contained in:
2026-01-15 17:40:46 +13:00
parent 798f2c1e44
commit c1af1e8ebb

View File

@@ -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))
@@ -16,6 +16,10 @@ export async function GET({ request }: APIContext) {
return getEntryByDate(date)
}
if (month) {
return getEntryByMonth(month)
}
return httpResponse({ error: 'Failed to retrieve entry' }, 500);
}
@@ -44,12 +48,25 @@ async function getEntryByDate(dateString: string) {
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)
}
}