upd: api mobile
Deskripsi: - api mobile banner create - api mobile banner delete - api mobile banner edit - api mobile banner get one NO Issues
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
||||||
import { funGetUserByCookies } from "@/module/auth";
|
import { funGetUserById } from "@/module/auth";
|
||||||
import { createLogUser } from "@/module/user";
|
import { createLogUserMobile } from "@/module/user";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
|
||||||
@@ -8,9 +8,12 @@ import { NextResponse } from "next/server";
|
|||||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
try {
|
||||||
const { id } = context.params;
|
const { id } = context.params;
|
||||||
const user = await funGetUserByCookies()
|
const { searchParams } = new URL(request.url);
|
||||||
if (user.id == undefined) {
|
const user = searchParams.get("user")
|
||||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
const userLogin = await funGetUserById({ id: String(user) })
|
||||||
|
|
||||||
|
if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await prisma.bannerImage.findUnique({
|
const data = await prisma.bannerImage.findUnique({
|
||||||
@@ -31,9 +34,10 @@ export async function GET(request: Request, context: { params: { id: string } })
|
|||||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
try {
|
||||||
const { id } = context.params;
|
const { id } = context.params;
|
||||||
const user = await funGetUserByCookies()
|
const { user } = (await request.json());
|
||||||
if (user.id == undefined) {
|
const userLogin = await funGetUserById({ id: user })
|
||||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const upd = await prisma.bannerImage.update({
|
const upd = await prisma.bannerImage.update({
|
||||||
@@ -47,7 +51,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
|
|
||||||
|
|
||||||
// create log user
|
// create log user
|
||||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus banner', table: 'bannerImage', data: id })
|
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus banner', table: 'bannerImage', data: id, user: String(userLogin.id) })
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Berhasil menghapus banner" }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Berhasil menghapus banner" }, { status: 200 });
|
||||||
|
|
||||||
@@ -62,17 +66,18 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
try {
|
||||||
const { id } = context.params;
|
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 body = await request.formData()
|
||||||
const file = body.get("file") as File
|
const file = body.get("file") as File
|
||||||
const data = body.get("data")
|
const data = body.get("data")
|
||||||
const { title } = JSON.parse(data as string)
|
const { title, user } = JSON.parse(data as string)
|
||||||
|
|
||||||
|
|
||||||
|
const userLogin = await funGetUserById({ id: user })
|
||||||
|
|
||||||
|
if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
const upd = await prisma.bannerImage.update({
|
const upd = await prisma.bannerImage.update({
|
||||||
where: {
|
where: {
|
||||||
id: String(id)
|
id: String(id)
|
||||||
@@ -102,7 +107,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create log user
|
// create log user
|
||||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data banner', table: 'bannerImage', data: user.id })
|
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data banner', table: 'bannerImage', data: id, user: String(userLogin.id) })
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Berhasil mengupdate banner" }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Berhasil mengupdate banner" }, { status: 200 });
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { DIR, funUploadFile, funViewDir, prisma } from "@/module/_global";
|
import { DIR, funUploadFile, prisma } from "@/module/_global";
|
||||||
import { funGetUserById } from "@/module/auth";
|
import { funGetUserById } from "@/module/auth";
|
||||||
import { createLogUser } from "@/module/user";
|
import { createLogUserMobile } from "@/module/user";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
|
||||||
@@ -38,32 +38,22 @@ export async function GET(request: Request) {
|
|||||||
// CREATE BANNER
|
// CREATE BANNER
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
console.log('masuklas')
|
|
||||||
console.log(request)
|
|
||||||
const body = await request.formData()
|
const body = await request.formData()
|
||||||
const fileAwal = body.get("file");
|
const file = body.get("file") as File;
|
||||||
const file = JSON.parse(fileAwal as string)
|
|
||||||
const fileFix = file as File
|
|
||||||
const fileCoba = new File([fileFix], fileFix.name, { type: fileFix.type});
|
|
||||||
const data = body.get("data");
|
const data = body.get("data");
|
||||||
|
|
||||||
console.log(body, fileCoba)
|
|
||||||
|
|
||||||
const { title, user } = JSON.parse(data as string)
|
const { title, user } = JSON.parse(data as string)
|
||||||
|
|
||||||
if (user == "null" || user == undefined || user == "") {
|
|
||||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const userLogin = await funGetUserById({ id: user })
|
const userLogin = await funGetUserById({ id: user })
|
||||||
|
|
||||||
|
if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
const fExt = fileCoba.name.split(".").pop()
|
|
||||||
const fName = fileCoba.name.replace("." + fExt, "")
|
|
||||||
const newFile = new File([fileCoba], fileCoba.name, { type: fileCoba.type });
|
|
||||||
|
|
||||||
const ini = funViewDir({ dirId: DIR.user })
|
const fExt = file.name.split(".").pop()
|
||||||
const upload = await funUploadFile({ file: fileCoba, dirId: DIR.banner })
|
const newFile = new File([file], file.name, { type: file.type });
|
||||||
|
|
||||||
|
const upload = await funUploadFile({ file: newFile, dirId: DIR.banner })
|
||||||
if (upload.success) {
|
if (upload.success) {
|
||||||
const create = await prisma.bannerImage.create({
|
const create = await prisma.bannerImage.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -71,12 +61,14 @@ export async function POST(request: Request) {
|
|||||||
idVillage: userLogin.idVillage,
|
idVillage: userLogin.idVillage,
|
||||||
extension: String(fExt),
|
extension: String(fExt),
|
||||||
image: upload.data.id
|
image: upload.data.id
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// create log user
|
// create log user
|
||||||
const log = await createLogUser({ act: 'CREATE', desc: 'User menambah data banner baru', table: 'bannerImage', data: String(userLogin.id) })
|
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah data banner baru', table: 'bannerImage', data: String(create.id), user: String(userLogin.id) })
|
||||||
|
|
||||||
return Response.json({ success: true, message: 'Sukses menambah data banner' }, { status: 200 });
|
return Response.json({ success: true, message: 'Sukses menambah data banner' }, { status: 200 });
|
||||||
} else {
|
} else {
|
||||||
return Response.json({ success: false, message: 'Gagal menambah data banner' }, { status: 200 });
|
return Response.json({ success: false, message: 'Gagal menambah data banner' }, { status: 200 });
|
||||||
|
|||||||
Reference in New Issue
Block a user