diff --git a/src/app/[locale]/(unauth)/api/guestbook/route.ts b/src/app/[locale]/(unauth)/api/guestbook/route.ts index 26f7134a5..45b355d50 100644 --- a/src/app/[locale]/(unauth)/api/guestbook/route.ts +++ b/src/app/[locale]/(unauth)/api/guestbook/route.ts @@ -3,19 +3,19 @@ import { NextResponse } from 'next/server'; import { z } from 'zod'; import { db } from '@/libs/DB'; -import { guestbookTable } from '@/models/Schema'; +import { guestbookSchema } from '@/models/Schema'; import { - DeleteGuestbookSchema, - EditGuestbookSchema, - GuestbookSchema, + DeleteGuestbookValidation, + EditGuestbookValidation, + GuestbookValidation, } from '@/validations/GuestbookValidation'; export const POST = async (request: Request) => { try { const json = await request.json(); - const body = GuestbookSchema.parse(json); + const body = GuestbookValidation.parse(json); - const guestbook = await db.insert(guestbookTable).values(body).returning(); + const guestbook = await db.insert(guestbookSchema).values(body).returning(); return NextResponse.json({ id: guestbook[0]?.id, @@ -32,15 +32,15 @@ export const POST = async (request: Request) => { export const PUT = async (request: Request) => { try { const json = await request.json(); - const body = EditGuestbookSchema.parse(json); + const body = EditGuestbookValidation.parse(json); await db - .update(guestbookTable) + .update(guestbookSchema) .set({ ...body, updatedAt: sql`(strftime('%s', 'now'))`, }) - .where(eq(guestbookTable.id, body.id)) + .where(eq(guestbookSchema.id, body.id)) .run(); return NextResponse.json({}); @@ -56,9 +56,12 @@ export const PUT = async (request: Request) => { export const DELETE = async (request: Request) => { try { const json = await request.json(); - const body = DeleteGuestbookSchema.parse(json); + const body = DeleteGuestbookValidation.parse(json); - await db.delete(guestbookTable).where(eq(guestbookTable.id, body.id)).run(); + await db + .delete(guestbookSchema) + .where(eq(guestbookSchema.id, body.id)) + .run(); return NextResponse.json({}); } catch (error) { diff --git a/src/app/[locale]/(unauth)/guestbook/page.tsx b/src/app/[locale]/(unauth)/guestbook/page.tsx index e9d24f51c..29624b100 100644 --- a/src/app/[locale]/(unauth)/guestbook/page.tsx +++ b/src/app/[locale]/(unauth)/guestbook/page.tsx @@ -5,7 +5,7 @@ import { DeleteGuestbookEntry } from '@/components/DeleteGuestbookEntry'; import { EditableGuestbookEntry } from '@/components/EditableGuestbookEntry'; import { GuestbookForm } from '@/components/GuestbookForm'; import { db } from '@/libs/DB'; -import { guestbookTable } from '@/models/Schema'; +import { guestbookSchema } from '@/models/Schema'; export async function generateMetadata({ params: { locale }, @@ -21,7 +21,7 @@ export async function generateMetadata({ } const Guestbook = async () => { - const guestbook = await db.select().from(guestbookTable).all(); + const guestbook = await db.select().from(guestbookSchema).all(); const t = await getTranslations('Guestbook'); return ( diff --git a/src/components/GuestbookForm.tsx b/src/components/GuestbookForm.tsx index 686fdfc16..c3c09eb93 100644 --- a/src/components/GuestbookForm.tsx +++ b/src/components/GuestbookForm.tsx @@ -6,13 +6,13 @@ import { useTranslations } from 'next-intl'; import { useForm } from 'react-hook-form'; import type { z } from 'zod'; -import { GuestbookSchema } from '@/validations/GuestbookValidation'; +import { GuestbookValidation } from '@/validations/GuestbookValidation'; type IGuestbookFormProps = | { edit: true; id: number; - defaultValues: z.infer; + defaultValues: z.infer; handleStopEditing: () => void; } | { edit?: false }; @@ -24,8 +24,8 @@ const GuestbookForm = (props: IGuestbookFormProps) => { reset, setFocus, formState: { errors }, - } = useForm>({ - resolver: zodResolver(GuestbookSchema), + } = useForm>({ + resolver: zodResolver(GuestbookValidation), defaultValues: props.edit ? props.defaultValues : undefined, }); const router = useRouter(); diff --git a/src/models/Schema.ts b/src/models/Schema.ts index d8ef9f5d1..0941fe1cf 100644 --- a/src/models/Schema.ts +++ b/src/models/Schema.ts @@ -1,7 +1,7 @@ import { sql } from 'drizzle-orm'; import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'; -export const guestbookTable = sqliteTable('guestbook', { +export const guestbookSchema = sqliteTable('guestbook', { id: integer('id').primaryKey(), username: text('username').notNull(), body: text('body').notNull(), diff --git a/src/validations/GuestbookValidation.ts b/src/validations/GuestbookValidation.ts index 5e6bfe8be..67279150c 100644 --- a/src/validations/GuestbookValidation.ts +++ b/src/validations/GuestbookValidation.ts @@ -1,16 +1,16 @@ import { z } from 'zod'; -export const GuestbookSchema = z.object({ +export const GuestbookValidation = z.object({ username: z.string().min(1), body: z.string().min(1), }); -export const EditGuestbookSchema = z.object({ +export const EditGuestbookValidation = z.object({ id: z.coerce.number(), username: z.string().min(1), body: z.string().min(1), }); -export const DeleteGuestbookSchema = z.object({ +export const DeleteGuestbookValidation = z.object({ id: z.coerce.number(), });