upd: refactor folder

This commit is contained in:
amel
2024-08-14 17:42:01 +08:00
parent de7ace8d49
commit a91fd8949c
30 changed files with 100 additions and 670 deletions

View File

@@ -1,34 +0,0 @@
import { getAllPosition } from "./get/getAllPosition";
import { getOnePosition } from "./get/getOnePosition";
import { createlPosition } from "./post/createPosition";
import { deletePosition } from "./post/deletePosition";
import { updatePosition } from "./post/updatePosition";
export const API_INDEX_POSITION = [
{
path: "get-all-position",
method: "GET",
bin: getAllPosition,
},
{
path: "create-position",
method: "POST",
bin: createlPosition,
},
{
path: "update-position",
method: "POST",
bin: updatePosition,
},
{
path: "delete-position",
method: "POST",
bin: deletePosition,
},
{
path: "get-one-position",
method: "GET",
bin: getOnePosition,
},
];

View File

@@ -1,15 +0,0 @@
import { NextRequest } from "next/server";
import { API_INDEX_POSITION } from "./api_index";
type Method = "GET" | "POST";
export async function apiPosition(req: NextRequest, method: Method) {
const { searchParams } = new URL(req.url);
const path = searchParams.get("path");
const act = API_INDEX_POSITION.find((v) => v.path === path && v.method === method);
if (!path)
return Response.json({ success: false, message: "page not found" }, { status: 404 });
if (act) return act.bin(req);
return Response.json({ success: false, message: "404" });
}

View File

@@ -1,53 +0,0 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _, { omit } from "lodash";
import { NextRequest } from "next/server";
export async function getAllPosition(req: NextRequest) {
try {
let grupFix
const searchParams = req.nextUrl.searchParams
const groupID = searchParams.get('groupId');
const active = searchParams.get('active');
const name = searchParams.get('name')
const user = await funGetUserByCookies()
if (groupID == "null") {
grupFix = user.idGroup
} else {
grupFix = groupID
}
const positions = await prisma.position.findMany({
where: {
idGroup: String(grupFix),
isActive: (active == "true" ? true : false),
name: {
contains: (name == undefined || name == null) ? "" : name,
mode: "insensitive"
}
},
select: {
id: true,
name: true,
isActive: true,
Group: {
select: {
name: true
}
}
},
});
const allData = positions.map((v: any) => ({
..._.omit(v, ["Group"]),
group: v.Group.name
}))
return Response.json(allData);
} catch (error) {
console.error(error);
return Response.json({ success: false, message: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -1,27 +0,0 @@
import { prisma } from "@/module/_global";
import { NextRequest } from "next/server";
export async function getOnePosition(req: NextRequest) {
try {
const searchParams = req.nextUrl.searchParams
const positionId = searchParams.get('positionId');
const getOne = await prisma.position.findUnique({
where: {
id: String(positionId),
},
select: {
id: true,
name: true,
idGroup: true,
},
});
return Response.json(getOne);
} catch (error) {
console.error(error);
return Response.json(
{ message: "Internal Server Error", success: false },
{ status: 500 }
);
}
}

View File

@@ -1,39 +0,0 @@
import { prisma } from "@/module/_global";
export async function createlPosition(req: Request) {
try {
const data = await req.json();
const cek = await prisma.position.count({
where: {
name: data.name,
idGroup: data.idGroup,
},
});
if (cek == 0) {
const positions = await prisma.position.create({
data: {
name: data.name,
idGroup: data.idGroup,
},
select: {
id: true,
name: true,
},
});
return Response.json(positions, { status: 201 });
} else {
return Response.json(
{ success: false, message: "Position sudah ada" },
{ status: 400 }
);
}
} catch (error) {
console.error(error);
return Response.json(
{ success: false, message: "Internal Server Error" },
{ status: 500 }
);
}
}

View File

@@ -1,31 +0,0 @@
import { prisma } from "@/module/_global";
import { revalidatePath } from "next/cache";
import { NextRequest } from "next/server";
export async function deletePosition(req: NextRequest) {
try {
const data = await req.json();
const active = data.isActive;
const update = await prisma.position.update({
where: {
id: data.id,
},
data: {
isActive: !active,
},
});
revalidatePath("/position");
return Response.json(
{ success: true, message: "Sukses Delete Position" },
{ status: 200 }
);
} catch (error) {
console.error(error);
return Response.json(
{ message: "Internal Server Error", success: false },
{ status: 500 }
);
}
}

View File

@@ -1,43 +0,0 @@
import { prisma } from "@/module/_global";
export async function updatePosition(req: Request) {
try {
const data = await req.json();
const cek = await prisma.position.count({
where: {
name: data.name,
idGroup: data.idGroup,
},
});
if (cek == 0) {
const update = await prisma.position.update({
where: {
id: data.id,
},
data: {
name: data.name,
idGroup: data.idGroup,
},
});
return Response.json({ success: true, message: "Sukses Update Position" }, { status: 200 });
} else {
return Response.json(
{ success: false, message: "Position sudah ada" },
{ status: 400 }
);
}
return Response.json(
{ success: true, message: "Sukses Update Position" },
{ status: 200 }
);
} catch (error) {
console.error(error);
return Response.json(
{ message: "Internal Server Error", success: false },
{ status: 500 }
);
}
}

View File

@@ -1,8 +1,6 @@
import { apiPosition } from "./api/api_position";
import TabListPosition from "./ui/tab_list_position";
import TabListGroup from "./ui/tab_list_position";
import NavbarListPosition from "./ui/navbar_list_position";
export { apiPosition };
export { NavbarListPosition }
export { TabListPosition }