From 787c7cfaf30dcf981368e576f7a911bf1b12334b Mon Sep 17 00:00:00 2001
From: Serdar D <serdar-dorak@hotmail.de>
Date: Wed, 31 May 2023 12:50:27 +0200
Subject: [PATCH] Following model, following button und following user

---
 components/following-button.tsx | 43 +++++++++++++++++++++++++++++++++
 prisma/schema.prisma            | 10 ++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 components/following-button.tsx

diff --git a/components/following-button.tsx b/components/following-button.tsx
new file mode 100644
index 0000000..8c08020
--- /dev/null
+++ b/components/following-button.tsx
@@ -0,0 +1,43 @@
+"use client"
+
+import { Prisma } from "@prisma/client";
+import { useRouter } from "next/navigation";
+import { startTransition } from "react";
+import { Icons } from "./icons";
+import { Button } from "./ui/button";
+
+type likeType = Prisma.LikeUncheckedCreateInput
+
+export default function FollowButton(props: { data: likeType }) {
+  const router = useRouter();
+
+  async function postFollow(e: any) {
+    e.preventDefault()
+    const msgLikeData = props.data;
+    const likeData = {} as likeType
+    likeData.userId = msgLikeData.userId
+    likeData.postId = msgLikeData.postId
+
+    const response = await fetch('http://localhost:3000/api/follows', {
+      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.
+      router.refresh();
+    });
+    return await response.json()
+  }
+
+  return (
+    <div>
+      <form onSubmit={postFollow}>
+        <Button type="submit" variant="ghost" size="lg" className="float-right" >
+          <Icons.heart className="h-3 w-3" />
+        </Button>
+      </form>
+    </div>
+  )
+}
\ No newline at end of file
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index a6b261a..2e93abf 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -22,6 +22,8 @@ model User {
   Post    Post[]
   Comment Comment[]
   Like    Like[]
+  followers User[]   @relation("Followers", references: [id])
+  following User[]   @relation("Following", references: [id])
 }
 
 model Post {
@@ -57,3 +59,11 @@ model Comment {
   post      Post     @relation(fields: [postId], references: [id], onDelete: Cascade)
   user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
 }
+
+model Follower {
+  id        Int    @id @default(autoincrement())
+  follower  User   @relation("Followers", fields: [followerId], references: [id])
+  followerId Int
+  following User   @relation("Following", fields: [followingId], references: [id])
+  followingId Int
+}
\ No newline at end of file
-- 
GitLab