diff --git a/.gitignore b/.gitignore index f5d4bce..a82de76 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,6 @@ next-env.d.ts # folder foto kandidat /public/image/ /public/file/ -certificates \ No newline at end of file +certificates + +test.png \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index 8c40967..d7534ce 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index d32500b..888b6fa 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "seed": "npx tsx prisma/seed.ts" }, "dependencies": { + "@elysiajs/cors": "^1.3.1", + "@elysiajs/swagger": "^1.3.0", "@hookstate/core": "^4.0.1", "@hookstate/localstored": "^4.0.2", "@mantine/carousel": "^7.11.1", @@ -42,6 +44,7 @@ "dayjs": "^1.11.11", "echarts": "^5.5.1", "echarts-for-react": "^3.0.2", + "elysia": "^1.3.1", "embla-carousel-autoplay": "^7.1.0", "embla-carousel-react": "^7.1.0", "iron-session": "^8.0.2", diff --git a/src/app/api/mobile/project/[id]/lainnya/route.ts b/src/app/api/mobile/project/[id]/lainnya/route.ts index bf3f042..d6a8260 100644 --- a/src/app/api/mobile/project/[id]/lainnya/route.ts +++ b/src/app/api/mobile/project/[id]/lainnya/route.ts @@ -1,15 +1,17 @@ import { prisma } from "@/module/_global"; -import { funGetUserByCookies } from "@/module/auth"; -import { createLogUser } from "@/module/user"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; import { NextResponse } from "next/server"; // HAPUS PROJECT YG TELAH DIBATALKAN export async function DELETE(request: Request, context: { params: { id: string } }) { try { - const user = await funGetUserByCookies() - if (user.id == undefined) { - return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + const { user } = await request.json() + const userMobile = await funGetUserById({ id: String(user) }) + + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } const { id } = context.params @@ -24,7 +26,7 @@ export async function DELETE(request: Request, context: { params: { id: string } { success: false, message: "Gagal mendapatkan kegiatan, data tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -38,8 +40,8 @@ export async function DELETE(request: Request, context: { params: { id: string } }) // create log user - const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus data kegiatan', table: 'project', data: String(id) }) - return NextResponse.json({ success: true, message: "Kegiatan berhasil dihapus", user: user.id }, { status: 200 }); + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus data kegiatan', table: 'project', data: String(id), user: userMobile.id }) + return NextResponse.json({ success: true, message: "Kegiatan berhasil dihapus" }, { status: 200 }); } catch (error) { console.error(error); diff --git a/src/app/api/v2/[[...slug]]/route.ts b/src/app/api/v2/[[...slug]]/route.ts new file mode 100644 index 0000000..2274ed5 --- /dev/null +++ b/src/app/api/v2/[[...slug]]/route.ts @@ -0,0 +1,51 @@ +import Elysia from "elysia"; +import { swagger } from "@elysiajs/swagger"; +import { t } from "elysia"; +import fs from "fs/promises"; +import cors from "@elysiajs/cors"; +const Api = new Elysia({ + prefix: "/api/v2" +}) + .onError((c) => { + console.log(c) + return c + }) + // send a file + .post("/test", async (c) => { + try { + // const { file } = c.body + + console.log("terima file") + // const buffer = Buffer.from(await file.arrayBuffer()); + // await fs.writeFile("./test.png", buffer); + + return { + success: true, + message: "File uploaded successfully", + } + } catch (error) { + console.error(JSON.stringify(error)); + return { + success: false, + message: "File uploaded failed", + } + } + }, { + body: t.Object({ + name: t.String(), + file: t.File(), + }), + }) + +const ApiServer = new Elysia() + .use(cors({ + origin: "*", + methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], + })) + .use(swagger({ + path: "/api/v2/docs" + })) + .use(Api) + +export const GET = ApiServer.handle +export const POST = ApiServer.handle \ No newline at end of file diff --git a/src/app/api/v2/_test/route.ts b/src/app/api/v2/_test/route.ts new file mode 100644 index 0000000..429642e --- /dev/null +++ b/src/app/api/v2/_test/route.ts @@ -0,0 +1,18 @@ +import { NextResponse } from "next/server"; +import fs from "fs/promises"; + +export const POST = async (request: Request) => { + try { + console.log("[POST]") + const formData = await request.formData() + // const file = formData.get("file") as File; + // const buffer = Buffer.from(await file.arrayBuffer()); + // await fs.writeFile("./test.png", buffer); + return NextResponse.json({ success: true, message: "Berhasil mendapatkan banner", data: "file" }, { 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 }); + + } + +} \ No newline at end of file