diff --git a/app/models/project.server.ts b/app/models/project.server.ts index 0eddf36..375e0e7 100644 --- a/app/models/project.server.ts +++ b/app/models/project.server.ts @@ -15,6 +15,17 @@ export function getProject({ }); } +export function getProjectByName({ + name, + userId +}: Pick & { + userId: User['id']; +}) { + return prisma.project.findFirst({ + where: { name, userId } + }); +} + export async function getProjects({ userId, page, diff --git a/app/routes/projects/new.tsx b/app/routes/projects/new.tsx index f4c0d40..5366480 100644 --- a/app/routes/projects/new.tsx +++ b/app/routes/projects/new.tsx @@ -20,7 +20,11 @@ import { } from '@remix-run/react'; import * as React from 'react'; import { AlertTriangle, RefreshCcw, Save } from 'react-feather'; -import { createProject, Project } from '~/models/project.server'; +import { + createProject, + getProjectByName, + Project +} from '~/models/project.server'; import { requireUserId } from '~/session.server'; export const meta: MetaFunction = () => { @@ -81,6 +85,20 @@ export async function action({ request }: ActionArgs) { ); } + const projectWithSameName = await getProjectByName({ name, userId }); + if (projectWithSameName) { + return json( + { + errors: { + name: 'A project with this name already exists', + description: null, + color: null + } + }, + { status: 409 } + ); + } + const project = await createProject({ name, description,