diff --git a/src/pages/api/entry/[id].ts b/src/pages/api/entry/[id].ts deleted file mode 100644 index a809fca..0000000 --- a/src/pages/api/entry/[id].ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { APIContext } from 'astro'; -import { eq } from 'drizzle-orm'; -import { db } from '../../../utils/db'; -import { entryTable } from '../../../db/schema'; -import { httpResponse } from '../../../utils/response'; - -export async function GET({ params }: APIContext) { - try { - const { id } = params - if (!id || isNaN(Number(id))) { - return httpResponse({'error': 'no id provided'}, 400) - } - return getEntry(Number(id)) - } catch (error) { - return httpResponse({ error: `Failed to retrieve entry: ${error}` }, 500); - } -} - -async function getEntry(id: number) { - try { - const entry = await db.select().from(entryTable).where(eq(entryTable.id, id)) - if (entry.length == 0) { - return httpResponse({'error': 'entry not found'}, 404) - } - return httpResponse(entry[0], 200) - } catch { - return httpResponse({'error': 'bad request'}, 400) - } - -} \ No newline at end of file diff --git a/src/pages/api/entry/date.ts b/src/pages/api/entry/date.ts deleted file mode 100644 index a7673ca..0000000 --- a/src/pages/api/entry/date.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { APIContext } from "astro"; -import { httpResponse } from "@util/response"; -import { getParams } from "@util/http"; - -export async function GET({ request }: APIContext) { - const params = getParams(request) - - return httpResponse({a: params}, 200) -} \ No newline at end of file diff --git a/src/pages/api/entry/index.ts b/src/pages/api/entry/index.ts new file mode 100644 index 0000000..821e8b1 --- /dev/null +++ b/src/pages/api/entry/index.ts @@ -0,0 +1,43 @@ +import type { APIContext } from "astro"; +import { httpResponse } from "@util/response"; +import { getParams } from "@util/http"; +import { eq, like } from 'drizzle-orm'; +import { db } from '@util/db'; +import { entryTable } from '@db/schema'; + +export async function GET({ request }: APIContext) { + const { id, date } = getParams(request) + + if (id && !isNaN(Number(id))) { + return getEntryByID(Number(id)) + } + + if (date) { + return getEntryByDate(date) + } + + return httpResponse({ error: 'Failed to retrieve entry' }, 500); +} + +async function getEntryByID(id: number) { + try { + const entry = await db.select().from(entryTable).where(eq(entryTable.id, id)) + if (entry.length == 0) { + return httpResponse({'error': 'entry not found'}, 404) + } + return httpResponse(entry[0], 200) + } catch { + return httpResponse({'error': 'bad request'}, 400) + } +} + +async function getEntryByDate(dateString: string) { + try { + const date = new Date(dateString) + + const entry = await db.select().from(entryTable).where(like(entryTable.content, `%${dateString}%`)) + + } catch(error) { + return httpResponse({'error': error}, 400) + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2cdd2f0..8834529 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "paths": { "@component/*": ["src/components/*.astro"], "@layout/*": ["src/layout/*.astro"], - "@util/*": ["src/utils/*"] + "@util/*": ["src/utils/*"], + "@db/*": ["src/db/*"] } } }