80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
import { DIR, funUploadFile, prisma } from "@/module/_global";
|
|
import { funGetUserById } from "@/module/auth";
|
|
import { createLogUserMobile } from "@/module/user";
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
// GET ALL BANNER
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const userMobile = searchParams.get("user")
|
|
|
|
if (userMobile == "null" || userMobile == undefined || userMobile == "") {
|
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
|
}
|
|
|
|
const user = await funGetUserById({ id: userMobile })
|
|
|
|
|
|
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 (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
|
|
// CREATE BANNER
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.formData()
|
|
const file = body.get("file") as File;
|
|
const data = body.get("data");
|
|
|
|
const { title, user } = JSON.parse(data as string)
|
|
const userLogin = await funGetUserById({ id: user })
|
|
|
|
if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") {
|
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
|
}
|
|
|
|
|
|
const fExt = file.name.split(".").pop()
|
|
const newFile = new File([file], file.name, { type: file.type });
|
|
|
|
const upload = await funUploadFile({ file: newFile, dirId: DIR.banner })
|
|
if (upload.success) {
|
|
const create = await prisma.bannerImage.create({
|
|
data: {
|
|
title: title,
|
|
idVillage: userLogin.idVillage,
|
|
extension: String(fExt),
|
|
image: upload.data.id
|
|
},
|
|
select: {
|
|
id: true,
|
|
}
|
|
})
|
|
|
|
// create log user
|
|
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah data banner baru', table: 'bannerImage', data: String(create.id), user: String(userLogin.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 (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
} |