diff --git a/app/(content)/(home)/home/page.tsx b/app/(content)/(home)/home/page.tsx index c0ce5746dfd6fab464c277d1ca8324b21a6ab98b..fd01a231716bdc6ed18985d3eea8fd3e48c9051a 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 7b5b87f1ec81cfe2ffdd14a5df29b51f4c8b9f0e..cadac2be6beeca112eacb847f5d04316d41f144a 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 15c6d33000e90db02e37fbc3deb48c296d3f9a99..cb35639e5da0638936e2e9832cf0af7db064a396 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 4f648aef330021faf89f9794b761229807cddef8..f9d283811254da527fb31025bb6e04a540922dcd 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 c99f7d3072a5f5fb310de42afee4b7015b74dec1..b0035aa6d0f90da8e5692c417626a8a7277a073a 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 e45e09bd0faf53cb23609e267b26ef1f748b90df..e17f95f97a84cb23285bfe4b47e26ec4a9fcee67 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 7be1448ba5d3e2658d593cd2f189b198bd8c536c..36c37ac6d577ccb1e6a8a682341b0808af6f0040 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()) +}