endpoint to get one entry by id
This commit is contained in:
30
src/pages/api/entry/[id].ts
Normal file
30
src/pages/api/entry/[id].ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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) {
|
||||
return httpResponse({'error': 'no id provided'}, 400)
|
||||
}
|
||||
return getEntry(id)
|
||||
} catch (error) {
|
||||
return httpResponse({ error: `Failed to retrieve entry: ${error}` }, 500);
|
||||
}
|
||||
}
|
||||
|
||||
async function getEntry(id) {
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user