From 4061cb75ba10acf6a8ba45587e4e5aa434525b16 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Fri, 19 Sep 2025 17:52:16 +0800 Subject: [PATCH] Mobile: API Voting ### No Issue --- .../mobile/voting/[id]/contribution/route.ts | 95 ++++++--- src/app/api/mobile/voting/[id]/route.ts | 145 +++++++------ src/app/api/mobile/voting/route.ts | 197 ++++++++++++++---- 3 files changed, 306 insertions(+), 131 deletions(-) diff --git a/src/app/api/mobile/voting/[id]/contribution/route.ts b/src/app/api/mobile/voting/[id]/contribution/route.ts index c48ba192..7adbaf34 100644 --- a/src/app/api/mobile/voting/[id]/contribution/route.ts +++ b/src/app/api/mobile/voting/[id]/contribution/route.ts @@ -7,50 +7,79 @@ async function GET(request: Request, { params }: { params: { id: string } }) { const { id } = params; const { searchParams } = new URL(request.url); const authorId = searchParams.get("authorId"); - // const category = searchParams.get("category"); + const category = searchParams.get("category"); - console.log("[ID Voting]", id); - console.log("[AUTHOR ID Voting]", authorId); - - console.log("[CEK DATA]"); + console.log("[ID]", id); + console.log("[AUTHOR ID]", authorId); + console.log("[CATEGORY]", category); let fixData; try { - const cek = await prisma.voting_Kontributor.count({ - where: { - authorId: authorId, - votingId: id, - }, - }); + if (category === "checked") { + const cek = await prisma.voting_Kontributor.count({ + where: { + authorId: authorId, + votingId: id, + }, + }); - const cekPilihan = await prisma.voting_Kontributor.findFirst({ - where: { - authorId: authorId, - votingId: id, - }, - select: { - Voting_DaftarNamaVote: { - select: { - value: true, + const cekPilihan = await prisma.voting_Kontributor.findFirst({ + where: { + authorId: authorId, + votingId: id, + }, + select: { + Voting_DaftarNamaVote: { + select: { + value: true, + }, }, }, - }, - }); + }); - if (cek > 0) { - fixData = true; - } else { - fixData = false; + if (cek > 0) { + fixData = true; + } else { + fixData = false; + } + + fixData = { + isContribution: cek > 0, + nameChoice: cekPilihan?.Voting_DaftarNamaVote?.value, + }; + } else if (category === "list") { + const listKontributor = await prisma.voting_Kontributor.findMany({ + where: { + votingId: id, + }, + select: { + Voting_DaftarNamaVote: { + select: { + value: true, + }, + }, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }); + + console.log("[LIST KONTRIBUTOR]", listKontributor); + + fixData = listKontributor; } - fixData = { - isContribution: cek > 0, - nameChoice: cekPilihan?.Voting_DaftarNamaVote?.value, - }; - - console.log("[FIX DATA]", fixData); - return NextResponse.json({ success: true, message: "Berhasil mendapatkan data", diff --git a/src/app/api/mobile/voting/[id]/route.ts b/src/app/api/mobile/voting/[id]/route.ts index 24db0a8f..a6ff1597 100644 --- a/src/app/api/mobile/voting/[id]/route.ts +++ b/src/app/api/mobile/voting/[id]/route.ts @@ -1,13 +1,12 @@ import { NextResponse } from "next/server"; import prisma from "@/lib/prisma"; +import _ from "lodash"; export { GET, DELETE, PUT, POST }; async function GET(request: Request, { params }: { params: { id: string } }) { + const { id } = params; try { - const { id } = params; - console.log("[ID]", id); - const data = await prisma.voting.findUnique({ where: { id: id, @@ -37,6 +36,26 @@ async function GET(request: Request, { params }: { params: { id: string } }) { }, }); + const listNamaVote = data?.Voting_DaftarNamaVote || []; + + for (let v of listNamaVote) { + + const kontributor = await prisma.voting_Kontributor.findMany({ + where: { + voting_DaftarNamaVoteId: v.id, + }, + }); + + const updateData = await prisma.voting_DaftarNamaVote.update({ + where: { + id: v.id, + }, + data: { + jumlah: kontributor.length, + }, + }); + } + return NextResponse.json({ success: true, message: "Success get voting", @@ -65,15 +84,12 @@ async function DELETE( }, }); - console.log("[DELETE LIST VOTE NAME]", deleteListVoteName); - const deleteData = await prisma.voting.delete({ where: { id: id, }, }); - console.log("[DELETE DATA]", deleteData); return NextResponse.json({ success: true, @@ -89,89 +105,95 @@ async function DELETE( } async function PUT(request: Request, { params }: { params: { id: string } }) { + const { id } = params; + const { data } = await request.json(); + const { searchParams } = new URL(request.url); + const category = searchParams.get("category"); + let fixData; + try { - const { id } = params; - const { data } = await request.json(); - - console.log("[ID]", id); - console.log("[DATA]", data); - - const updateVoting = await prisma.voting.update({ - where: { - id: id, - }, - data: { - title: data.title, - deskripsi: data.deskripsi.trim(), - awalVote: data.awalVote, - akhirVote: data.akhirVote, - }, - select: { - Voting_DaftarNamaVote: { - where: { - isActive: true, + if (category === "edit") { + const updateVoting = await prisma.voting.update({ + where: { + id: id, + }, + data: { + title: data.title, + deskripsi: data.deskripsi.trim(), + awalVote: data.awalVote, + akhirVote: data.akhirVote, + }, + select: { + Voting_DaftarNamaVote: { + where: { + isActive: true, + }, }, }, - }, - }); - - console.log("[UPDATE VOTING]", updateVoting); - - if (!updateVoting) - return NextResponse.json({ status: 400, message: "Gagal Update" }); - - const deleatePrevPilihan = await prisma.voting_DaftarNamaVote.deleteMany({ - where: { - votingId: id, - }, - }); - - console.log("[DELETE PREV PILIHAN]", deleatePrevPilihan); - - if (!deleatePrevPilihan) - return NextResponse.json({ - status: 400, - message: "Gagal Update Pilihan", }); - for (let v of data.listVote) { - console.log("[VOTING LIST >>]", v); - const namaPilihan = await prisma.voting_DaftarNamaVote.create({ - data: { - value: v, + if (!updateVoting) + return NextResponse.json({ status: 400, message: "Gagal Update" }); + + const deleatePrevPilihan = await prisma.voting_DaftarNamaVote.deleteMany({ + where: { votingId: id, }, }); - console.log("[NAMA PILIHAN]", namaPilihan); - - if (!namaPilihan) + if (!deleatePrevPilihan) return NextResponse.json({ status: 400, - message: "Gagal Membuat List", + message: "Gagal Update Pilihan", }); + + for (let v of data.listVote) { + const namaPilihan = await prisma.voting_DaftarNamaVote.create({ + data: { + value: v, + votingId: id, + }, + }); + + if (!namaPilihan) + return NextResponse.json({ + status: 400, + message: "Gagal Membuat List", + }); + } + } else if (category === "archive") { + const updateVoting = await prisma.voting.update({ + where: { + id: id, + }, + data: { + isArsip: data, + }, + }); + + + if (!updateVoting) + return NextResponse.json({ status: 400, message: "Gagal Update" }); } return NextResponse.json({ success: true, - message: "Berhasil menghapus data", + message: "Berhasil mengupdate data", }); } catch (error) { return NextResponse.json({ success: false, - message: "Gagal menghapus data", + message: "Gagal mengupdate data", reason: (error as Error).message, }); } } +// Voting masuk melalui API ini async function POST(request: Request, { params }: { params: { id: string } }) { const { id } = params; const { data } = await request.json(); - console.log("[ID]", id); - console.log("[CHOOSE ID]", data); - try { const findData = await prisma.voting_DaftarNamaVote.findFirst({ where: { @@ -198,7 +220,6 @@ async function POST(request: Request, { params }: { params: { id: string } }) { }, }); - console.log("[UPDATE DATA]", updateData); if (!updateData) return NextResponse.json({ @@ -223,8 +244,6 @@ async function POST(request: Request, { params }: { params: { id: string } }) { }, }); - console.log("[CREATE KONTRIBUTOR]", createKontributor); - if (!createKontributor) return NextResponse.json({ success: false, diff --git a/src/app/api/mobile/voting/route.ts b/src/app/api/mobile/voting/route.ts index a2f025e9..84e8cb58 100644 --- a/src/app/api/mobile/voting/route.ts +++ b/src/app/api/mobile/voting/route.ts @@ -1,5 +1,6 @@ import { NextResponse } from "next/server"; import prisma from "@/lib/prisma"; +import _ from "lodash"; export { POST, GET }; @@ -7,8 +8,6 @@ async function POST(request: Request) { try { const { data } = await request.json(); - console.log("[DATA]", data); - const create = await prisma.voting.create({ data: { title: data.title, @@ -56,53 +55,181 @@ async function POST(request: Request) { async function GET(request: Request) { const { searchParams } = new URL(request.url); const search = searchParams.get("search"); - + const category = searchParams.get("category"); + const authorId = searchParams.get("authorId"); + + let fixData; + try { - const data = await prisma.voting.findMany({ - orderBy: { - updatedAt: "desc", - }, - where: { - voting_StatusId: "1", - isArsip: false, - isActive: true, - akhirVote: { - gte: new Date(), + if (category === "beranda") { + fixData = await prisma.voting.findMany({ + orderBy: { + updatedAt: "desc", }, - title: { - contains: search || "", - mode: "insensitive", - }, - }, - include: { - Voting_DaftarNamaVote: { - orderBy: { - createdAt: "asc", + where: { + voting_StatusId: "1", + isArsip: false, + isActive: true, + akhirVote: { + gte: new Date(), + }, + title: { + contains: search || "", + mode: "insensitive", }, }, - Author: { - select: { - id: true, - username: true, - Profile: { - select: { - id: true, - name: true, - imageId: true, + include: { + Voting_DaftarNamaVote: { + orderBy: { + createdAt: "asc", + }, + }, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, }, }, }, }, - }, - }); + }); + } else if (category === "contribution") { + const data = await prisma.voting_Kontributor.findMany({ + orderBy: { + createdAt: "desc", + }, + where: { + authorId: authorId, + }, + include: { + Voting: { + select: { + id: true, + title: true, + awalVote: true, + akhirVote: true, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }, + }, + }); - console.log("[DATA]", data); + const result = data.map((item) => ({ + contributionId: item.id, + isActive: item.isActive, + createdAt: item.createdAt, + updatedAt: item.updatedAt, + votingId: item.votingId, + authorId: item.authorId, + voting_DaftarNamaVoteId: item.voting_DaftarNamaVoteId, + id: item.Voting?.id, + title: item.Voting?.title, + awalVote: item.Voting?.awalVote, + akhirVote: item.Voting?.akhirVote, + Author: item.Voting?.Author, + })); + + fixData = result; + } else if (category === "all-history") { + fixData = await prisma.voting.findMany({ + orderBy: { + updatedAt: "desc", + }, + where: { + voting_StatusId: "1", + isActive: true, + akhirVote: { + lte: new Date(), + }, + title: { + contains: search || "", + mode: "insensitive", + }, + }, + include: { + Voting_DaftarNamaVote: { + orderBy: { + createdAt: "asc", + }, + }, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }); + } else if (category === "my-history") { + fixData = await prisma.voting.findMany({ + orderBy: { + updatedAt: "desc", + }, + where: { + authorId: authorId as any, + voting_StatusId: "1", + isActive: true, + akhirVote: { + lte: new Date(), + }, + title: { + contains: search || "", + mode: "insensitive", + }, + }, + include: { + Voting_DaftarNamaVote: { + orderBy: { + createdAt: "asc", + }, + }, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }); + } return NextResponse.json( { success: true, message: "Berhasil mendapatkan data", - data: data, + data: fixData, }, { status: 200,