From 770970ec14f86951ebd150f5fb4ee3bfa284f9ad Mon Sep 17 00:00:00 2001 From: Caner <s86215@bht-berlin.de> Date: Thu, 8 Jun 2023 02:28:12 +0200 Subject: [PATCH] gamelist --- app/api/gameList/route.ts | 37 +++++++++++++ components/addGameToList.tsx | 36 +++++++++++++ prisma/schema.prisma | 100 ++++++++++++++++------------------- 3 files changed, 118 insertions(+), 55 deletions(-) create mode 100644 app/api/gameList/route.ts create mode 100644 components/addGameToList.tsx diff --git a/app/api/gameList/route.ts b/app/api/gameList/route.ts new file mode 100644 index 0000000..8c14bc2 --- /dev/null +++ b/app/api/gameList/route.ts @@ -0,0 +1,37 @@ +import { db } from "@/lib/db"; +import { getCurrentUser } from "@/lib/session"; +import { revalidatePath } from "next/cache"; +import { NextRequest, NextResponse } from "next/server"; + +export async function PUT(req: NextRequest) { + const user = await getCurrentUser(); + + if (!user) { + return NextResponse.json({ status: 401, message: 'Unauthorized' }); + } + + const userId = user.id; + const content = await req.json() + + console.log(content); + console.log(userId); + try { + await db.user.update({ + where:{ + id: userId + }, + data: { + favGameList:{ + push: content.gameId + } + } + }) + const path = req.nextUrl.searchParams.get('path') || '/'; + revalidatePath(path); + + return NextResponse.json({ status: 201, message: 'Game Hinzugefügt' }) + + } catch (error: any) { + return NextResponse.json({ status: 500, message: error.message }) + } +} \ No newline at end of file diff --git a/components/addGameToList.tsx b/components/addGameToList.tsx new file mode 100644 index 0000000..74ed545 --- /dev/null +++ b/components/addGameToList.tsx @@ -0,0 +1,36 @@ +"use client" + +import { Post, Prisma } from "@prisma/client"; +import { useRouter } from "next/navigation"; +import { startTransition, useEffect, useState } from "react"; + +export default function AddGameToList(props: { userid: string, gameId: string }) { + + const router = useRouter(); + const gameId = props.gameId + let formData = {gameId} + + async function addGame(e: any) { + e.preventDefault() + + formData.gameId = gameId; + + const response = await fetch('http://localhost:3000/api/gameList', { + method: 'PUT', + body: JSON.stringify(formData) + }) + + startTransition(() => { + // Refresh the current route and fetch new data from the server without + // losing client-side browser or React state. + router.refresh(); + }); + return await response.json() + } + + return ( + <button type="submit" className="mt-2 bg-gray-300 text-gray-800 px-4 py-2 rounded float-right"> + Post + </button> + ) +} \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d48c1ca..acecf74 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,6 +1,3 @@ -// This is your Prisma schema file, -// learn more about it in the docs: https://pris.ly/d/prisma-schema - generator client { provider = "prisma-client-js" } @@ -16,21 +13,20 @@ model Account { type String provider String providerAccountId String - refresh_token String? @db.Text - access_token String? @db.Text + refresh_token String? + access_token String? expires_at Int? token_type String? scope String? - id_token String? @db.Text + id_token String? session_state String? - createdAt DateTime @default(now()) @map(name: "created_at") - updatedAt DateTime @default(now()) @map(name: "updated_at") + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") refresh_token_expires_in Int? - - user User @relation(fields: [userId], references: [id], onDelete: Cascade) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@unique([provider, providerAccountId]) - @@map(name: "accounts") + @@map("accounts") } model Session { @@ -38,10 +34,9 @@ model Session { sessionToken String @unique userId String @unique expires DateTime + user User @relation(fields: [userId], references: [id], onDelete: Cascade) - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - - @@map(name: "sessions") + @@map("sessions") } model User { @@ -52,20 +47,18 @@ model User { emailVerified DateTime? password String? image String? - createdAt DateTime @default(now()) @map(name: "created_at") - updatedAt DateTime @default(now()) @map(name: "updated_at") - - accounts Account[] - sessions Session[] - - Post Post[] - Comment Comment[] - Like Like[] - - followers Follows[] @relation("follower") - following Follows[] @relation("following") - - @@map(name: "users") + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") + favGameList Int[] + accounts Account? + Comment Comment[] + following Follows[] @relation("following") + followers Follows[] @relation("follower") + Like Like[] + Post Post[] + sessions Session? + + @@map("users") } model VerificationToken { @@ -74,61 +67,58 @@ model VerificationToken { expires DateTime @@unique([identifier, token]) - @@map(name: "verification_tokens") + @@map("verification_tokens") } model Follows { - follower User @relation("following", fields: [followerId], references: [id]) followerId String - following User @relation("follower", fields: [followingId], references: [id]) followingId String createdAt DateTime @default(now()) + follower User @relation("following", fields: [followerId], references: [id]) + following User @relation("follower", fields: [followingId], references: [id]) @@id([followerId, followingId]) - @@map(name: "follows") + @@map("follows") } model Post { - id String @id @default(cuid()) - createdAt DateTime @default(now()) @map(name: "created_at") - updatedAt DateTime @default(now()) @map(name: "updated_at") + id String @id @default(cuid()) + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") userId String content String - likeCount Int? @default(0) - published Boolean @default(false) + likeCount Int? @default(0) + published Boolean @default(false) + Comment Comment[] + Like Like[] + user User @relation(fields: [userId], references: [id], onDelete: Cascade) - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - Comment Comment[] - Like Like[] - - @@map(name: "posts") + @@map("posts") } model Like { - id String @id @default(cuid()) + id String @id @default(cuid()) postId String commentId String? userId String + comment Comment? @relation(fields: [commentId], references: [id], onDelete: Cascade) + post Post @relation(fields: [postId], references: [id], onDelete: Cascade) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) - post Post @relation(fields: [postId], references: [id], onDelete: Cascade) - comment Comment? @relation(fields: [commentId], references: [id], onDelete: Cascade) - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - - @@map(name: "likes") + @@map("likes") } model Comment { id String @id @default(cuid()) - createdAt DateTime @default(now()) @map(name: "created_at") - updatedAt DateTime @default(now()) @map(name: "updated_at") + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @map("updated_at") message String likeCount Int? @default(0) postId String userId String + post Post @relation(fields: [postId], references: [id], onDelete: Cascade) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + Like Like[] - post Post @relation(fields: [postId], references: [id], onDelete: Cascade) - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - Like Like[] - - @@map(name: "comments") + @@map("comments") } -- GitLab