Mobile API Voting
Add: - api/mobile/voting/[id]/contribution : mengecek kontibutor - api/mobile/voting: method : GET , POST , PUT, DEL ### No Issue
This commit is contained in:
66
src/app/api/mobile/voting/[id]/contribution/route.ts
Normal file
66
src/app/api/mobile/voting/[id]/contribution/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export { GET };
|
||||
|
||||
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");
|
||||
|
||||
console.log("[ID Voting]", id);
|
||||
console.log("[AUTHOR ID Voting]", authorId);
|
||||
|
||||
console.log("[CEK DATA]");
|
||||
|
||||
let fixData;
|
||||
|
||||
try {
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (cek > 0) {
|
||||
fixData = true;
|
||||
} else {
|
||||
fixData = false;
|
||||
}
|
||||
|
||||
fixData = {
|
||||
isContribution: cek > 0,
|
||||
nameChoice: cekPilihan?.Voting_DaftarNamaVote?.value,
|
||||
};
|
||||
|
||||
console.log("[FIX DATA]", fixData);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: fixData,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal mendapatkan data",
|
||||
reason: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export { GET };
|
||||
export { GET, DELETE, PUT, POST };
|
||||
|
||||
async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
@@ -51,3 +51,196 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const { id } = params;
|
||||
|
||||
const deleteListVoteName = await prisma.voting_DaftarNamaVote.deleteMany({
|
||||
where: {
|
||||
votingId: id,
|
||||
},
|
||||
});
|
||||
|
||||
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,
|
||||
message: "Berhasil menghapus data",
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal menghapus data",
|
||||
reason: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function PUT(request: Request, { params }: { params: { id: string } }) {
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
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,
|
||||
votingId: id,
|
||||
},
|
||||
});
|
||||
|
||||
console.log("[NAMA PILIHAN]", namaPilihan);
|
||||
|
||||
if (!namaPilihan)
|
||||
return NextResponse.json({
|
||||
status: 400,
|
||||
message: "Gagal Membuat List",
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Berhasil menghapus data",
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal menghapus data",
|
||||
reason: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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: {
|
||||
id: data.chooseId,
|
||||
},
|
||||
select: {
|
||||
jumlah: true,
|
||||
value: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!findData)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
});
|
||||
|
||||
const updateData = await prisma.voting_DaftarNamaVote.update({
|
||||
where: {
|
||||
id: data.chooseId,
|
||||
},
|
||||
data: {
|
||||
jumlah: findData.jumlah + 1,
|
||||
},
|
||||
});
|
||||
|
||||
console.log("[UPDATE DATA]", updateData);
|
||||
|
||||
if (!updateData)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal Update Data",
|
||||
});
|
||||
|
||||
const createKontributor = await prisma.voting_Kontributor.create({
|
||||
data: {
|
||||
votingId: id,
|
||||
authorId: data.userId,
|
||||
voting_DaftarNamaVoteId: data.chooseId,
|
||||
},
|
||||
select: {
|
||||
Voting: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
authorId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log("[CREATE KONTRIBUTOR]", createKontributor);
|
||||
|
||||
if (!createKontributor)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal Menjadi Kontributor",
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Berhasil Voting",
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("[ERROR]", error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal menghapus data",
|
||||
reason: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export { POST };
|
||||
export { POST, GET };
|
||||
|
||||
async function POST(request: Request) {
|
||||
try {
|
||||
@@ -52,3 +52,72 @@ async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const search = searchParams.get("search");
|
||||
|
||||
try {
|
||||
const data = await prisma.voting.findMany({
|
||||
orderBy: {
|
||||
updatedAt: "desc",
|
||||
},
|
||||
where: {
|
||||
voting_StatusId: "1",
|
||||
isArsip: false,
|
||||
isActive: true,
|
||||
akhirVote: {
|
||||
gte: 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,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log("[DATA]", data);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: data,
|
||||
},
|
||||
{
|
||||
status: 200,
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal mendapatkan data",
|
||||
reason: (error as Error).message,
|
||||
},
|
||||
{
|
||||
status: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user