-
Notifications
You must be signed in to change notification settings - Fork 0
release: MVP feature enhancements and documentation config update #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
be6d5c7
f175b2e
9cc4f81
bedc228
e4432a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "public"."CommunityPostLike" ( | ||
| "userId" TEXT NOT NULL, | ||
| "postId" TEXT NOT NULL, | ||
|
|
||
| CONSTRAINT "CommunityPostLike_pkey" PRIMARY KEY ("userId","postId") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "CommunityPostLike_postId_idx" ON "public"."CommunityPostLike"("postId"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "public"."CommunityPostLike" ADD CONSTRAINT "CommunityPostLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "public"."User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "public"."CommunityPostLike" ADD CONSTRAINT "CommunityPostLike_postId_fkey" FOREIGN KEY ("postId") REFERENCES "public"."CommunityPost"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,9 @@ | ||||||||||||||||||
| import type { Request, Response } from 'express'; | ||||||||||||||||||
| import { | ||||||||||||||||||
| addLikeToPost, | ||||||||||||||||||
| createComment, | ||||||||||||||||||
| createPost, | ||||||||||||||||||
| findAllPosts, | ||||||||||||||||||
| toggleLikeOnPost, | ||||||||||||||||||
| type PostCategory, | ||||||||||||||||||
| } from './community.service.js'; | ||||||||||||||||||
| import { asyncHandler } from '../../handler/async.handler.js'; | ||||||||||||||||||
|
|
@@ -54,11 +54,23 @@ export const createCommentHandler = asyncHandler(async (req: Request, res: Respo | |||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| export const addLikeHandler = asyncHandler(async (req: Request, res: Response) => { | ||||||||||||||||||
| const userId = req.user?.id; | ||||||||||||||||||
| const { postId } = req.params; | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!userId) { | ||||||||||||||||||
| return errorResponse( | ||||||||||||||||||
| res, | ||||||||||||||||||
| 401, | ||||||||||||||||||
| 'Tidak diizinkan', | ||||||||||||||||||
| 'ID pengguna tidak ditemukan dalam permintaan' | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
| if (!postId) { | ||||||||||||||||||
| return errorResponse(res, 400, 'Permintaan Tidak Valid', 'ID postingan diperlukan'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const post = await addLikeToPost(postId); | ||||||||||||||||||
| return successResponse(res, 200, 'Postingan berhasil disukai', { likeCount: post.likeCount }); | ||||||||||||||||||
| const post = await toggleLikeOnPost(userId, postId); | ||||||||||||||||||
| const message = post.isLiked ? 'Postingan berhasil disukai' : 'Suka pada postingan dibatalkan'; | ||||||||||||||||||
|
|
||||||||||||||||||
| return successResponse(res, 200, message, post); | ||||||||||||||||||
|
||||||||||||||||||
| return successResponse(res, 200, message, post); | |
| return successResponse(res, 200, message, { | |
| likeCount: post.likedCount, | |
| isLiked: post.isLiked, | |
| // Optionally include likedCount as an alias for forward compatibility | |
| // likedCount: post.likedCount, | |
| // ...include other post properties if needed | |
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,17 +109,63 @@ export async function createComment(userId: string, postId: string, content: str | |
| }); | ||
| } | ||
|
|
||
| export async function addLikeToPost(postId: string) { | ||
| const updatedPost = await prisma.communityPost.update({ | ||
| export async function toggleLikeOnPost(userId: string, postId: string) { | ||
| const existingLike = await prisma.communityPostLike.findUnique({ | ||
| where: { | ||
| id: postId, | ||
| }, | ||
| data: { | ||
| likeCount: { | ||
| increment: 1, | ||
| userId_postId: { | ||
| userId, | ||
| postId, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return updatedPost; | ||
| if (existingLike) { | ||
| const updatedPost = await prisma.$transaction([ | ||
| prisma.communityPostLike.delete({ | ||
| where: { | ||
| userId_postId: { | ||
| userId, | ||
| postId, | ||
| }, | ||
| }, | ||
| }), | ||
| prisma.communityPost.update({ | ||
| where: { | ||
| id: postId, | ||
| }, | ||
| data: { | ||
| likeCount: { | ||
| decrement: 1, | ||
| }, | ||
| }, | ||
| }), | ||
| ]); | ||
| return { | ||
| likedCount: updatedPost[1].likeCount, | ||
| isLiked: false, | ||
| }; | ||
| } else { | ||
| const updatedPost = await prisma.$transaction([ | ||
| prisma.communityPostLike.create({ | ||
| data: { | ||
| userId, | ||
| postId, | ||
| }, | ||
| }), | ||
| prisma.communityPost.update({ | ||
| where: { | ||
| id: postId, | ||
| }, | ||
| data: { | ||
| likeCount: { | ||
| increment: 1, | ||
| }, | ||
| }, | ||
| }), | ||
| ]); | ||
| return { | ||
| likedCount: updatedPost[1].likeCount, | ||
| isLiked: true, | ||
| }; | ||
| } | ||
|
Comment on lines
+122
to
+170
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { Router } from 'express'; | ||
| import { getDailyContentHandler } from './content.controller.js'; | ||
| import { requireAuth } from '../../middleware/auth.middleware.js'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.get('/daily', getDailyContentHandler); | ||
| router.get('/daily', requireAuth, getDailyContentHandler); | ||
|
|
||
| export default router; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import type { Request, Response } from 'express'; | ||
| import config from '../../config/index.js'; | ||
|
|
||
| export function getWelcomeHandler(_req: Request, res: Response) { | ||
| res.render('web/index', { | ||
| documentationUrl: 'https://documenter.getpostman.com/view/38960737/2sB3QJQBZ8', | ||
| documentationUrl: config.docsUrl, | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ dotenv.config(); | |||||
| const config = { | ||||||
| port: process.env.PORT || 3000, | ||||||
| databaseUrl: process.env.DATABASE_URL || '', | ||||||
| docsUrl: process.env.DOCS_URL || '', | ||||||
|
||||||
| docsUrl: process.env.DOCS_URL || '', | |
| docsUrl: process.env.DOCS_URL || 'https://documenter.getpostman.com/view/38960737/2sB3QJQBZ8', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] For a pure join table, ON DELETE CASCADE is typically preferred to avoid manual cleanup when deleting users or posts; RESTRICT will block deletions and can leave orphaned likeCount if deletes are forced elsewhere. Consider using ON DELETE CASCADE on both FKs.