Add query param for searching by month
This commit is contained in:
@@ -6,7 +6,7 @@ import { db } from '@util/db';
|
|||||||
import { entryTable } from '@db/schema';
|
import { entryTable } from '@db/schema';
|
||||||
|
|
||||||
export async function GET({ request }: APIContext) {
|
export async function GET({ request }: APIContext) {
|
||||||
const { id, date } = getParams(request)
|
const { id, date, month } = getParams(request)
|
||||||
|
|
||||||
if (id && !isNaN(Number(id))) {
|
if (id && !isNaN(Number(id))) {
|
||||||
return getEntryByID(Number(id))
|
return getEntryByID(Number(id))
|
||||||
@@ -14,7 +14,11 @@ export async function GET({ request }: APIContext) {
|
|||||||
|
|
||||||
if (date) {
|
if (date) {
|
||||||
return getEntryByDate(date)
|
return getEntryByDate(date)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (month) {
|
||||||
|
return getEntryByMonth(month)
|
||||||
|
}
|
||||||
|
|
||||||
return httpResponse({ error: 'Failed to retrieve entry' }, 500);
|
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(
|
const entry = await db.select().from(entryTable).where(
|
||||||
sql`${entryTable.date} >= ${startDate.toISOString()}::timestamp AND ${entryTable.date} <= ${endDate.toISOString()}::timestamp`
|
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)
|
return httpResponse(entry, 200)
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
return httpResponse({'error': error}, 400)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user