api : add api

Deskripsi:
- add gorup
- add position
- village

No issue
This commit is contained in:
lukman
2024-07-25 16:43:13 +08:00
parent 404734c8f6
commit 3af192313a
33 changed files with 670 additions and 3 deletions

View File

@@ -0,0 +1,34 @@
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

@@ -0,0 +1,14 @@
import { API_INDEX_POSITION } from "./api_index";
type Method = "GET" | "POST";
export async function apiPosition(req: Request, 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({ message: "page not found" }, { status: 404 });
if (act) return act.bin(req);
return Response.json({ message: "404" });
}

View File

@@ -0,0 +1,20 @@
import { prisma } from "@/module/_global";
export async function getAllPosition(req: Request) {
try {
const positions = await prisma.position.findMany({
where: {
isActive: true,
},
select: {
id: true,
name: true,
},
});
return Response.json(positions);
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}

View File

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

View File

@@ -0,0 +1,24 @@
import { prisma } from "@/module/_global";
export async function createlPosition(req: Request) {
try {
const data = await req.json();
const positions = await prisma.position.create({
data: {
name: data.name,
isActive: true,
idGroup: data.idGroup,
},
select: {
id: true,
name: true,
},
});
return Response.json(positions, { status: 201 });
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -0,0 +1,27 @@
import { prisma } from "@/module/_global";
export async function deletePosition(req: Request) {
try {
const data = await req.json();
const update = await prisma.position.update({
where: {
id: data.id,
},
data: {
isActive: false,
},
});
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

@@ -0,0 +1,22 @@
import { prisma } from "@/module/_global"
export async function updatePosition(req: Request) {
try {
const data = await req.json()
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 });
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error", success: false }, { status: 500 });
}
}

View File

@@ -1,3 +1,5 @@
import { apiPosition } from "./api/api_position";
import ViewListPosition from "./view/view_list_position";
export { ViewListPosition }
export { ViewListPosition };
export { apiPosition };