Search works with the timezone in the db
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { APIContext } from "astro";
|
||||
import { httpResponse } from "@util/response";
|
||||
import { getParams } from "@util/http";
|
||||
import { eq, like } from 'drizzle-orm';
|
||||
import { eq, like, sql } from 'drizzle-orm';
|
||||
import { db } from '@util/db';
|
||||
import { entryTable } from '@db/schema';
|
||||
|
||||
@@ -33,10 +33,22 @@ async function getEntryByID(id: number) {
|
||||
|
||||
async function getEntryByDate(dateString: string) {
|
||||
try {
|
||||
const date = new Date(dateString)
|
||||
// timezones suck
|
||||
const startDate = new Date(dateString)
|
||||
startDate.setHours(0, 0, 0, 0)
|
||||
|
||||
const endDate = new Date(dateString)
|
||||
endDate.setHours(23, 59, 59, 999)
|
||||
|
||||
const entry = await db.select().from(entryTable).where(like(entryTable.content, `%${dateString}%`))
|
||||
const entry = await db.select().from(entryTable).where(
|
||||
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)
|
||||
} catch(error) {
|
||||
return httpResponse({'error': error}, 400)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user