api : add api
Deskripsi: - add gorup - add position - village No issue
This commit is contained in:
34
src/module/position/api/api_index.ts
Normal file
34
src/module/position/api/api_index.ts
Normal 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,
|
||||
},
|
||||
];
|
||||
|
||||
14
src/module/position/api/api_position.ts
Normal file
14
src/module/position/api/api_position.ts
Normal 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" });
|
||||
}
|
||||
20
src/module/position/api/get/getAllPosition.ts
Normal file
20
src/module/position/api/get/getAllPosition.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
24
src/module/position/api/get/getOnePosition.ts
Normal file
24
src/module/position/api/get/getOnePosition.ts
Normal 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
24
src/module/position/api/post/createPosition.ts
Normal file
24
src/module/position/api/post/createPosition.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
27
src/module/position/api/post/deletePosition.ts
Normal file
27
src/module/position/api/post/deletePosition.ts
Normal 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
22
src/module/position/api/post/updatePosition.ts
Normal file
22
src/module/position/api/post/updatePosition.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { apiPosition } from "./api/api_position";
|
||||
import ViewListPosition from "./view/view_list_position";
|
||||
|
||||
export { ViewListPosition }
|
||||
export { ViewListPosition };
|
||||
export { apiPosition };
|
||||
|
||||
Reference in New Issue
Block a user