upd: be banner
Deskripsi: - database table banner - api crud banner No Issues
This commit is contained in:
113
src/app/api/banner/[id]/route.ts
Normal file
113
src/app/api/banner/[id]/route.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { createLogUser } from "@/module/user";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ONE BANNER
|
||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const data = await prisma.bannerImage.findUnique({
|
||||
where: {
|
||||
id: String(id)
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan banner", data }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan banner, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// DELETE BANNER
|
||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const upd = await prisma.bannerImage.update({
|
||||
where: {
|
||||
id: String(id)
|
||||
},
|
||||
data: {
|
||||
isActive: false
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus banner', table: 'bannerImage', data: id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil menghapus banner" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menghapus banner, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// UPDATE BANNER
|
||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params;
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.formData()
|
||||
const file = body.get("file") as File
|
||||
const data = body.get("data")
|
||||
const { title } = JSON.parse(data as string)
|
||||
|
||||
|
||||
const upd = await prisma.bannerImage.update({
|
||||
where: {
|
||||
id: String(id)
|
||||
},
|
||||
data: {
|
||||
title
|
||||
},
|
||||
select: {
|
||||
image: true
|
||||
}
|
||||
})
|
||||
|
||||
if (String(file) != "undefined" && String(file) != "null") {
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fileName = id + '.' + fExt;
|
||||
const newFile = new File([file], fileName, { type: file.type });
|
||||
await funDeleteFile({ fileId: String(upd.image) })
|
||||
const upload = await funUploadFile({ file: newFile, dirId: DIR.banner })
|
||||
await prisma.bannerImage.update({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
data: {
|
||||
image: upload.data.id
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data banner', table: 'bannerImage', data: user.id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mengupdate banner" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengupdate banner, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
74
src/app/api/banner/route.ts
Normal file
74
src/app/api/banner/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { DIR, funUploadFile, funViewDir, prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { createLogUser } from "@/module/user";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ALL BANNER
|
||||
export async function GET() {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const data = await prisma.bannerImage.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idVillage: user.idVillage
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan banner", data }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan data banner, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CREATE BANNER
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.formData()
|
||||
const file = body.get("file") as File;
|
||||
const data = body.get("data");
|
||||
|
||||
const { title } = JSON.parse(data as string)
|
||||
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
const newFile = new File([file], file.name, { type: file.type });
|
||||
|
||||
const ini = funViewDir({ dirId: DIR.user })
|
||||
const upload = await funUploadFile({ file: newFile, dirId: DIR.banner })
|
||||
if (upload.success) {
|
||||
const create = await prisma.bannerImage.create({
|
||||
data: {
|
||||
title: title,
|
||||
idVillage: user.idVillage,
|
||||
extension: String(fExt),
|
||||
image: upload.data.id
|
||||
}
|
||||
})
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'CREATE', desc: 'User menambah data banner baru', table: 'bannerImage', data: user.id })
|
||||
|
||||
return Response.json({ success: true, message: 'Sukses menambah data banner' }, { status: 200 });
|
||||
} else {
|
||||
return Response.json({ success: false, message: 'Gagal menambah data banner' }, { status: 200 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menambahkan banner, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user