From 34b0d64555b66b8bb5158332c39aba3509205125 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Mon, 18 Aug 2025 14:40:41 +0200 Subject: [PATCH] parentheses stuff --- src/app/api/movies/route.ts | 38 ++++++++++++++++++++++++++++++++++++- src/app/suggest/page.tsx | 9 +++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/app/api/movies/route.ts b/src/app/api/movies/route.ts index 598a35c..c12e6a1 100644 --- a/src/app/api/movies/route.ts +++ b/src/app/api/movies/route.ts @@ -9,9 +9,45 @@ const prisma = new PrismaClient(); export async function POST(req: Request) { const { title, description, posterUrl, suggestedBy } = await req.json(); + // Check if there's already a pending request for this movie (not approved) + const existingPendingMovies = await prisma.movie.findMany({ + where: { + title: { + equals: title, + mode: "insensitive" + }, + approved: false + } + }); + + // If there's already a pending request, don't allow duplicate + if (existingPendingMovies.length > 0) { + return NextResponse.json( + { error: "This movie has already been suggested and is pending approval." }, + { status: 400 } + ); + } + + // Check if there are already approved movies with the same title + const existingApprovedMovies = await prisma.movie.findMany({ + where: { + title: { + equals: title, + mode: "insensitive" + }, + approved: true + } + }); + + // If there are existing approved movies, append a number to the title + let finalTitle = title; + if (existingApprovedMovies.length > 0) { + finalTitle = `${title} (${existingApprovedMovies.length + 1})`; + } + const movie = await prisma.movie.create({ data: { - title, + title: finalTitle, description, posterUrl, suggestedBy, diff --git a/src/app/suggest/page.tsx b/src/app/suggest/page.tsx index dd8b2a8..949802e 100644 --- a/src/app/suggest/page.tsx +++ b/src/app/suggest/page.tsx @@ -28,6 +28,7 @@ export default function SuggestPage() { const [query, setQuery] = useState(""); const [movies, setMovies] = useState([]); const [selectedMovie, setSelectedMovie] = useState(null); + const [error, setError] = useState(null); const handleSearch = async (searchQuery: string) => { setQuery(searchQuery); @@ -42,13 +43,17 @@ export default function SuggestPage() { const handleSelectMovie = (movie: Movie) => { setSelectedMovie(movie); + setError(null); }; + const handleSubmit = async () => { if (!session || !selectedMovie) { return; } + setError(null); + const res = await fetch("/api/movies", { method: "POST", headers: { @@ -64,6 +69,9 @@ export default function SuggestPage() { if (res.ok) { router.push("/"); + } else { + const data = await res.json(); + setError(data.error || "Failed to submit movie suggestion"); } }; @@ -97,6 +105,7 @@ export default function SuggestPage() { {selectedMovie && (

Selected: {selectedMovie.title}

+ {error &&

{error}

}