-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor post and comment handling to use Prisma for data manag…
…ement
- Loading branch information
Showing
6 changed files
with
71 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- CreateTable | ||
CREATE TABLE "Post" ( | ||
"id" TEXT NOT NULL, | ||
"from" TEXT NOT NULL DEFAULT 'Anonim', | ||
"to" TEXT NOT NULL, | ||
"message" TEXT NOT NULL, | ||
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"loveCount" INTEGER NOT NULL DEFAULT 0, | ||
|
||
CONSTRAINT "Post_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Comment" ( | ||
"id" TEXT NOT NULL, | ||
"from" TEXT NOT NULL DEFAULT 'Anonim', | ||
"text" TEXT NOT NULL, | ||
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"postId" TEXT 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (e.g., Git) | ||
provider = "postgresql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,46 @@ | ||
import { KeluhPost, Comment } from '@/app/types'; | ||
"use server" | ||
|
||
const STORAGE_KEY = 'keluh-posts'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
export function getPosts(): KeluhPost[] { | ||
if (typeof window === 'undefined') return []; | ||
const posts = localStorage.getItem(STORAGE_KEY); | ||
return posts ? JSON.parse(posts) : []; | ||
const prisma = new PrismaClient(); | ||
|
||
export async function getPosts() { | ||
return await prisma.post.findMany({ | ||
include: { comments: true }, | ||
}); | ||
} | ||
|
||
export function savePost(post: KeluhPost) { | ||
const posts = getPosts(); | ||
posts.unshift(post); | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(posts)); | ||
export async function savePost(post) { | ||
const { comments, ...postData } = post; | ||
return await prisma.post.create({ | ||
data: postData, | ||
}); | ||
} | ||
|
||
export function updatePost(updatedPost: KeluhPost) { | ||
const posts = getPosts(); | ||
const index = posts.findIndex((post) => post.id === updatedPost.id); | ||
if (index !== -1) { | ||
posts[index] = updatedPost; | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(posts)); | ||
} | ||
export async function updatePost(updatedPost) { | ||
return await prisma.post.update({ | ||
where: { id: updatedPost.id }, | ||
data: updatedPost, | ||
}); | ||
} | ||
|
||
export function addComment(postId: string, comment: Comment) { | ||
const posts = getPosts(); | ||
const post = posts.find((p) => p.id === postId); | ||
if (post) { | ||
post.comments.push(comment); | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(posts)); | ||
} | ||
export async function addComment(postId, comment) { | ||
return await prisma.comment.create({ | ||
data: { | ||
...comment, | ||
postId, | ||
}, | ||
}); | ||
} | ||
|
||
export function toggleLove(postId: string) { | ||
const posts = getPosts(); | ||
const post = posts.find((p) => p.id === postId); | ||
export async function toggleLove(postId) { | ||
const post = await prisma.post.findUnique({ | ||
where: { id: postId }, | ||
}); | ||
if (post) { | ||
post.loveCount += 1; | ||
localStorage.setItem(STORAGE_KEY, JSON.stringify(posts)); | ||
return post.loveCount; | ||
return await prisma.post.update({ | ||
where: { id: postId }, | ||
data: { loveCount: post.loveCount + 1 }, | ||
}); | ||
} | ||
return 0; | ||
} |