From ab5357c827e150fa0a8875654ab14bb152c76639 Mon Sep 17 00:00:00 2001
From: Izan Gil <66965250+SrIzan10@users.noreply.github.com>
Date: Sat, 11 Jan 2025 00:38:55 +0100
Subject: [PATCH] feat: comments
---
.../20250110222317_add_comments/migration.sql | 18 ++++++
.../migration.sql | 9 +++
prisma/schema.prisma | 15 +++++
src/app/(public)/post/[id]/page.tsx | 22 ++++---
src/components/app/Comments/Comments.tsx | 54 ++++++++++++++++
.../app/UniversalForm/UniversalForm.tsx | 15 ++++-
src/components/app/UniversalForm/types.ts | 2 +
.../app/UpvoteComment/UpvoteComment.tsx | 31 +++++++++
src/components/ui/button.tsx | 3 +-
src/instrumentation.ts | 1 +
src/lib/form/actions.ts | 64 ++++++++++++++++++-
src/lib/form/zod.ts | 5 ++
12 files changed, 225 insertions(+), 14 deletions(-)
create mode 100644 prisma/migrations/20250110222317_add_comments/migration.sql
create mode 100644 prisma/migrations/20250110231356_improve_comments/migration.sql
create mode 100644 src/components/app/Comments/Comments.tsx
create mode 100644 src/components/app/UpvoteComment/UpvoteComment.tsx
diff --git a/prisma/migrations/20250110222317_add_comments/migration.sql b/prisma/migrations/20250110222317_add_comments/migration.sql
new file mode 100644
index 0000000..73c1d24
--- /dev/null
+++ b/prisma/migrations/20250110222317_add_comments/migration.sql
@@ -0,0 +1,18 @@
+-- CreateTable
+CREATE TABLE "Comment" (
+ "id" TEXT NOT NULL,
+ "postId" TEXT NOT NULL,
+ "authorId" TEXT NOT NULL,
+ "content" TEXT NOT NULL,
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "upvotes" INTEGER NOT NULL,
+ "downvotes" INTEGER NOT NULL,
+
+ CONSTRAINT "Comment_pkey" PRIMARY KEY ("id")
+);
+
+-- AddForeignKey
+ALTER TABLE "Comment" ADD CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE "Comment" ADD CONSTRAINT "Comment_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/prisma/migrations/20250110231356_improve_comments/migration.sql b/prisma/migrations/20250110231356_improve_comments/migration.sql
new file mode 100644
index 0000000..95d5b7b
--- /dev/null
+++ b/prisma/migrations/20250110231356_improve_comments/migration.sql
@@ -0,0 +1,9 @@
+/*
+ Warnings:
+
+ - You are about to drop the column `downvotes` on the `Comment` table. All the data in the column will be lost.
+
+*/
+-- AlterTable
+ALTER TABLE "Comment" DROP COLUMN "downvotes",
+ADD COLUMN "votedBy" TEXT[];
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 4b71549..c1d773e 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -20,6 +20,7 @@ model User {
likedPosts String[]
sessions Session[]
posts Post[]
+ comments Comment[]
}
model Session {
@@ -38,4 +39,18 @@ model Post {
authorId String
author User @relation(references: [id], fields: [authorId], onDelete: Cascade)
previewHash String
+
+ comments Comment[]
}
+
+model Comment {
+ id String @id @default(cuid())
+ postId String
+ authorId String
+ content String
+ createdAt DateTime @default(now())
+ upvotes Int
+ votedBy String[]
+ post Post @relation(references: [id], fields: [postId], onDelete: Cascade)
+ author User @relation(references: [id], fields: [authorId], onDelete: Cascade)
+}
\ No newline at end of file
diff --git a/src/app/(public)/post/[id]/page.tsx b/src/app/(public)/post/[id]/page.tsx
index 4fd0cca..72586d0 100644
--- a/src/app/(public)/post/[id]/page.tsx
+++ b/src/app/(public)/post/[id]/page.tsx
@@ -1,3 +1,4 @@
+import Comments from '@/components/app/Comments/Comments';
import LikePost from '@/components/app/LikePost/LikePost';
import RelatedImages, { RelatedImagesSkeleton } from '@/components/app/RelatedImages/RelatedImages';
import { Badge } from '@/components/ui/badge';
@@ -8,6 +9,7 @@ import { validateRequest } from '@/lib/auth';
import prisma from '@/lib/db';
import { Download, Flag, MessageSquare, User, Calendar } from 'lucide-react';
import Image from 'next/image';
+import Link from 'next/link';
import { Suspense } from 'react';
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
@@ -46,14 +48,18 @@ export default async function Page({ params }: { params: Promise<{ id: string }>
comments here
+
{comment.author.username}
+{comment.content}
+{comment.upvotes}
+