refactor both entry endpoints to one
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
43
src/pages/api/entry/index.ts
Normal file
43
src/pages/api/entry/index.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,8 @@
|
||||
"paths": {
|
||||
"@component/*": ["src/components/*.astro"],
|
||||
"@layout/*": ["src/layout/*.astro"],
|
||||
"@util/*": ["src/utils/*"]
|
||||
"@util/*": ["src/utils/*"],
|
||||
"@db/*": ["src/db/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user