Mobile: API Voting

### No Issue
This commit is contained in:
2025-09-19 17:52:16 +08:00
parent 7e3204da1c
commit 4061cb75ba
3 changed files with 306 additions and 131 deletions

View File

@@ -7,50 +7,79 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
const { id } = params; const { id } = params;
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
const authorId = searchParams.get("authorId"); const authorId = searchParams.get("authorId");
// const category = searchParams.get("category"); const category = searchParams.get("category");
console.log("[ID Voting]", id); console.log("[ID]", id);
console.log("[AUTHOR ID Voting]", authorId); console.log("[AUTHOR ID]", authorId);
console.log("[CATEGORY]", category);
console.log("[CEK DATA]");
let fixData; let fixData;
try { try {
const cek = await prisma.voting_Kontributor.count({ if (category === "checked") {
where: { const cek = await prisma.voting_Kontributor.count({
authorId: authorId, where: {
votingId: id, authorId: authorId,
}, votingId: id,
}); },
});
const cekPilihan = await prisma.voting_Kontributor.findFirst({ const cekPilihan = await prisma.voting_Kontributor.findFirst({
where: { where: {
authorId: authorId, authorId: authorId,
votingId: id, votingId: id,
}, },
select: { select: {
Voting_DaftarNamaVote: { Voting_DaftarNamaVote: {
select: { select: {
value: true, value: true,
},
}, },
}, },
}, });
});
if (cek > 0) { if (cek > 0) {
fixData = true; fixData = true;
} else { } else {
fixData = false; 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({ return NextResponse.json({
success: true, success: true,
message: "Berhasil mendapatkan data", message: "Berhasil mendapatkan data",

View File

@@ -1,13 +1,12 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import prisma from "@/lib/prisma"; import prisma from "@/lib/prisma";
import _ from "lodash";
export { GET, DELETE, PUT, POST }; export { GET, DELETE, PUT, POST };
async function GET(request: Request, { params }: { params: { id: string } }) { async function GET(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
try { try {
const { id } = params;
console.log("[ID]", id);
const data = await prisma.voting.findUnique({ const data = await prisma.voting.findUnique({
where: { where: {
id: id, 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({ return NextResponse.json({
success: true, success: true,
message: "Success get voting", message: "Success get voting",
@@ -65,15 +84,12 @@ async function DELETE(
}, },
}); });
console.log("[DELETE LIST VOTE NAME]", deleteListVoteName);
const deleteData = await prisma.voting.delete({ const deleteData = await prisma.voting.delete({
where: { where: {
id: id, id: id,
}, },
}); });
console.log("[DELETE DATA]", deleteData);
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
@@ -89,89 +105,95 @@ async function DELETE(
} }
async function PUT(request: Request, { params }: { params: { id: string } }) { 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 { try {
const { id } = params; if (category === "edit") {
const { data } = await request.json(); const updateVoting = await prisma.voting.update({
where: {
console.log("[ID]", id); id: id,
console.log("[DATA]", data); },
data: {
const updateVoting = await prisma.voting.update({ title: data.title,
where: { deskripsi: data.deskripsi.trim(),
id: id, awalVote: data.awalVote,
}, akhirVote: data.akhirVote,
data: { },
title: data.title, select: {
deskripsi: data.deskripsi.trim(), Voting_DaftarNamaVote: {
awalVote: data.awalVote, where: {
akhirVote: data.akhirVote, isActive: true,
}, },
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) { if (!updateVoting)
console.log("[VOTING LIST >>]", v); return NextResponse.json({ status: 400, message: "Gagal Update" });
const namaPilihan = await prisma.voting_DaftarNamaVote.create({
data: { const deleatePrevPilihan = await prisma.voting_DaftarNamaVote.deleteMany({
value: v, where: {
votingId: id, votingId: id,
}, },
}); });
console.log("[NAMA PILIHAN]", namaPilihan); if (!deleatePrevPilihan)
if (!namaPilihan)
return NextResponse.json({ return NextResponse.json({
status: 400, 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({ return NextResponse.json({
success: true, success: true,
message: "Berhasil menghapus data", message: "Berhasil mengupdate data",
}); });
} catch (error) { } catch (error) {
return NextResponse.json({ return NextResponse.json({
success: false, success: false,
message: "Gagal menghapus data", message: "Gagal mengupdate data",
reason: (error as Error).message, reason: (error as Error).message,
}); });
} }
} }
// Voting masuk melalui API ini
async function POST(request: Request, { params }: { params: { id: string } }) { async function POST(request: Request, { params }: { params: { id: string } }) {
const { id } = params; const { id } = params;
const { data } = await request.json(); const { data } = await request.json();
console.log("[ID]", id);
console.log("[CHOOSE ID]", data);
try { try {
const findData = await prisma.voting_DaftarNamaVote.findFirst({ const findData = await prisma.voting_DaftarNamaVote.findFirst({
where: { where: {
@@ -198,7 +220,6 @@ async function POST(request: Request, { params }: { params: { id: string } }) {
}, },
}); });
console.log("[UPDATE DATA]", updateData);
if (!updateData) if (!updateData)
return NextResponse.json({ return NextResponse.json({
@@ -223,8 +244,6 @@ async function POST(request: Request, { params }: { params: { id: string } }) {
}, },
}); });
console.log("[CREATE KONTRIBUTOR]", createKontributor);
if (!createKontributor) if (!createKontributor)
return NextResponse.json({ return NextResponse.json({
success: false, success: false,

View File

@@ -1,5 +1,6 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import prisma from "@/lib/prisma"; import prisma from "@/lib/prisma";
import _ from "lodash";
export { POST, GET }; export { POST, GET };
@@ -7,8 +8,6 @@ async function POST(request: Request) {
try { try {
const { data } = await request.json(); const { data } = await request.json();
console.log("[DATA]", data);
const create = await prisma.voting.create({ const create = await prisma.voting.create({
data: { data: {
title: data.title, title: data.title,
@@ -56,53 +55,181 @@ async function POST(request: Request) {
async function GET(request: Request) { async function GET(request: Request) {
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
const search = searchParams.get("search"); const search = searchParams.get("search");
const category = searchParams.get("category");
const authorId = searchParams.get("authorId");
let fixData;
try { try {
const data = await prisma.voting.findMany({ if (category === "beranda") {
orderBy: { fixData = await prisma.voting.findMany({
updatedAt: "desc", orderBy: {
}, updatedAt: "desc",
where: {
voting_StatusId: "1",
isArsip: false,
isActive: true,
akhirVote: {
gte: new Date(),
}, },
title: { where: {
contains: search || "", voting_StatusId: "1",
mode: "insensitive", isArsip: false,
}, isActive: true,
}, akhirVote: {
include: { gte: new Date(),
Voting_DaftarNamaVote: { },
orderBy: { title: {
createdAt: "asc", contains: search || "",
mode: "insensitive",
}, },
}, },
Author: { include: {
select: { Voting_DaftarNamaVote: {
id: true, orderBy: {
username: true, createdAt: "asc",
Profile: { },
select: { },
id: true, Author: {
name: true, select: {
imageId: true, 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( return NextResponse.json(
{ {
success: true, success: true,
message: "Berhasil mendapatkan data", message: "Berhasil mendapatkan data",
data: data, data: fixData,
}, },
{ {
status: 200, status: 200,