not throwing error on duplicates, but also not creating zoderror
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m22s
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m22s
This commit is contained in:
parent
a065e6b592
commit
aa209bc0a8
@ -4,6 +4,7 @@ import { revalidatePath } from "next/cache";
|
|||||||
import { db } from "../db";
|
import { db } from "../db";
|
||||||
import { countries } from "../db/schema";
|
import { countries } from "../db/schema";
|
||||||
import { ZodError, z } from "zod";
|
import { ZodError, z } from "zod";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
name: z.string().min(1, { message: "Name is required" }),
|
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) {
|
export async function addCountry(prevstate: any, formData: FormData) {
|
||||||
const name = formData.get("name") as string;
|
const name = formData.get("name") as string;
|
||||||
|
const exists = await db
|
||||||
|
.select({ name: countries.name })
|
||||||
|
.from(countries)
|
||||||
|
.where(eq(countries.name, name));
|
||||||
try {
|
try {
|
||||||
schema.parse({
|
schema.parse({
|
||||||
name,
|
name,
|
||||||
});
|
});
|
||||||
|
if (!exists[0]) {
|
||||||
|
await db
|
||||||
|
.insert(countries)
|
||||||
|
.values({ name })
|
||||||
|
.returning({ name: countries.name });
|
||||||
|
revalidatePath("/");
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
message: "success",
|
message: "success",
|
||||||
errors: undefined,
|
errors: undefined,
|
||||||
|
Loading…
Reference in New Issue
Block a user