From a3f893e5f2860822a363ebb39ebd57b18c7bf3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Akg=C3=BCl?= <s86116@bht-berlin.de> Date: Mon, 29 May 2023 00:35:55 +0200 Subject: [PATCH] cleanup merges --- app/(content)/(home)/home/page.tsx | 147 ++++++++---------- app/api/likes/likeService.ts | 65 ++++---- app/api/likes/route.ts | 8 +- app/api/messages/route.ts | 4 +- .../{LikeButton.tsx => like-button.tsx} | 10 +- components/post-messages.tsx | 12 +- prisma/schema.prisma | 14 +- 7 files changed, 120 insertions(+), 140 deletions(-) rename components/{LikeButton.tsx => like-button.tsx} (85%) diff --git a/app/(content)/(home)/home/page.tsx b/app/(content)/(home)/home/page.tsx index c0ce574..fd01a23 100644 --- a/app/(content)/(home)/home/page.tsx +++ b/app/(content)/(home)/home/page.tsx @@ -1,94 +1,85 @@ -import LikeButton from "@/components/LikeButton"; +import LikeButton from "@/components/like-button"; import PostMessageForm from "@/components/post-messages"; import { prisma } from "@/prisma/db"; -import { Prisma} from "@prisma/client" -type likeType = Prisma.LikeUncheckedCreateInput +import { Prisma } from "@prisma/client"; + type messageType = Prisma.MessageUncheckedCreateInput type messageItemProps = { - msg: messageType; - }; + msg: messageType; +}; export default async function HomePage() { - let messages = null - try { - messages = await prisma.message.findMany({ - orderBy:{ - sentAt: "desc" - } - }) - - } catch (error) { - console.log("the database is not running, try: 'npx prisma migrate dev --name init' if you want to use the database") - } + let messages = null + try { + messages = await prisma.message.findMany({ + orderBy: { + sentAt: "desc" + } + }) - return ( - <div> - <h1>Home WIP</h1> - <p>This will be where all messages show up.</p> - <p>Needs a reload after posting!!</p> - {/* <PostMessageForm data={messages}></PostMessageForm> */} - <PostMessageForm data={messages}/> - {messages ? - <> - {messages.map((msg) => ( - <MessageItem msg={msg} key={msg.id} /> - ))} - - </> - : - <p>no messages / no database</p>} - </div> - ) + } catch (error) { + console.log("the database is not running, try: 'npx prisma migrate dev --name init' if you want to use the database") + } + + return ( + <div> + <h1>Home WIP</h1> + <p>This will be where all messages show up.</p> + <p>Needs a reload after posting!!</p> + <PostMessageForm data={messages} /> + {messages ? + <> + {messages.map((msg) => ( + <MessageItem msg={msg} key={msg.id} /> + ))} + + </> + : + <p>no messages / no database</p>} + </div> + ) } - const MessageItem = ({ msg }: messageItemProps) => { - if(!msg.id){ - return <div></div> - } - return ( - <div className="flex border-b border-gray-200 py-4"> - <div className="flex-shrink-0"> - <div className="h-10 w-10 rounded-full bg-gray-300"></div> {/* Profile picture */} - </div> - <div className="ml-4 flex flex-col"> - <div> - <div className="flex items-center"> - <span className="font-bold mr-2">{msg.author}</span> - <span className="text-gray-500 text-sm"> - {formatDate(new Date(msg.sentAt!))} - </span> - </div> - <div className="text-gray-800">{msg.content}</div> +const MessageItem = ({ msg }: messageItemProps) => { + if (!msg.id) { + return <div></div> + } + return ( + <div className="flex border-b border-gray-200 py-4"> + <div className="flex-shrink-0"> + <div className="h-10 w-10 rounded-full bg-gray-300"></div> {/* Profile picture */} + </div> + <div className="ml-4 flex flex-col"> + <div> + <div className="flex items-center"> + <span className="font-bold mr-2">{msg.author}</span> + <span className="text-gray-500 text-sm"> + {formatDate(new Date(msg.sentAt!))} + </span> </div> - <div className="mt-4"> - <div className="flex items-center"> - <div className="bg-gray-200 rounded-lg py-10 px-20 mr-2"> - {/* potential Image */} - </div> + <div className="text-gray-800">{msg.content}</div> + </div> + <div className="mt-4"> + <div className="flex items-center"> + <div className="bg-gray-200 rounded-lg py-10 px-20 mr-2"> + {/* potential Image */} </div> </div> - <LikeButton data={{ - postId: msg.id, - author: msg.author - }}/> - <span className="text-gray-600">Like Count: {msg.likeCount} | <span className="text-gray-600">ReplyButton (Number of Replies)</span></span> </div> + <LikeButton data={{ + postId: msg.id, + author: msg.author + }} /> + <span className="text-gray-600">Like Count: {msg.likeCount} | <span className="text-gray-600">ReplyButton (Number of Replies)</span></span> </div> - ); - }; - - - - - - - + </div> + ); +}; -function formatDate(date: Date){ - - return date.toLocaleDateString("en-US", { - day: "numeric", - month: "short", - year: "numeric" - }); +function formatDate(date: Date) { + return date.toLocaleDateString("en-US", { + day: "numeric", + month: "short", + year: "numeric" + }); } \ No newline at end of file diff --git a/app/api/likes/likeService.ts b/app/api/likes/likeService.ts index 7b5b87f..cadac2b 100644 --- a/app/api/likes/likeService.ts +++ b/app/api/likes/likeService.ts @@ -1,55 +1,52 @@ import { prisma } from "@/prisma/db" -import { Prisma} from "@prisma/client" +import { Prisma } from "@prisma/client" type likeType = Prisma.LikeUncheckedCreateInput /** * Creates like if user has not liked this post. * Deletes like if user has liked post already. - * */ - export async function putLike(like: likeType): Promise<likeType | undefined> { -//check if like exists by this user and for this post -// if exists delete -//if not create - try{ + // check if like exists by this user and for this post + // if exists delete + // if not create + try { const actualLike = await prisma.like.findFirst({ where: { id: like.id, postId: like.postId, author: like.author } - }) + }) - if(actualLike == null){ - console.log("like is null") - throw Error("Message was not liked by this user") - } - - await prisma.like.delete({ - where: { - id: actualLike.id - } - }) + if (actualLike == null) { + console.log("like is null") + throw Error("Message was not liked by this user") + } - const msg = await prisma.message.update({ - where: { - id: like.postId - }, - data:{ - likeCount: {increment: -1} - } - }) + await prisma.like.delete({ + where: { + id: actualLike.id + } + }) - return undefined; + const msg = await prisma.message.update({ + where: { + id: like.postId + }, + data: { + likeCount: { increment: -1 } + } + }) - } catch{ + return undefined; + } catch { const createdLike = await prisma.like.create({ - data:{ + data: { postId: like.postId, - author: like.author + author: like.author } }) @@ -57,13 +54,11 @@ export async function putLike(like: likeType): Promise<likeType | undefined> { where: { id: like.postId }, - data:{ - likeCount: {increment: 1} + data: { + likeCount: { increment: 1 } } }) return createdLike } - - -} +} \ No newline at end of file diff --git a/app/api/likes/route.ts b/app/api/likes/route.ts index 15c6d33..cb35639 100644 --- a/app/api/likes/route.ts +++ b/app/api/likes/route.ts @@ -1,16 +1,14 @@ +import { Prisma } from "@prisma/client"; import { NextRequest, NextResponse } from "next/server"; -import { prisma } from "@/prisma/db" -import { Prisma} from "@prisma/client" import { putLike } from "./likeService"; type like = Prisma.LikeUncheckedCreateInput export async function PUT(req: NextRequest) { - const data:like = await req.json() - + const data: like = await req.json() + console.log("router data: " + data, "status:") try { - const msg = await putLike(data) return NextResponse.json({ status: 200, message: 'Like handled' }) diff --git a/app/api/messages/route.ts b/app/api/messages/route.ts index 4f648ae..f9d2838 100644 --- a/app/api/messages/route.ts +++ b/app/api/messages/route.ts @@ -21,14 +21,14 @@ export async function POST(req: NextRequest) { console.log("post") } -export async function GET(req: NextRequest, res:NextResponse) { +export async function GET(req: NextRequest, res: NextResponse) { const data = await req.json() console.log("router data: " + data, "status:") console.log(data) try { const messages = await prisma.message.findMany({ - orderBy:{ + orderBy: { sentAt: "desc" } }) diff --git a/components/LikeButton.tsx b/components/like-button.tsx similarity index 85% rename from components/LikeButton.tsx rename to components/like-button.tsx index c99f7d3..b0035aa 100644 --- a/components/LikeButton.tsx +++ b/components/like-button.tsx @@ -1,12 +1,10 @@ "use client" -import { Message } from "@prisma/client" +import { Prisma } from "@prisma/client"; import { useRouter } from "next/navigation"; -import { prisma } from "@/prisma/db"; -import { Prisma} from "@prisma/client" -type likeType = Prisma.LikeUncheckedCreateInput +import { startTransition } from "react"; -import { startTransition, useState } from "react" +type likeType = Prisma.LikeUncheckedCreateInput export default function LikeButton(props: { data: likeType }) { const router = useRouter(); @@ -22,7 +20,7 @@ export default function LikeButton(props: { data: likeType }) { method: 'PUT', body: JSON.stringify(likeData) }) - + startTransition(() => { // Refresh the current route and fetch new data from the server without // losing client-side browser or React state. diff --git a/components/post-messages.tsx b/components/post-messages.tsx index e45e09b..e17f95f 100644 --- a/components/post-messages.tsx +++ b/components/post-messages.tsx @@ -1,16 +1,14 @@ "use client" -import { Message } from "@prisma/client" +import { Message, Prisma } from "@prisma/client"; import { useRouter } from "next/navigation"; -import { prisma } from "@/prisma/db"; -import { Prisma} from "@prisma/client" -type messageType = Prisma.MessageUncheckedCreateInput +import { startTransition, useState } from "react"; -import { startTransition, useState } from "react" +type messageType = Prisma.MessageUncheckedCreateInput export default function PostMessageForm(props: { data: Message[] | null }) { - - const [formData, setFormData] = useState<messageType>({content:""} as messageType); + + const [formData, setFormData] = useState<messageType>({ content: "" } as messageType); // const [messagesState, setMessages] = useState(props.data) const router = useRouter(); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7be1448..36c37ac 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,15 +16,15 @@ model Message { gameId String? title String? content String - likeCount Int? @default (0) + likeCount Int? @default(0) sentAt DateTime? @default(now()) updatedAt DateTime? @updatedAt } model Like { - id Int @id @default(autoincrement()) - postId Int - author String? - gameId String? - likedAt DateTime? @default(now()) -} \ No newline at end of file + id Int @id @default(autoincrement()) + postId Int + author String? + gameId String? + likedAt DateTime? @default(now()) +} -- GitLab