Portofolio
Fix: - api/mobile/portofolio/[id]/route.ts: tambah put dan delete File: Add: - src/app/api/mobile/file/: upload & delete ### No Issue
This commit is contained in:
41
src/app/api/mobile/file/[id]/route.ts
Normal file
41
src/app/api/mobile/file/[id]/route.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export { DELETE };
|
||||||
|
|
||||||
|
async function DELETE(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const { id } = params;
|
||||||
|
|
||||||
|
const deleteFile = await fetch(
|
||||||
|
`https://wibu-storage.wibudev.com/api/files/${id}/delete`,
|
||||||
|
{
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${process.env.WS_APIKEY}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (deleteFile.ok) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: "File berhasil dihapus",
|
||||||
|
},
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: "Gagal menghapus file, coba lagi nanti (error: 500)",
|
||||||
|
reason: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,15 +2,9 @@ import { funGetDirectoryNameByValue } from "@/app_modules/_global/fun/get";
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const contentType = request.headers.get("content-type");
|
|
||||||
console.log("Incoming Content-Type:", contentType);
|
|
||||||
|
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
const file: any = formData.get("file");
|
|
||||||
const dirId = formData.get("dirId");
|
const dirId = formData.get("dirId");
|
||||||
|
|
||||||
console.log("formData >>", formData);
|
|
||||||
|
|
||||||
const keyOfDirectory = await funGetDirectoryNameByValue({
|
const keyOfDirectory = await funGetDirectoryNameByValue({
|
||||||
value: dirId as string,
|
value: dirId as string,
|
||||||
});
|
});
|
||||||
@@ -27,11 +21,12 @@ export async function POST(request: Request) {
|
|||||||
const dataRes = await res.json();
|
const dataRes = await res.json();
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
console.log(
|
|
||||||
`Success upload ${keyOfDirectory}: ${JSON.stringify(dataRes.data, null, 2)}`
|
|
||||||
);
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: true, data: dataRes.data },
|
{
|
||||||
|
success: true,
|
||||||
|
data: dataRes.data,
|
||||||
|
message: "Success upload file " + keyOfDirectory,
|
||||||
|
},
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { prisma } from "@/lib";
|
import { prisma } from "@/lib";
|
||||||
|
|
||||||
export { GET };
|
export { GET, DELETE, PUT };
|
||||||
|
|
||||||
async function GET(request: Request, { params }: { params: { id: string } }) {
|
async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||||
try {
|
try {
|
||||||
@@ -50,3 +50,193 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const { id } = context.params;
|
||||||
|
|
||||||
|
const data = await prisma.portofolio.findUnique({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
BusinessMaps: {
|
||||||
|
select: {
|
||||||
|
pinId: true,
|
||||||
|
imageId: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (data?.logoId != null) {
|
||||||
|
const id = data?.logoId;
|
||||||
|
const deleteLogo = await fetch(
|
||||||
|
`https://wibu-storage.wibudev.com/api/files/${id}/delete`,
|
||||||
|
{
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${process.env.WS_APIKEY}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (deleteLogo.ok) {
|
||||||
|
console.log("Success delete logo");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data?.BusinessMaps?.pinId != null) {
|
||||||
|
const pinId = data?.BusinessMaps?.pinId;
|
||||||
|
const deletePin = await fetch(
|
||||||
|
`https://wibu-storage.wibudev.com/api/files/${pinId}/delete`,
|
||||||
|
{
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${process.env.WS_APIKEY}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (deletePin.ok) {
|
||||||
|
console.log(`Success delete pin`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const imageId = data?.BusinessMaps?.imageId;
|
||||||
|
const deleteImage = await fetch(
|
||||||
|
`https://wibu-storage.wibudev.com/api/files/${imageId}/delete`,
|
||||||
|
{
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${process.env.WS_APIKEY}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (deleteImage.ok) {
|
||||||
|
console.log(`Success delete image`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error delete logo", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
const deletePortoMedsos = await prisma.portofolio_MediaSosial.delete({
|
||||||
|
where: {
|
||||||
|
portofolioId: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// const deleteMap = await prisma.businessMaps.delete({
|
||||||
|
// where: {
|
||||||
|
// portofolioId: id,
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
||||||
|
// console.log("DELETE PORTOFOLIO MEDSOS >>", deletePortoMedsos);
|
||||||
|
|
||||||
|
const deletePortofolio = await prisma.portofolio.delete({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: true, message: "Berhasil menghapus data" },
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: "Gagal menghapus data, coba lagi nanti (error: 500)",
|
||||||
|
reason: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function PUT(request: Request, { params }: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const { id } = params;
|
||||||
|
const { data } = await request.json();
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const category = searchParams.get("category");
|
||||||
|
|
||||||
|
let message;
|
||||||
|
|
||||||
|
if (category === "detail") {
|
||||||
|
const updateDetail = await prisma.portofolio.update({
|
||||||
|
where: { id },
|
||||||
|
data: {
|
||||||
|
namaBisnis: data.namaBisnis,
|
||||||
|
alamatKantor: data.alamatKantor,
|
||||||
|
tlpn: data.tlpn,
|
||||||
|
deskripsi: data.deskripsi,
|
||||||
|
masterBidangBisnisId: data.masterBidangBisnisId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!updateDetail) {
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
message: "Gagal mengupdate detail portofolio",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
message = "Berhasil mengupdate detail portofolio";
|
||||||
|
} else if (category === "medsos") {
|
||||||
|
const updateMedsos = await prisma.portofolio_MediaSosial.update({
|
||||||
|
where: { portofolioId: id },
|
||||||
|
data: {
|
||||||
|
facebook: data.facebook,
|
||||||
|
instagram: data.instagram,
|
||||||
|
tiktok: data.tiktok,
|
||||||
|
twitter: data.twitter,
|
||||||
|
youtube: data.youtube,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!updateMedsos) {
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
message: "Gagal mengupdate medsos portofolio",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
message = "Berhasil mengupdate medsos portofolio";
|
||||||
|
} else if (category === "logo") {
|
||||||
|
const updateLogo = await prisma.portofolio.update({
|
||||||
|
where: { id },
|
||||||
|
data: {
|
||||||
|
logoId: data.fileId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!updateLogo) {
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
message: "Gagal mengupdate logo portofolio",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
message = "Berhasil mengupdate logo portofolio";
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: message,
|
||||||
|
},
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error update data portofolio", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: "Gagal mengupdate data, coba lagi nanti (error: 500)",
|
||||||
|
reason: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user