From b1e946124c42020d8ea47fd3541631c5cb0e743d Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Fri, 3 Oct 2025 17:38:48 +0800 Subject: [PATCH] Investmen & Donation Add: src/app/api/mobile/donation/ src/app/api/mobile/investment/[id]/news/ src/app/api/mobile/master/donation/ ### No Issue --- src/app/api/mobile/donation/[id]/route.ts | 44 ++++++ src/app/api/mobile/donation/route.ts | 85 +++++++++++ .../api/mobile/investment/[id]/news/route.ts | 140 ++++++++++++++++++ src/app/api/mobile/master/donation/route.ts | 72 +++++++++ 4 files changed, 341 insertions(+) create mode 100644 src/app/api/mobile/donation/[id]/route.ts create mode 100644 src/app/api/mobile/donation/route.ts create mode 100644 src/app/api/mobile/investment/[id]/news/route.ts create mode 100644 src/app/api/mobile/master/donation/route.ts diff --git a/src/app/api/mobile/donation/[id]/route.ts b/src/app/api/mobile/donation/[id]/route.ts new file mode 100644 index 00000000..27e9e7dd --- /dev/null +++ b/src/app/api/mobile/donation/[id]/route.ts @@ -0,0 +1,44 @@ +import { NextResponse } from "next/server"; + +export { GET }; + +async function GET(request: Request, { params }: { params: { id: string } }) { + const { id } = params; + const { searchParams } = new URL(request.url); + const category = searchParams.get("category"); + let fixData; + + console.log("[ID]", id); + console.log("[CATEGORY]", category); + + try { + if (category === "temporary") { + fixData = await prisma.donasi_TemporaryCreate.findUnique({ + where: { + id: id, + }, + }); + } else if (category === "permanent") { + fixData = await prisma.donasi.findUnique({ + where: { + id: id, + }, + }); + } + + return NextResponse.json({ + status: 200, + success: true, + message: "Data donasi berhasil diambil", + data: fixData, + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + status: 500, + success: false, + message: "Gagal mengambil data donasi", + reason: (error as Error).message, + }); + } +} diff --git a/src/app/api/mobile/donation/route.ts b/src/app/api/mobile/donation/route.ts new file mode 100644 index 00000000..6707da9d --- /dev/null +++ b/src/app/api/mobile/donation/route.ts @@ -0,0 +1,85 @@ +import { NextResponse } from "next/server"; + +export { POST }; + +async function POST(request: Request) { + const { data } = await request.json(); + const { searchParams } = new URL(request.url); + const category = searchParams.get("category"); + + console.log("[DATA]", data); + console.log("[CATEGORY]", category); + + let fixData; + try { + // CODE HERE + + if (category === "temporary") { + fixData = await prisma.donasi_TemporaryCreate.create({ + data: { + title: data.title, + target: data.target, + donasiMaster_DurasiId: data.durasiId, + donasiMaster_KategoriId: data.kategoriId, + imageId: data.imageId, + }, + }); + } else if (category === "permanent") { + const dataDonasi = await prisma.donasi.create({ + data: { + target: data.target, + title: data.title, + donasiMaster_DurasiId: data.donasiMaster_DurasiId, + donasiMaster_KategoriId: data.donasiMaster_KategoriId, + authorId: data.authorId, + namaBank: data.namaBank, + rekening: data.rekening, + imageId: data.imageId, + }, + select: { + id: true, + title: true, + authorId: true, + DonasiMaster_Status: { + select: { + name: true, + }, + }, + }, + }); + + if (!dataDonasi) return { status: 400, message: "Gagal disimpan" }; + const del = await prisma.donasi_TemporaryCreate.delete({ + where: { + id: data.id, + }, + }); + + const dataCerita = await prisma.donasi_Cerita.create({ + data: { + donasiId: dataDonasi.id, + pembukaan: data.CeritaDonasi.pembukaan, + cerita: data.CeritaDonasi.cerita, + imageId: data.imageCeritaId, + }, + }); + + if (!dataCerita) return { status: 400, message: "Gagal disimpan" }; + } + + return NextResponse.json({ + status: 201, + success: true, + message: "Donasi berhasil ditambahkan", + data: fixData, + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + status: 500, + success: false, + error: "Gagal menambah donasi", + reason: (error as Error).message, + }); + } +} diff --git a/src/app/api/mobile/investment/[id]/news/route.ts b/src/app/api/mobile/investment/[id]/news/route.ts new file mode 100644 index 00000000..390e0740 --- /dev/null +++ b/src/app/api/mobile/investment/[id]/news/route.ts @@ -0,0 +1,140 @@ +import _ from "lodash"; +import { prisma } from "@/lib"; +import { NextResponse } from "next/server"; + +export { POST, GET, DELETE }; + +async function POST(request: Request, { params }: { params: { id: string } }) { + const { id } = params; + const { data } = await request.json(); + let fixData; + + console.log("id", id); + console.log("data", data); + + try { + if (data && data.imageId) { + const createWithFile = await prisma.beritaInvestasi.create({ + data: { + investasiId: id, + title: _.startCase(data.title), + deskripsi: data.deskripsi, + imageId: data.imageId, + }, + }); + + fixData = createWithFile; + } + + const createWitOutFile = await prisma.beritaInvestasi.create({ + data: { + investasiId: id, + title: _.startCase(data.title), + deskripsi: data.deskripsi, + }, + }); + + fixData = createWitOutFile; + + return NextResponse.json({ + status: 201, + success: true, + message: "Berita berhasil ditambahkan", + data: fixData, + }); + } catch (error) { + console.log(["ERROR"], error); + return NextResponse.json({ + status: 500, + success: false, + error: "Gagal menambah berita", + }); + } +} + +async function GET(request: Request, { params }: { params: { id: string } }) { + const { id } = params; + console.log("id", id); + const { searchParams } = new URL(request.url); + const category = searchParams.get("category"); + + let fixData; + try { + if (category === "one-news") { + const data = await prisma.beritaInvestasi.findFirst({ + where: { + id: id, + }, + include: { + investasi: { + select: { + authorId: true, + }, + }, + }, + }); + + const authorId = data?.investasi?.authorId; + const newData = { + ...data, + authorId, + }; + + fixData = newData; + } else if (category === "all-news") { + fixData = await prisma.beritaInvestasi.findMany({ + orderBy: { + updatedAt: "desc", + }, + where: { + investasiId: id, + active: true, + }, + }); + } + + return NextResponse.json({ + status: 200, + success: true, + message: "Berita berhasil diambil", + data: fixData, + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + status: 500, + success: false, + error: "Gagal mengambil berita", + }); + } +} + +async function DELETE( + request: Request, + { params }: { params: { id: string } } +) { + const { id } = params; + console.log("id", id); + try { + const deleteData = await prisma.beritaInvestasi.delete({ + where: { + id: id, + }, + }); + + console.log("DELETE", deleteData); + + return NextResponse.json({ + status: 200, + success: true, + message: "Berita berhasil dihapus", + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + status: 500, + success: false, + error: "Gagal menghapus berita", + }); + } +} diff --git a/src/app/api/mobile/master/donation/route.ts b/src/app/api/mobile/master/donation/route.ts new file mode 100644 index 00000000..9204a0aa --- /dev/null +++ b/src/app/api/mobile/master/donation/route.ts @@ -0,0 +1,72 @@ +import { NextResponse } from "next/server"; + +export { GET }; + +async function GET(request: Request) { + const { searchParams } = new URL(request.url); + const category = searchParams.get("category"); + console.log("[CATEGORY]", category); + let fixData; + + try { + // CODE HERE + + if (category === "category") { + fixData = await prisma.donasiMaster_Kategori.findMany({ + orderBy: { + createdAt: "asc", + }, + where: { + active: true, + }, + }); + } else if (category === "duration") { + fixData = await prisma.donasiMaster_Durasi.findMany({ + orderBy: { + createdAt: "asc", + }, + where: { + active: true, + }, + }); + } else { + const category = await prisma.donasiMaster_Kategori.findMany({ + orderBy: { + createdAt: "asc", + }, + where: { + active: true, + }, + }); + + const duration = await prisma.donasiMaster_Durasi.findMany({ + orderBy: { + createdAt: "asc", + }, + where: { + active: true, + }, + }); + + fixData = { + category: category, + duration: duration, + }; + } + + return NextResponse.json({ + status: 200, + success: true, + message: "Master berhasil diambil", + data: fixData, + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + status: 500, + success: false, + error: "Gagal mengambil data master", + reason: (error as Error).message, + }); + } +}