From 1645803e3600ea710b65d91b330eabd6cc9d6dcf Mon Sep 17 00:00:00 2001 From: amel Date: Thu, 3 Oct 2024 17:18:13 +0800 Subject: [PATCH] upd: be banner Deskripsi: - database table banner - api crud banner No Issues --- prisma/schema.prisma | 13 +++ src/app/api/.gitkeep | 0 src/app/api/banner/[id]/route.ts | 113 +++++++++++++++++++++++++++ src/app/api/banner/route.ts | 74 ++++++++++++++++++ src/module/_global/bin/val_global.ts | 5 +- src/module/_global/fun/view_dir.ts | 22 ++++++ src/module/_global/index.ts | 2 + 7 files changed, 227 insertions(+), 2 deletions(-) delete mode 100644 src/app/api/.gitkeep create mode 100644 src/app/api/banner/[id]/route.ts create mode 100644 src/app/api/banner/route.ts create mode 100644 src/module/_global/fun/view_dir.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b8a6ce8..9a702b6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -59,6 +59,7 @@ model Village { Project Project[] Division Division[] ColorTheme ColorTheme[] + BannerImage BannerImage[] } model Group { @@ -481,6 +482,18 @@ model ColorTheme { updatedAt DateTime @updatedAt } +model BannerImage { + id String @id @default(cuid()) + Village Village? @relation(fields: [idVillage], references: [id]) + idVillage String? + title String + extension String + image String + isActive Boolean @default(true) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} + model Subscription { id String @id @default(cuid()) data Json diff --git a/src/app/api/.gitkeep b/src/app/api/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/api/banner/[id]/route.ts b/src/app/api/banner/[id]/route.ts new file mode 100644 index 0000000..22ec8d2 --- /dev/null +++ b/src/app/api/banner/[id]/route.ts @@ -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 }); + } +} \ No newline at end of file diff --git a/src/app/api/banner/route.ts b/src/app/api/banner/route.ts new file mode 100644 index 0000000..a8010ae --- /dev/null +++ b/src/app/api/banner/route.ts @@ -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 }); + } +} \ No newline at end of file diff --git a/src/module/_global/bin/val_global.ts b/src/module/_global/bin/val_global.ts index 2442734..906b7d7 100644 --- a/src/module/_global/bin/val_global.ts +++ b/src/module/_global/bin/val_global.ts @@ -5,15 +5,16 @@ import { RefObject } from "react"; export const pwd_key_config = "fchgvjknlmdfnbvghhujlaknsdvjbhknlkmsdbdyu567t8y9u30r4587638y9uipkoeghjvuyi89ipkoefmnrjbhtiu4or9ipkoemnjfbhjiuoijdklnjhbviufojkejnshbiuojijknehgruyu" export const globalRole = hookstate('') export const DIR = { + parentDir: "cm0x8a1as0001bp5te7354yrp", task: "cm0xhcqf0000dacbbixjb09yn", project: "cm0xhc9sv000bacbb7rfikw1k", document: "cm0xhbkf50009acbbtw03qo4l", village: "cm0xhb91o0007acbbkx8rk8hj", user: "cm0x8dbwn0005bp5tgmfcthzw", - + banner: "cm1sxex19004938bjvyaq8vta" } -export const keyWibu= 'padahariminggukuturutayahkekotanaikdelmanistimewakududukdimuka' +export const keyWibu = 'padahariminggukuturutayahkekotanaikdelmanistimewakududukdimuka' export const TEMA = hookstate({ utama: "#19345E", diff --git a/src/module/_global/fun/view_dir.ts b/src/module/_global/fun/view_dir.ts new file mode 100644 index 0000000..0005934 --- /dev/null +++ b/src/module/_global/fun/view_dir.ts @@ -0,0 +1,22 @@ +export async function funViewDir({ dirId }: { dirId: string }) { + + try { + const res = await fetch("https://wibu-storage.wibudev.com/api/dir/" + dirId + "/tree", { + method: "GET", + headers: { + Authorization: `Bearer ${process.env.WS_APIKEY}` + } + }); + + if (res.ok) { + const hasil = await res.json() + return { success: true, data: hasil.data } + } else { + const errorText = await res.text(); + return { success: false, data: {} } + } + } catch (error) { + console.error("Upload error:", error); + return { success: false, data: {} } + } +} \ No newline at end of file diff --git a/src/module/_global/index.ts b/src/module/_global/index.ts index 0e87f99..d9ee900 100644 --- a/src/module/_global/index.ts +++ b/src/module/_global/index.ts @@ -25,6 +25,7 @@ import NotificationCustome from "./components/notification_custome"; import { ScrollProvider } from "./components/scroll_provider"; import SkeletonUser from "./components/skeleton_user"; import SkeletonList from "./components/skeleton_list"; +import { funViewDir } from "./fun/view_dir"; export { WARNA }; export { LayoutLogin }; @@ -59,3 +60,4 @@ export { currentScroll } export { SkeletonUser } export { SkeletonList } export { keyWibu } +export { funViewDir }