From aa209bc0a887c30d44681bd14e5dd1e1d2a6f091 Mon Sep 17 00:00:00 2001 From: ChrQR Date: Sun, 2 Jun 2024 12:43:10 +0200 Subject: [PATCH] not throwing error on duplicates, but also not creating zoderror --- src/server/actions/addCountry.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/server/actions/addCountry.ts b/src/server/actions/addCountry.ts index 126bab1..810e745 100644 --- a/src/server/actions/addCountry.ts +++ b/src/server/actions/addCountry.ts @@ -4,6 +4,7 @@ import { revalidatePath } from "next/cache"; import { db } from "../db"; import { countries } from "../db/schema"; import { ZodError, z } from "zod"; +import { eq } from "drizzle-orm"; const schema = z.object({ name: z.string().min(1, { message: "Name is required" }), @@ -11,10 +12,21 @@ const schema = z.object({ export async function addCountry(prevstate: any, formData: FormData) { const name = formData.get("name") as string; + const exists = await db + .select({ name: countries.name }) + .from(countries) + .where(eq(countries.name, name)); try { schema.parse({ name, }); + if (!exists[0]) { + await db + .insert(countries) + .values({ name }) + .returning({ name: countries.name }); + revalidatePath("/"); + } return { message: "success", errors: undefined,