From 95b938d3fe7ab43a5648a1a5fe6d21d2c39c17d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Akg=C3=BCl?= <s86116@bht-berlin.de> Date: Sat, 8 Jul 2023 16:32:48 +0200 Subject: [PATCH] added response status codes and made small changes --- .../(community)/communities/page.tsx | 7 --- app/(content)/(search)/search/page.tsx | 19 +++++++ app/api/games/route.ts | 2 +- app/api/gweets/[id]/route.ts | 29 ++++------ app/api/gweets/likes/route.ts | 28 ++++------ app/api/gweets/regweets/route.ts | 14 +++-- app/api/gweets/route.ts | 53 ++++++++----------- app/api/hashtags/route.ts | 14 +---- app/api/route.ts | 2 +- app/api/signup/route.ts | 22 +++----- app/api/users/favgameslist/route.ts | 4 +- app/api/{ => users}/gamelists/route.ts | 17 +++--- app/api/users/route.ts | 1 - components/add-game-to-finished-list.tsx | 4 +- components/add-game-to-plan-list.tsx | 4 +- components/add-game-to-playing-list.tsx | 4 +- components/user-auth-form.tsx | 2 + 17 files changed, 97 insertions(+), 129 deletions(-) delete mode 100644 app/(content)/(community)/communities/page.tsx create mode 100644 app/(content)/(search)/search/page.tsx rename app/api/{ => users}/gamelists/route.ts (79%) diff --git a/app/(content)/(community)/communities/page.tsx b/app/(content)/(community)/communities/page.tsx deleted file mode 100644 index 7a1e2cf..0000000 --- a/app/(content)/(community)/communities/page.tsx +++ /dev/null @@ -1,7 +0,0 @@ -export default function CommunitiesPage() { - return ( - <div> - <h1>Community WIP</h1> - </div> - ) -} \ No newline at end of file diff --git a/app/(content)/(search)/search/page.tsx b/app/(content)/(search)/search/page.tsx new file mode 100644 index 0000000..cb8d68e --- /dev/null +++ b/app/(content)/(search)/search/page.tsx @@ -0,0 +1,19 @@ +import { GlobalLayout } from "@/components/global-layout" + +export default async function SearchPage() { + return ( + <GlobalLayout + mainContent={ + <> + Search Page + </> + } + + sideContent={ + <> + + </> + } + /> + ) +} \ No newline at end of file diff --git a/app/api/games/route.ts b/app/api/games/route.ts index 04d0ee9..8af8dfb 100644 --- a/app/api/games/route.ts +++ b/app/api/games/route.ts @@ -37,7 +37,7 @@ export async function GET(req: NextRequest) { sortby ? sortby : undefined, order ? order : undefined ) - return NextResponse.json(games) + return NextResponse.json(games, { status: 200 }) } catch (error) { return NextResponse.json(error, { status: 500 }) } diff --git a/app/api/gweets/[id]/route.ts b/app/api/gweets/[id]/route.ts index a9c7065..1340831 100644 --- a/app/api/gweets/[id]/route.ts +++ b/app/api/gweets/[id]/route.ts @@ -3,7 +3,6 @@ import { z } from "zod" import { db } from "@/lib/db" -// get a single gweet export async function GET(request: Request, { params }: { params: { id: string } }) { const { id } = params @@ -12,12 +11,10 @@ export async function GET(request: Request, { params }: { params: { id: string } const zod = gweetIdSchema.safeParse(id) if (!zod.success) { - return NextResponse.json( - { - message: "Invalid request body", - error: zod.error.formErrors, - }, { status: 400 }, - ) + return NextResponse.json({ + message: "Invalid request body", + error: zod.error.formErrors, + }, { status: 400 }) } try { @@ -96,20 +93,16 @@ export async function GET(request: Request, { params }: { params: { id: string } }) if (!gweet) { - return NextResponse.json( - { - message: "Gweet not found", - }, { status: 404 }, - ) + return NextResponse.json({ + message: "Gweet not found", + }, { status: 404 }) } return NextResponse.json(gweet, { status: 200 }) } catch (error) { - return NextResponse.json( - { - message: "Something went wrong", - error, - }, { status: 500 }, - ) + return NextResponse.json({ + message: "Something went wrong", + error, + }, { status: 500 }) } } \ No newline at end of file diff --git a/app/api/gweets/likes/route.ts b/app/api/gweets/likes/route.ts index 6c298fe..f560fe5 100644 --- a/app/api/gweets/likes/route.ts +++ b/app/api/gweets/likes/route.ts @@ -3,7 +3,6 @@ import { z } from "zod" import { db } from "@/lib/db" -// get likes from user export async function GET(request: Request) { const { searchParams } = new URL(request.url) const user_id = searchParams.get("user_id") || undefined @@ -41,16 +40,13 @@ export async function GET(request: Request) { return NextResponse.json(gweets, { status: 200 }) } catch (error: any) { - return NextResponse.json( - { - message: "Something went wrong", - error: error.message, - }, { status: error.errorCode || 500 }, - ) + return NextResponse.json({ + message: "Something went wrong", + error: error.message, + }, { status: error.errorCode || 500 }) } } -// like and dislike export async function POST(request: Request) { const { gweet_id, user_id } = await request.json() @@ -64,12 +60,10 @@ export async function POST(request: Request) { const zod = likeSchema.safeParse({ gweet_id, user_id }) if (!zod.success) { - return NextResponse.json( - { - message: "Invalid request body", - error: zod.error.formErrors, - }, { status: 400 }, - ) + return NextResponse.json({ + message: "Invalid request body", + error: zod.error.formErrors, + }, { status: 400 }) } try { @@ -87,7 +81,7 @@ export async function POST(request: Request) { }, }) - return NextResponse.json({ message: "Gweet unliked" }) + return NextResponse.json({ message: "Gweet unliked" }, { status: 200 }) } else { await db.like.create({ data: { @@ -96,12 +90,12 @@ export async function POST(request: Request) { }, }) - return NextResponse.json({ message: "Gweet liked" }) + return NextResponse.json({ message: "Gweet liked" }, { status: 201 }) } } catch (error: any) { return NextResponse.json({ message: "Something went wrong", error: error.message, - }) + }, { status: 500 }) } } \ No newline at end of file diff --git a/app/api/gweets/regweets/route.ts b/app/api/gweets/regweets/route.ts index 1cc8241..00a446e 100644 --- a/app/api/gweets/regweets/route.ts +++ b/app/api/gweets/regweets/route.ts @@ -16,12 +16,10 @@ export async function POST(request: Request) { const zod = regweetSchema.safeParse({ gweet_id, user_id }) if (!zod.success) { - return NextResponse.json( - { - message: "Invalid request body", - error: zod.error.formErrors, - }, { status: 400 }, - ) + return NextResponse.json({ + message: "Invalid request body", + error: zod.error.formErrors, + }, { status: 400 }) } try { @@ -39,7 +37,7 @@ export async function POST(request: Request) { }, }) - return NextResponse.json({ message: "Deleted gweet regweet" }) + return NextResponse.json({ message: "Deleted gweet regweet" }, { status: 200 }) } else { await db.regweet.create({ data: { @@ -48,7 +46,7 @@ export async function POST(request: Request) { }, }) - return NextResponse.json({ message: "Gweet regweeted" }) + return NextResponse.json({ message: "Gweet regweeted" }, { status: 201 }) } } catch (error: any) { return NextResponse.json({ error: error.message }, { status: 500 }) diff --git a/app/api/gweets/route.ts b/app/api/gweets/route.ts index 9cdd2ab..4f5903d 100644 --- a/app/api/gweets/route.ts +++ b/app/api/gweets/route.ts @@ -4,7 +4,6 @@ import { z } from "zod" import { db } from "@/lib/db" import { utapi } from "uploadthing/server" -// get gweets export async function GET(request: Request) { const { searchParams } = new URL(request.url) @@ -32,7 +31,7 @@ export async function GET(request: Request) { // // logic correct TODO get all gweets above comment // const prevId = thread.length < 4 ? undefined : thread[thread.length - 1].id - // return NextResponse.json({ gweets: thread, prevId }) + // return NextResponse.json({ gweets: thread, prevId }, { status: 200 }) // } const gweets = await db.gweet.findMany({ @@ -128,13 +127,12 @@ export async function GET(request: Request) { const nextId = gweets.length < take ? undefined : gweets[gweets.length - 1].id - return NextResponse.json({ gweets, nextId }) + return NextResponse.json({ gweets, nextId }, { status: 200 }) } catch (error) { - return NextResponse.error() + return NextResponse.json(error, { status: 500 }) } } -// create gweet export async function POST(request: Request) { const { gweet, fileprops } = await request.json() @@ -159,12 +157,10 @@ export async function POST(request: Request) { ) if (!zodGweet.success) { - return NextResponse.json( - { - message: "Invalid request body", - error: zodGweet.error.formErrors, - }, { status: 400 }, - ) + return NextResponse.json({ + message: "Invalid request body", + error: zodGweet.error.formErrors, + }, { status: 400 }) } try { @@ -189,12 +185,10 @@ export async function POST(request: Request) { const zodMedia = mediaSchema.safeParse(mediaArray) if (!zodMedia.success) { - return NextResponse.json( - { - message: "Invalid media body", - error: zodMedia.error.formErrors, - }, { status: 400 }, - ) + return NextResponse.json({ + message: "Invalid media body", + error: zodMedia.error.formErrors, + }, { status: 400 }) } await db.media.createMany({ @@ -202,18 +196,15 @@ export async function POST(request: Request) { }) } - return NextResponse.json(created_gweet, { status: 200 }) + return NextResponse.json(created_gweet, { status: 201 }) } catch (error: any) { - return NextResponse.json( - { - message: "Something went wrong", - error: error.message, - }, { status: error.errorCode || 500 }, - ) + return NextResponse.json({ + message: "Something went wrong", + error: error.message, + }, { status: error.errorCode || 500 }) } } -// delete gweet export async function DELETE(request: Request) { const { searchParams } = new URL(request.url) const id = searchParams.get("id") as string @@ -247,14 +238,12 @@ export async function DELETE(request: Request) { }, }) - return NextResponse.json({ message: "Gweet deleted successfully", }) + return NextResponse.json({ message: "Gweet deleted" }, { status: 200 }) } catch (error: any) { - return NextResponse.json( - { - message: "Something went wrong", - error: error.message, - }, { status: error.errorCode || 500 }, - ) + return NextResponse.json({ + message: "Something went wrong", + error: error.message, + }, { status: error.errorCode || 500 }) } } diff --git a/app/api/hashtags/route.ts b/app/api/hashtags/route.ts index b4ffb8e..58ee86e 100644 --- a/app/api/hashtags/route.ts +++ b/app/api/hashtags/route.ts @@ -57,18 +57,8 @@ export async function POST(request: Request) { } } - return NextResponse.json( - { - message: "Hashtag(s) created", - }, - { status: 200 }, - ) + return NextResponse.json({ message: "Hashtag(s) created", }, { status: 201 }) } catch (error: any) { - return NextResponse.json( - { - error: error.message, - }, - { status: 500 }, - ) + return NextResponse.json({ error: error.message, }, { status: 500 },) } } \ No newline at end of file diff --git a/app/api/route.ts b/app/api/route.ts index ae6ece8..8672d27 100644 --- a/app/api/route.ts +++ b/app/api/route.ts @@ -9,5 +9,5 @@ export async function GET() { return new NextResponse(JSON.stringify({ error: 'unauthorized' }), { status: 401 }) } - return NextResponse.json({ authenticated: !!session }) + return NextResponse.json({ authenticated: !!session }, { status: 200 }) } \ No newline at end of file diff --git a/app/api/signup/route.ts b/app/api/signup/route.ts index ffb9dba..d754d92 100644 --- a/app/api/signup/route.ts +++ b/app/api/signup/route.ts @@ -19,7 +19,7 @@ export async function POST(req: Request) { }) if (existingUser) { - throw new Error('email already exists') + return NextResponse.json({ error: 'Email already exists' }, { status: 422 }) } let isUnique = false @@ -46,21 +46,11 @@ export async function POST(req: Request) { } }) + return NextResponse.json({ usernameOrEmail: user.email }) + } catch (error) { return NextResponse.json({ - usernameOrEmail: user.email - }) - } catch (err: any) { - if (err.message === 'email already exists') { - return new NextResponse(JSON.stringify({ - error: err.message - }), { status: 422 } - ) - } - - return new NextResponse( - JSON.stringify({ - error: err.message - }), { status: 500 } - ) + message: "Something went wrong", + error, + }, { status: 500 }) } } \ No newline at end of file diff --git a/app/api/users/favgameslist/route.ts b/app/api/users/favgameslist/route.ts index 967f901..67bd79f 100644 --- a/app/api/users/favgameslist/route.ts +++ b/app/api/users/favgameslist/route.ts @@ -51,9 +51,9 @@ export async function PUT(req: NextRequest) { const path = req.nextUrl.searchParams.get('path') || '/' revalidatePath(path) - return NextResponse.json({ status: 201, message: 'Game Hinzugefügt' }) + return NextResponse.json({ message: 'Game added' }, { status: 201 }) } catch (error: any) { - return NextResponse.json({ status: 500, message: error.message }) + return NextResponse.json({ message: error.message }, { status: 500 }) } } \ No newline at end of file diff --git a/app/api/gamelists/route.ts b/app/api/users/gamelists/route.ts similarity index 79% rename from app/api/gamelists/route.ts rename to app/api/users/gamelists/route.ts index 7c578b3..629f995 100644 --- a/app/api/gamelists/route.ts +++ b/app/api/users/gamelists/route.ts @@ -8,11 +8,11 @@ export async function PUT(req: NextRequest) { const sessionUser = await getCurrentUser() const data: User = await req.json() - console.log("userid", sessionUser!.id, "formdataid", data.id) + if (!sessionUser || sessionUser.id != data.id) { - return NextResponse.json({ status: 401, message: 'Unauthorized' }) + return NextResponse.json({ message: 'Unauthorized' }, { status: 401 }) } - console.log("put list") + try { const dbUser = await db.user.findFirst({ where: { @@ -41,11 +41,12 @@ export async function PUT(req: NextRequest) { } }) } - } catch (error) { - } - const path = req.nextUrl.searchParams.get('path') || '/' - revalidatePath(path) + const path = req.nextUrl.searchParams.get('path') || '/' + revalidatePath(path) - return NextResponse.json({ status: 201, message: 'Game Hinzugefügt' }) + return NextResponse.json({ message: 'Game added' }, { status: 201 }) + } catch (error: any) { + return NextResponse.json({ message: error.message }, { status: 500 }) + } } \ No newline at end of file diff --git a/app/api/users/route.ts b/app/api/users/route.ts index e0611fd..7f3814a 100644 --- a/app/api/users/route.ts +++ b/app/api/users/route.ts @@ -2,7 +2,6 @@ import { db } from "@/lib/db" import { NextResponse } from "next/server" import { z } from "zod" -// get all other users export async function GET(request: Request) { const { searchParams } = new URL(request.url) const id = searchParams.get("id") || undefined diff --git a/components/add-game-to-finished-list.tsx b/components/add-game-to-finished-list.tsx index bc37572..6f8ec4f 100644 --- a/components/add-game-to-finished-list.tsx +++ b/components/add-game-to-finished-list.tsx @@ -33,7 +33,7 @@ export default function AddGameToFinishedList(props: { gameId: string, user: Use formData.id = user.id formData.finishedGameList = props.user.finishedGameList.filter((id) => id !== gameId) console.log(formData.finishedGameList) - const response = await fetch('/api/gamelists', { + const response = await fetch('/api/users/gamelists', { method: 'PUT', body: JSON.stringify(formData) }) @@ -52,7 +52,7 @@ export default function AddGameToFinishedList(props: { gameId: string, user: Use formData.id = user.id props.user.finishedGameList.push(gameId) formData.finishedGameList = props.user.finishedGameList - const response = await fetch('/api/gamelists', { + const response = await fetch('/api/users/gamelists', { method: 'PUT', body: JSON.stringify(formData) }) diff --git a/components/add-game-to-plan-list.tsx b/components/add-game-to-plan-list.tsx index 61f62e8..9de2099 100644 --- a/components/add-game-to-plan-list.tsx +++ b/components/add-game-to-plan-list.tsx @@ -33,7 +33,7 @@ export default function AddGameToPlanList(props: { gameId: string, user: User }) formData.id = user.id formData.planningGameList = props.user.planningGameList.filter((id) => id !== gameId) console.log(formData.planningGameList) - const response = await fetch('/api/gamelists', { + const response = await fetch('/api/users/gamelists', { method: 'PUT', body: JSON.stringify(formData) }) @@ -52,7 +52,7 @@ export default function AddGameToPlanList(props: { gameId: string, user: User }) formData.id = user.id props.user.planningGameList.push(gameId) formData.planningGameList = props.user.planningGameList - const response = await fetch('/api/gamelists', { + const response = await fetch('/api/users/gamelists', { method: 'PUT', body: JSON.stringify(formData) }) diff --git a/components/add-game-to-playing-list.tsx b/components/add-game-to-playing-list.tsx index 73e8067..81232c6 100644 --- a/components/add-game-to-playing-list.tsx +++ b/components/add-game-to-playing-list.tsx @@ -33,7 +33,7 @@ export default function AddGameToPlayingList(props: { gameId: string, user: User formData.id = user.id formData.playingGameList = props.user.playingGameList.filter((id) => id !== gameId) console.log(formData.playingGameList) - const response = await fetch('/api/gamelists', { + const response = await fetch('/api/users/gamelists', { method: 'PUT', body: JSON.stringify(formData) }) @@ -52,7 +52,7 @@ export default function AddGameToPlayingList(props: { gameId: string, user: User formData.id = user.id props.user.playingGameList.push(gameId) formData.playingGameList = props.user.playingGameList - const response = await fetch('/api/gamelists', { + const response = await fetch('/api/users/gamelists', { method: 'PUT', body: JSON.stringify(formData) }) diff --git a/components/user-auth-form.tsx b/components/user-auth-form.tsx index 21a0d03..9ce0046 100644 --- a/components/user-auth-form.tsx +++ b/components/user-auth-form.tsx @@ -55,6 +55,8 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) { if (!res.ok) { if (res.status === 422) { setError('email', { type: 'manual', message: 'This email is already in use. Please choose another one.' }) + setIsLoading(false) + return } setIsLoading(false) -- GitLab