From 40443e70211a9819191aee808ce6a6c067340941 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Wed, 17 Sep 2025 17:32:38 +0800 Subject: [PATCH] Voting Mobile API Add: - api/mobile/voting/ ### No Issue --- .../api/mobile/voting/[id]/[status]/route.ts | 116 ++++++++++++++++++ src/app/api/mobile/voting/[id]/route.ts | 53 ++++++++ src/app/api/mobile/voting/route.ts | 54 ++++++++ 3 files changed, 223 insertions(+) create mode 100644 src/app/api/mobile/voting/[id]/[status]/route.ts create mode 100644 src/app/api/mobile/voting/[id]/route.ts create mode 100644 src/app/api/mobile/voting/route.ts diff --git a/src/app/api/mobile/voting/[id]/[status]/route.ts b/src/app/api/mobile/voting/[id]/[status]/route.ts new file mode 100644 index 00000000..23295d65 --- /dev/null +++ b/src/app/api/mobile/voting/[id]/[status]/route.ts @@ -0,0 +1,116 @@ +import _ from "lodash"; +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export { GET, PUT }; + +async function GET( + request: Request, + { params }: { params: { id: string; status: string } } +) { + try { + const { id, status } = params; + console.log("[ID]", id); + const fixStatusName = _.startCase(status); + console.log("[STATUS]", fixStatusName); + + const data = await prisma.voting.findMany({ + where: { + authorId: id, + Voting_Status: { + name: fixStatusName, + }, + }, + }); + + return NextResponse.json({ + success: true, + message: "Success get voting", + data: data, + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + success: false, + message: "Error get voting", + reason: (error as Error).message, + }); + } +} + +async function PUT( + request: Request, + { params }: { params: { id: string; status: string } } +) { + try { + const { id, status } = params; + console.log("[ID]", id); + const fixStatusName = _.startCase(status); + console.log("[STATUS]", fixStatusName); + + const checkData = await prisma.voting.findFirst({ + where: { + id: id, + }, + select: { + id: true, + Voting_Status: { + select: { + name: true, + }, + }, + }, + }); + + console.log("[CHECKDATA]", checkData); + + if (!checkData) + return NextResponse.json({ + success: false, + message: "Voting tidak ditemukan", + }); + + if (checkData?.Voting_Status?.name === "Publish") { + return NextResponse.json({ + success: false, + message: "Voting telah terpublish", + }); + } + + const checkStatus = await prisma.masterStatus.findFirst({ + where: { + name: fixStatusName, + }, + }); + + if (!checkStatus) + return NextResponse.json({ + success: false, + message: "Status tidak ditemukan", + }); + + const updateData = await prisma.voting.update({ + where: { + id: id, + }, + data: { + voting_StatusId: checkStatus.id, + }, + }); + + console.log("[UPDATE]", updateData); + + return NextResponse.json({ + success: true, + message: "Success update voting", + data: updateData, + }); + } catch (error) { + console.log("[ERROR]", error); + return NextResponse.json({ + success: false, + message: "Error update voting", + reason: (error as Error).message, + }); + } +} diff --git a/src/app/api/mobile/voting/[id]/route.ts b/src/app/api/mobile/voting/[id]/route.ts new file mode 100644 index 00000000..d8d818ae --- /dev/null +++ b/src/app/api/mobile/voting/[id]/route.ts @@ -0,0 +1,53 @@ +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export { GET }; + +async function GET(request: Request, { params }: { params: { id: string } }) { + try { + const { id } = params; + console.log("[ID]", id); + + const data = await prisma.voting.findUnique({ + where: { + id: id, + }, + include: { + Voting_DaftarNamaVote: { + orderBy: { + createdAt: "asc", + }, + where: { + isActive: true, + }, + }, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }); + + return NextResponse.json({ + success: true, + message: "Success get voting", + data: data, + }); + } catch (error) { + console.log("[ERROR VOTING]", error); + return NextResponse.json({ + success: false, + message: "Error get voting", + reason: (error as Error).message, + }); + } +} diff --git a/src/app/api/mobile/voting/route.ts b/src/app/api/mobile/voting/route.ts new file mode 100644 index 00000000..62796b68 --- /dev/null +++ b/src/app/api/mobile/voting/route.ts @@ -0,0 +1,54 @@ +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export { POST }; + +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, + deskripsi: data.deskripsi, + awalVote: data.awalVote, + akhirVote: data.akhirVote, + authorId: data.authorId, + }, + }); + + if (!create) return { status: 400, message: "Gagal Membuat Vote" }; + + for (let v of data.listVote) { + const val = v.value; + + const namaVote = await prisma.voting_DaftarNamaVote.create({ + data: { + value: val, + votingId: create.id, + }, + }); + + if (!namaVote) return { status: 400, message: "Gagal Membuat List" }; + } + + return NextResponse.json( + { + success: true, + message: "Success create voting", + }, + { status: 201 } + ); + } catch (error) { + return NextResponse.json( + { + success: false, + message: "Error create voting", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +}