Skip to content

Commit

Permalink
Merge pull request #212 from luishdez/confusing-naming-between-Schema…
Browse files Browse the repository at this point in the history
…-and-Validation.-#208

refactor: naming between Schema and Validation
  • Loading branch information
ixartz authored Dec 20, 2023
2 parents 2f7b89c + 86f81a1 commit 25d2750
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
25 changes: 14 additions & 11 deletions src/app/[locale]/(unauth)/api/guestbook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({});
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/[locale]/(unauth)/guestbook/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 (
Expand Down
8 changes: 4 additions & 4 deletions src/components/GuestbookForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof GuestbookSchema>;
defaultValues: z.infer<typeof GuestbookValidation>;
handleStopEditing: () => void;
}
| { edit?: false };
Expand All @@ -24,8 +24,8 @@ const GuestbookForm = (props: IGuestbookFormProps) => {
reset,
setFocus,
formState: { errors },
} = useForm<z.infer<typeof GuestbookSchema>>({
resolver: zodResolver(GuestbookSchema),
} = useForm<z.infer<typeof GuestbookValidation>>({
resolver: zodResolver(GuestbookValidation),
defaultValues: props.edit ? props.defaultValues : undefined,
});
const router = useRouter();
Expand Down
2 changes: 1 addition & 1 deletion src/models/Schema.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions src/validations/GuestbookValidation.ts
Original file line number Diff line number Diff line change
@@ -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(),
});

0 comments on commit 25d2750

Please sign in to comment.