API Mobile
Fix: - api/mobile/profile/route.tsx: hapus validasi email dan di buat khusus - api/mobile/profile/[id]/route.ts: cat untuk memilih update apa yang di lakikan Add: - api/mobile/validate/: Validasi email ### No Issue
This commit is contained in:
@@ -47,45 +47,82 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
|
||||
}
|
||||
|
||||
try {
|
||||
let message;
|
||||
const { id } = params;
|
||||
const body = await request.json();
|
||||
const { data } = body;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const category = searchParams.get("category");
|
||||
|
||||
const cekEmail = await prisma.profile.findUnique({
|
||||
where: {
|
||||
email: data.email,
|
||||
},
|
||||
});
|
||||
|
||||
if (category === "profile") {
|
||||
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" });
|
||||
}
|
||||
|
||||
if (cekEmail && cekEmail.id != id)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Email sudah digunakan",
|
||||
message = "Berhasil edit profile";
|
||||
} else if (category === "photo") {
|
||||
const updateData = await prisma.profile.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
imageId: data.fileId,
|
||||
},
|
||||
});
|
||||
|
||||
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 foto",
|
||||
});
|
||||
}
|
||||
|
||||
if (!updateData) {
|
||||
return NextResponse.json({ success: false, message: "Gagal update" });
|
||||
message = "Berhasil edit foto profile";
|
||||
} else if (category === "background") {
|
||||
const updateData = await prisma.profile.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
imageBackgroundId: data.fileId,
|
||||
},
|
||||
});
|
||||
if (!updateData) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Gagal update background",
|
||||
});
|
||||
}
|
||||
|
||||
message = "Berhasil edit background profile";
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Berhasil edit profile",
|
||||
message: message,
|
||||
});
|
||||
} catch (error) {
|
||||
backendLogger.error("Error edit profile", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
@@ -7,27 +7,15 @@ async function POST(request: Request) {
|
||||
try {
|
||||
const { data } = await request.json();
|
||||
|
||||
const existingEmail = await prisma.profile.findUnique({
|
||||
where: {
|
||||
email: data.email,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingEmail) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Email telah digunakan",
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const create = await prisma.profile.create({
|
||||
await prisma.profile.create({
|
||||
data: {
|
||||
userId: data.id,
|
||||
name: data.name,
|
||||
email: data.email,
|
||||
alamat: data.alamat,
|
||||
jenisKelamin: data.jenisKelamin,
|
||||
imageId: data.imageId || "",
|
||||
imageBackgroundId: data.imageBackgroundId || "",
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
38
src/app/api/mobile/validate/email/route.ts
Normal file
38
src/app/api/mobile/validate/email/route.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const { data } = await request.json();
|
||||
const email = data;
|
||||
|
||||
try {
|
||||
const existingEmail = await prisma.profile.findUnique({
|
||||
where: {
|
||||
email: email,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingEmail) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Email telah digunakan",
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Email tidak digunakan",
|
||||
status: 200,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal validasi email",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user