From 6f10ff7c3e51e9d9f3f71ff7efb85eede25bfee6 Mon Sep 17 00:00:00 2001 From: bagasbanuna Date: Wed, 26 Nov 2025 16:16:08 +0800 Subject: [PATCH] API mobile forum ### No Issue --- src/app/api/mobile/block-user/route.ts | 47 +++++ src/app/api/mobile/forum/route.ts | 165 ++++++++++++------ .../api/mobile/master/app-category/route.ts | 28 +++ 3 files changed, 184 insertions(+), 56 deletions(-) create mode 100644 src/app/api/mobile/block-user/route.ts create mode 100644 src/app/api/mobile/master/app-category/route.ts diff --git a/src/app/api/mobile/block-user/route.ts b/src/app/api/mobile/block-user/route.ts new file mode 100644 index 00000000..76ca0a9b --- /dev/null +++ b/src/app/api/mobile/block-user/route.ts @@ -0,0 +1,47 @@ +import _ from "lodash"; +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export { POST }; + +async function POST(request: Request) { + const { data } = await request.json(); + + console.log("data >>", data); + console.log("menuFeature masuk>>", data.menuFeature); + + try { + const nameApp = _.lowerCase(data.menuFeature); + const menuFeature = await prisma.masterKategoriApp.findFirst({ + where: { value: nameApp }, + select: { + id: true, + }, + }); + + console.log(" fix menuFeature >>", menuFeature); + + const blockUser = await prisma.blockedUser.create({ + data: { + blockerId: data.blockerId, + blockedId: data.blockedId, + menuFeatureId: menuFeature?.id as any, + }, + }); + + return NextResponse.json({ + status: 200, + success: true, + message: "success", + // data: blockUser, + }); + } catch (error) { + console.log("[ERROR BLOCK USER] >>", error); + return NextResponse.json({ + status: 500, + success: false, + message: "error", + reason: (error as Error).message || error, + }); + } +} diff --git a/src/app/api/mobile/forum/route.ts b/src/app/api/mobile/forum/route.ts index e27fef7a..bd236cf4 100644 --- a/src/app/api/mobile/forum/route.ts +++ b/src/app/api/mobile/forum/route.ts @@ -36,11 +36,109 @@ async function GET(request: Request) { let fixData; const { searchParams } = new URL(request.url); const authorId = searchParams.get("authorId"); + const userLoginId = searchParams.get("userLoginId"); const search = searchParams.get("search"); + const category = searchParams.get("category"); + const page = searchParams.get("page"); + const takeData = 5; + const skipData = (Number(page) - 1) * takeData; + + + // console.log("authorId", authorId); + // console.log("userLoginId", userLoginId); + // console.log("search", search); + // console.log("category", category); + console.log("page", page); try { - if (authorId) { + if (category === "beranda") { + const blockUserId = await prisma.blockedUser + .findMany({ + where: { + blockerId: userLoginId as string, + }, + select: { + blockedId: true, + }, + }) + .then((res) => { + return res.map((item) => item.blockedId); + }); + + console.log("blockUserId", blockUserId); + const data = await prisma.forum_Posting.findMany({ + take: page ? takeData : undefined, + skip: page ? skipData : undefined, + orderBy: { + createdAt: "desc", + }, + where: { + isActive: true, + diskusi: { + mode: "insensitive", + contains: search || "", + }, + authorId: { + notIn: blockUserId, + }, + }, + + select: { + id: true, + diskusi: true, + createdAt: true, + isActive: true, + authorId: true, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + Forum_Komentar: { + where: { + isActive: true, + }, + }, + ForumMaster_StatusPosting: { + select: { + id: true, + status: true, + }, + }, + forumMaster_StatusPostingId: true, + }, + }); + + const newData = data.map((item) => { + const count = item.Forum_Komentar?.length ?? 0; + return { + ..._.omit(item, ["Forum_Komentar"]), + count, + }; + }); + + fixData = newData; + } else if (category === "forumku") { + + const count = await prisma.forum_Posting.count({ + where: { + isActive: true, + authorId: authorId, + }, + }) + + const data = await prisma.forum_Posting.findMany({ + take: page ? takeData : undefined, + skip: page ? skipData : undefined, orderBy: { createdAt: "desc", }, @@ -90,62 +188,18 @@ async function GET(request: Request) { }; }); - fixData = newData; + const dataFix = { + data: newData, + count, + } + + fixData = dataFix; } else { - const data = await prisma.forum_Posting.findMany({ - orderBy: { - createdAt: "desc", - }, - where: { - isActive: true, - diskusi: { - mode: "insensitive", - contains: search || "", - }, - }, - select: { - id: true, - diskusi: true, - createdAt: true, - isActive: true, - authorId: true, - Author: { - select: { - id: true, - username: true, - Profile: { - select: { - id: true, - name: true, - imageId: true, - }, - }, - }, - }, - Forum_Komentar: { - where: { - isActive: true, - }, - }, - ForumMaster_StatusPosting: { - select: { - id: true, - status: true, - }, - }, - forumMaster_StatusPostingId: true, - }, + return NextResponse.json({ + success: false, + message: "Gagal mendapatkan data", + reason: "Kategori tidak ditemukan", }); - - const newData = data.map((item) => { - const count = item.Forum_Komentar?.length ?? 0; - return { - ..._.omit(item, ["Forum_Komentar"]), - count, - }; - }); - - fixData = newData; } return NextResponse.json({ @@ -153,7 +207,6 @@ async function GET(request: Request) { message: "Berhasil mendapatkan data", data: fixData, }); - } catch (error) { console.log("[ERROR]", error); return NextResponse.json({ diff --git a/src/app/api/mobile/master/app-category/route.ts b/src/app/api/mobile/master/app-category/route.ts new file mode 100644 index 00000000..fa4c870b --- /dev/null +++ b/src/app/api/mobile/master/app-category/route.ts @@ -0,0 +1,28 @@ +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export { GET }; + +async function GET(request: Request) { + try { + const data = await prisma.masterKategoriApp.findMany({ + where: { + isActive: true, + }, + }); + return NextResponse.json({ + status: 200, + success: true, + message: "success", + data: data, + }); + } catch (error) { + console.log("[ERROR GET APP CATEGORY] >>", error); + return NextResponse.json({ + status: 500, + success: false, + message: "error", + reason: (error as Error).message || error, + }); + } +}