From b9e92446ea63f7b57e105daf606cf7cdd2b6dc3d Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Mon, 25 Aug 2025 17:03:35 +0800 Subject: [PATCH] Mobile API Add: - app/api/mobile/profile/[id] Fix: - app/api/mobile/profile/route: clear console Middle: Fix: - console DB_Url ### No Issue --- src/app/api/mobile/profile/[id]/route.ts | 100 +++++++++++++++++++++++ src/app/api/mobile/profile/route.tsx | 2 - src/middleware.tsx | 2 + 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/app/api/mobile/profile/[id]/route.ts diff --git a/src/app/api/mobile/profile/[id]/route.ts b/src/app/api/mobile/profile/[id]/route.ts new file mode 100644 index 00000000..a9f30c93 --- /dev/null +++ b/src/app/api/mobile/profile/[id]/route.ts @@ -0,0 +1,100 @@ +import { prisma } from "@/lib"; +import backendLogger from "@/util/backendLogger"; +import { NextResponse } from "next/server"; + +export { GET, PUT }; +async function GET(request: Request, { params }: { params: { id: string } }) { + try { + let fixData; + const { id } = params; + + fixData = await prisma.profile.findFirst({ + where: { + id: id, + }, + include: { + User: true, + }, + }); + + return NextResponse.json( + { + success: true, + message: "Success get profile", + data: fixData, + }, + { status: 200 } + ); + } catch (error) { + backendLogger.error("Error get profile", error); + return NextResponse.json( + { + success: false, + message: "Error get profile", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} + +async function PUT(request: Request, { params }: { params: { id: string } }) { + if (request.method !== "PUT") { + return NextResponse.json( + { success: false, message: "Method not allowed" }, + { status: 405 } + ); + } + + try { + const { id } = params; + const body = await request.json(); + const { data } = body; + + const cekEmail = await prisma.profile.findUnique({ + where: { + email: data.email, + }, + }); + + + if (cekEmail && cekEmail.id != id) + return NextResponse.json({ + success: false, + message: "Email sudah digunakan", + }); + + const updateData = await prisma.profile.update({ + where: { + id: id, + }, + data: { + name: data.name, + email: data.email, + alamat: data.alamat, + jenisKelamin: data.jenisKelamin, + }, + }); + + if (!updateData) { + return NextResponse.json({ success: false, message: "Gagal update" }); + } + + return NextResponse.json({ + success: true, + message: "Berhasil edit profile", + }); + } catch (error) { + backendLogger.error("Error edit profile", error); + return NextResponse.json( + { + success: false, + message: "Error edit profile", + reason: (error as Error).message, + }, + { status: 500 } + ); + } finally { + await prisma.$disconnect(); + } +} diff --git a/src/app/api/mobile/profile/route.tsx b/src/app/api/mobile/profile/route.tsx index 5d769ba6..eb3e1651 100644 --- a/src/app/api/mobile/profile/route.tsx +++ b/src/app/api/mobile/profile/route.tsx @@ -21,8 +21,6 @@ async function POST(request: Request) { }); } - console.log("data", data); - const create = await prisma.profile.create({ data: { userId: data.id, diff --git a/src/middleware.tsx b/src/middleware.tsx index 4ad8930c..1a826580 100644 --- a/src/middleware.tsx +++ b/src/middleware.tsx @@ -59,6 +59,8 @@ export const middleware = async (req: NextRequest) => { const { pathname } = req.nextUrl; const apiBaseUrl = new URL(req.url).origin || process.env.NEXT_PUBLIC_API_URL; + const dbUrl = process.env.DATABASE_URL; + console.log("DATABASE_URL >>", dbUrl); // Handle CORS preflight const corsResponse = handleCors(req);