Skip to content
Snippets Groups Projects
Commit 68383b1b authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

Merge branch 'tweeting' into 'main'

cleanup merges

See merge request !11
parents b924b942 a3f893e5
No related branches found
No related tags found
1 merge request!11cleanup merges
Pipeline #35287 passed
import LikeButton from "@/components/LikeButton"; import LikeButton from "@/components/like-button";
import PostMessageForm from "@/components/post-messages"; import PostMessageForm from "@/components/post-messages";
import { prisma } from "@/prisma/db"; import { prisma } from "@/prisma/db";
import { Prisma} from "@prisma/client" import { Prisma } from "@prisma/client";
type likeType = Prisma.LikeUncheckedCreateInput
type messageType = Prisma.MessageUncheckedCreateInput type messageType = Prisma.MessageUncheckedCreateInput
type messageItemProps = { type messageItemProps = {
msg: messageType; msg: messageType;
}; };
export default async function HomePage() { export default async function HomePage() {
let messages = null let messages = null
try { try {
messages = await prisma.message.findMany({ messages = await prisma.message.findMany({
orderBy:{ orderBy: {
sentAt: "desc" 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")
}
return ( } catch (error) {
<div> console.log("the database is not running, try: 'npx prisma migrate dev --name init' if you want to use the database")
<h1>Home WIP</h1> }
<p>This will be where all messages show up.</p>
<p>Needs a reload after posting!!</p> return (
{/* <PostMessageForm data={messages}></PostMessageForm> */} <div>
<PostMessageForm data={messages}/> <h1>Home WIP</h1>
{messages ? <p>This will be where all messages show up.</p>
<> <p>Needs a reload after posting!!</p>
{messages.map((msg) => ( <PostMessageForm data={messages} />
<MessageItem msg={msg} key={msg.id} /> {messages ?
))} <>
{messages.map((msg) => (
</> <MessageItem msg={msg} key={msg.id} />
: ))}
<p>no messages / no database</p>}
</div> </>
) :
<p>no messages / no database</p>}
</div>
)
} }
const MessageItem = ({ msg }: messageItemProps) => { const MessageItem = ({ msg }: messageItemProps) => {
if(!msg.id){ if (!msg.id) {
return <div></div> return <div></div>
} }
return ( return (
<div className="flex border-b border-gray-200 py-4"> <div className="flex border-b border-gray-200 py-4">
<div className="flex-shrink-0"> <div className="flex-shrink-0">
<div className="h-10 w-10 rounded-full bg-gray-300"></div> {/* Profile picture */} <div className="h-10 w-10 rounded-full bg-gray-300"></div> {/* Profile picture */}
</div> </div>
<div className="ml-4 flex flex-col"> <div className="ml-4 flex flex-col">
<div> <div>
<div className="flex items-center"> <div className="flex items-center">
<span className="font-bold mr-2">{msg.author}</span> <span className="font-bold mr-2">{msg.author}</span>
<span className="text-gray-500 text-sm"> <span className="text-gray-500 text-sm">
{formatDate(new Date(msg.sentAt!))} {formatDate(new Date(msg.sentAt!))}
</span> </span>
</div>
<div className="text-gray-800">{msg.content}</div>
</div> </div>
<div className="mt-4"> <div className="text-gray-800">{msg.content}</div>
<div className="flex items-center"> </div>
<div className="bg-gray-200 rounded-lg py-10 px-20 mr-2"> <div className="mt-4">
{/* potential Image */} <div className="flex items-center">
</div> <div className="bg-gray-200 rounded-lg py-10 px-20 mr-2">
{/* potential Image */}
</div> </div>
</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> </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>
); </div>
}; );
};
function formatDate(date: Date){ function formatDate(date: Date) {
return date.toLocaleDateString("en-US", {
return date.toLocaleDateString("en-US", { day: "numeric",
day: "numeric", month: "short",
month: "short", year: "numeric"
year: "numeric" });
});
} }
\ No newline at end of file
import { prisma } from "@/prisma/db" import { prisma } from "@/prisma/db"
import { Prisma} from "@prisma/client" import { Prisma } from "@prisma/client"
type likeType = Prisma.LikeUncheckedCreateInput type likeType = Prisma.LikeUncheckedCreateInput
/** /**
* Creates like if user has not liked this post. * Creates like if user has not liked this post.
* Deletes like if user has liked post already. * Deletes like if user has liked post already.
*
*/ */
export async function putLike(like: likeType): Promise<likeType | undefined> { export async function putLike(like: likeType): Promise<likeType | undefined> {
//check if like exists by this user and for this post // check if like exists by this user and for this post
// if exists delete // if exists delete
//if not create // if not create
try{ try {
const actualLike = await prisma.like.findFirst({ const actualLike = await prisma.like.findFirst({
where: { where: {
id: like.id, id: like.id,
postId: like.postId, postId: like.postId,
author: like.author author: like.author
} }
}) })
if(actualLike == null){ if (actualLike == null) {
console.log("like is null") console.log("like is null")
throw Error("Message was not liked by this user") throw Error("Message was not liked by this user")
} }
await prisma.like.delete({
where: {
id: actualLike.id
}
})
const msg = await prisma.message.update({ await prisma.like.delete({
where: { where: {
id: like.postId id: actualLike.id
}, }
data:{ })
likeCount: {increment: -1}
}
})
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({ const createdLike = await prisma.like.create({
data:{ data: {
postId: like.postId, postId: like.postId,
author: like.author author: like.author
} }
}) })
...@@ -57,13 +54,11 @@ export async function putLike(like: likeType): Promise<likeType | undefined> { ...@@ -57,13 +54,11 @@ export async function putLike(like: likeType): Promise<likeType | undefined> {
where: { where: {
id: like.postId id: like.postId
}, },
data:{ data: {
likeCount: {increment: 1} likeCount: { increment: 1 }
} }
}) })
return createdLike return createdLike
} }
}
\ No newline at end of file
}
import { Prisma } from "@prisma/client";
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/prisma/db"
import { Prisma} from "@prisma/client"
import { putLike } from "./likeService"; import { putLike } from "./likeService";
type like = Prisma.LikeUncheckedCreateInput type like = Prisma.LikeUncheckedCreateInput
export async function PUT(req: NextRequest) { export async function PUT(req: NextRequest) {
const data:like = await req.json() const data: like = await req.json()
console.log("router data: " + data, "status:") console.log("router data: " + data, "status:")
try { try {
const msg = await putLike(data) const msg = await putLike(data)
return NextResponse.json({ status: 200, message: 'Like handled' }) return NextResponse.json({ status: 200, message: 'Like handled' })
......
...@@ -21,14 +21,14 @@ export async function POST(req: NextRequest) { ...@@ -21,14 +21,14 @@ export async function POST(req: NextRequest) {
console.log("post") console.log("post")
} }
export async function GET(req: NextRequest, res:NextResponse) { export async function GET(req: NextRequest, res: NextResponse) {
const data = await req.json() const data = await req.json()
console.log("router data: " + data, "status:") console.log("router data: " + data, "status:")
console.log(data) console.log(data)
try { try {
const messages = await prisma.message.findMany({ const messages = await prisma.message.findMany({
orderBy:{ orderBy: {
sentAt: "desc" sentAt: "desc"
} }
}) })
......
"use client" "use client"
import { Message } from "@prisma/client" import { Prisma } from "@prisma/client";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { prisma } from "@/prisma/db"; import { startTransition } from "react";
import { Prisma} from "@prisma/client"
type likeType = Prisma.LikeUncheckedCreateInput
import { startTransition, useState } from "react" type likeType = Prisma.LikeUncheckedCreateInput
export default function LikeButton(props: { data: likeType }) { export default function LikeButton(props: { data: likeType }) {
const router = useRouter(); const router = useRouter();
...@@ -22,7 +20,7 @@ export default function LikeButton(props: { data: likeType }) { ...@@ -22,7 +20,7 @@ export default function LikeButton(props: { data: likeType }) {
method: 'PUT', method: 'PUT',
body: JSON.stringify(likeData) body: JSON.stringify(likeData)
}) })
startTransition(() => { startTransition(() => {
// Refresh the current route and fetch new data from the server without // Refresh the current route and fetch new data from the server without
// losing client-side browser or React state. // losing client-side browser or React state.
......
"use client" "use client"
import { Message } from "@prisma/client" import { Message, Prisma } from "@prisma/client";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { prisma } from "@/prisma/db"; import { startTransition, useState } from "react";
import { Prisma} from "@prisma/client"
type messageType = Prisma.MessageUncheckedCreateInput
import { startTransition, useState } from "react" type messageType = Prisma.MessageUncheckedCreateInput
export default function PostMessageForm(props: { data: Message[] | null }) { 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 [messagesState, setMessages] = useState(props.data)
const router = useRouter(); const router = useRouter();
......
...@@ -16,15 +16,15 @@ model Message { ...@@ -16,15 +16,15 @@ model Message {
gameId String? gameId String?
title String? title String?
content String content String
likeCount Int? @default (0) likeCount Int? @default(0)
sentAt DateTime? @default(now()) sentAt DateTime? @default(now())
updatedAt DateTime? @updatedAt updatedAt DateTime? @updatedAt
} }
model Like { model Like {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
postId Int postId Int
author String? author String?
gameId String? gameId String?
likedAt DateTime? @default(now()) likedAt DateTime? @default(now())
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment