Mobile API
Add: - app/api/mobile/profile/[id] Fix: - app/api/mobile/profile/route: clear console Middle: Fix: - console DB_Url ### No Issue
This commit is contained in:
100
src/app/api/mobile/profile/[id]/route.ts
Normal file
100
src/app/api/mobile/profile/[id]/route.ts
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,8 +21,6 @@ async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("data", data);
|
|
||||||
|
|
||||||
const create = await prisma.profile.create({
|
const create = await prisma.profile.create({
|
||||||
data: {
|
data: {
|
||||||
userId: data.id,
|
userId: data.id,
|
||||||
|
|||||||
@@ -59,6 +59,8 @@ export const middleware = async (req: NextRequest) => {
|
|||||||
const { pathname } = req.nextUrl;
|
const { pathname } = req.nextUrl;
|
||||||
|
|
||||||
const apiBaseUrl = new URL(req.url).origin || process.env.NEXT_PUBLIC_API_URL;
|
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
|
// Handle CORS preflight
|
||||||
const corsResponse = handleCors(req);
|
const corsResponse = handleCors(req);
|
||||||
|
|||||||
Reference in New Issue
Block a user