Voting Mobile API
Add: - api/mobile/voting/ ### No Issue
This commit is contained in:
116
src/app/api/mobile/voting/[id]/[status]/route.ts
Normal file
116
src/app/api/mobile/voting/[id]/[status]/route.ts
Normal file
@@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
53
src/app/api/mobile/voting/[id]/route.ts
Normal file
53
src/app/api/mobile/voting/[id]/route.ts
Normal file
@@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/app/api/mobile/voting/route.ts
Normal file
54
src/app/api/mobile/voting/route.ts
Normal file
@@ -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 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user