refactor both entry endpoints to one

This commit is contained in:
2026-01-15 17:05:08 +13:00
parent cb027a6e92
commit 22772ad436
4 changed files with 45 additions and 40 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}

View File

@@ -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)
}
}

View File

@@ -7,7 +7,8 @@
"paths": { "paths": {
"@component/*": ["src/components/*.astro"], "@component/*": ["src/components/*.astro"],
"@layout/*": ["src/layout/*.astro"], "@layout/*": ["src/layout/*.astro"],
"@util/*": ["src/utils/*"] "@util/*": ["src/utils/*"],
"@db/*": ["src/db/*"]
} }
} }
} }