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

109
api.http
View File

@@ -4,4 +4,113 @@ Content-Type: application/json
{
"phone": "6287701790942"
}
// GROUP
###
GET http://localhost:3000/api/group/get?path=list-group HTTP/1.1
###
POST http://localhost:3000/api/group/post?path=create-group HTTP/1.1
Content-Type: application/json
{
"name": "amalia2",
"idVillage": "121212"
}
###
POST http://localhost:3000/api/group/post?path=update-group HTTP/1.1
Content-Type: application/json
{
"id": "1",
"name": "LPD2",
"idVillage": "121212"
}
###
POST http://localhost:3000/api/group/post?path=delete-group HTTP/1.1
Content-Type: application/json
{
"id": "1",
"idVillage": "121212"
}
###
GET http://localhost:3000/api/group/get?path=get-one-group HTTP/1.1
// VILLAGE
###
GET http://localhost:3000/api/village/get?path=get-all-village HTTP/1.1
###
GET http://localhost:3000/api/village/get?path=get-one-village HTTP/1.1
###
POST http://localhost:3000/api/village/post?path=create-village HTTP/1.1
Content-Type: application/json
{
"name": "nama satu satu ",
"desc": "description data"
}
###
POST http://localhost:3000/api/village/post?path=update-village HTTP/1.1
Content-Type: application/json
{
"id": "11",
"name": "nama satu new",
"desc": "description data new"
}
###
POST http://localhost:3000/api/village/post?path=delete-village HTTP/1.1
Content-Type: application/json
{
"id": "11"
}
// POSITION
###
GET http://localhost:3000/api/position/get?path=get-all-position HTTP/1.1
###
GET http://localhost:3000/api/position/get?path=get-one-position HTTP/1.1
###
POST http://localhost:3000/api/position/post?path=create-position HTTP/1.1
Content-Type: application/json
{
"name": "Wakil Bendahara 10",
"idGroup": "2"
}
###
POST http://localhost:3000/api/position/post?path=update-position HTTP/1.1
Content-Type: application/json
{
"id": "1",
"name": "Wakil Sekertaris",
"idGroup": "2"
}
###
POST http://localhost:3000/api/position/post?path=delete-position HTTP/1.1
Content-Type: application/json
{
"id": "1"
}

View File

@@ -0,0 +1,5 @@
import { apiGroup } from "@/module/group";
export async function GET(req: Request) {
return apiGroup(req, "GET")
}

View File

@@ -0,0 +1,5 @@
import { apiGroup } from "@/module/group";
export async function POST(req: Request) {
return apiGroup(req, "POST")
}

View File

@@ -0,0 +1,5 @@
import { apiPosition } from "@/module/position";
export async function GET(req: Request) {
return apiPosition(req, "GET");
}

View File

@@ -0,0 +1,5 @@
import { apiPosition } from "@/module/position";
export async function POST(req: Request) {
return apiPosition(req, "POST");
}

View File

@@ -0,0 +1,5 @@
import { apiViilage } from "@/module/village";
export async function GET(req: Request) {
return apiViilage(req, "GET");
}

View File

@@ -0,0 +1,5 @@
import { apiViilage } from "@/module/village";
export async function POST(req: Request) {
return apiViilage(req, "POST");
}

View File

@@ -1,3 +1,4 @@
export async function createProject(req: Request) {
const data = await req.json()
return Response.json({ message: "success create projects" })
}

View File

@@ -0,0 +1,13 @@
import { API_INDEX_GROUP } from "./api_index";
type Method = "GET" | "POST";
export async function apiGroup(req: Request, method: Method) {
const { searchParams } = new URL(req.url);
const path = searchParams.get("path");
const act = API_INDEX_GROUP.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,33 @@
import { getOneGroup } from "./get/getOneGroup";
import { listGroup } from "./get/listGroup";
import { createGroup } from "./post/createGroup";
import { deleteGroup } from "./post/deleteGroup";
import { updateGroup } from "./post/updateGroup";
export const API_INDEX_GROUP = [
{
path: "list-group",
method: "GET",
bin: listGroup,
},
{
path: "create-group",
method: "POST",
bin: createGroup,
},
{
path: "update-group",
method: "POST",
bin: updateGroup,
},
{
path: "delete-group",
method: "POST",
bin: deleteGroup,
},
{
path: "get-one-group",
method: "GET",
bin: getOneGroup,
},
];

View File

@@ -0,0 +1,32 @@
import { prisma } from "@/module/_global";
export async function getOneGroup(req: Request): Promise<Response> {
try {
// const groupId = req.params.id;
const groupId = "clz0v4kce0009e6mukfhzmyzb";
const getOne = await prisma.group.findUnique({
where: {
id: groupId,
},
select: {
id: true,
name: true,
},
});
if (!getOne) {
return Response.json(
{ message: "Grup tidak ditemukan", success: false },
{ status: 404 }
);
}
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,20 @@
import { prisma } from "@/module/_global";
export async function listGroup(req: Request): Promise<Response> {
try {
const groups = await prisma.group.findMany({
where: {
isActive: true,
},
select: {
id: true,
name: true,
},
});
return Response.json(groups);
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -0,0 +1,32 @@
import { prisma } from "@/module/_global";
export async function createGroup(req: Request){
try {
const data = await req.json();
if (!data || !data.name) {
return Response.json(
{ message: "Nama grup harus diisi" },
{ status: 400 }
);
}
const group = await prisma.group.create({
data: {
name: data.name,
isActive: true,
idVillage: data.idVillage,
},
select: {
id: true,
name: true,
},
});
return Response.json(group, { status: 201 });
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -0,0 +1,19 @@
import { prisma } from "@/module/_global";
export async function deleteGroup(req: Request) {
try {
const data = await req.json();
const update = await prisma.group.update({
where: {
id: data.id,
},
data: {
isActive: false,
},
});
return Response.json({ success: true, message: "Sukses Delete Grup" }, { status: 200 });
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error", success: false }, { status: 500 });
}
}

View File

@@ -0,0 +1,21 @@
import { prisma } from "@/module/_global";
export async function updateGroup(req: Request) {
try {
const data = await req.json();
const update = await prisma.group.update({
where: {
id: data.id,
},
data: {
name: data.name,
idVillage: data.idVillage,
},
});
return Response.json({ success: true, message: "Sukses Update Grup" }, { 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 { apiGroup } from "./api/api_group";
import ViewGroup from "./view/view_group";
export {ViewGroup}
export { ViewGroup };
export { apiGroup };

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 };

View File

@@ -0,0 +1,36 @@
import { getAllVillage } from "./get/getAllVillage";
import { getOneVillage } from "./get/getOneVillage";
import { createVillage } from "./post/createVillage";
import { deleteVillage } from "./post/deleteVillage";
import { updateVillage } from "./post/updateVillage";
export const API_INDEX_VILLAGE = [
{
path: "get-all-village",
method: "GET",
bin: getAllVillage,
},
{
path: "create-village",
method: "POST",
bin: createVillage,
},
{
path: "update-village",
method: "POST",
bin: updateVillage,
},
{
path: "delete-village",
method: "POST",
bin: deleteVillage,
},
{
path: "get-one-village",
method: "GET",
bin: getOneVillage,
},
];

View File

@@ -0,0 +1,15 @@
import { API_INDEX_VILLAGE } from "./api_index";
type Method = "GET" | "POST";
export async function apiViilage(req: Request, method: Method) {
const { searchParams } = new URL(req.url);
const path = searchParams.get("path");
const act = API_INDEX_VILLAGE.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,21 @@
import { prisma } from "@/module/_global";
export async function getAllVillage(req: Request) {
try {
const villages = await prisma.village.findMany({
where: {
isActive: true,
},
select: {
id: true,
name: true,
desc: true,
},
});
return Response.json(villages);
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -0,0 +1,32 @@
import { prisma } from "@/module/_global";
export async function getOneVillage(req: Request) {
try {
const village = "11";
const getOne = await prisma.village.findUnique({
where: {
id: village,
},
select: {
id: true,
name: true,
desc: true,
},
});
if (!getOne) {
return Response.json(
{ message: "Village tidak ditemukan", success: false },
{ status: 404 }
);
}
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 createVillage(req: Request) {
try {
const data = await req.json();
const village = await prisma.village.create({
data: {
name: data.name,
desc: data.desc,
isActive: true,
},
select: {
id: true,
name: true,
desc: true,
},
});
return Response.json(village, { status: 201 });
} catch (error) {
console.error(error);
return Response.json({ message: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -0,0 +1,25 @@
import { prisma } from "@/module/_global";
export async function deleteVillage(req: Request) {
try {
const data = await req.json();
const update = await prisma.village.update({
where: {
id: data.id,
},
data: {
isActive: false,
},
});
return Response.json(
{ success: true, message: "Sukses Delete Village" },
{ status: 200 }
);
} catch (error) {
console.error(error);
return Response.json(
{ message: "Internal Server Error", success: false },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,28 @@
import { prisma } from "@/module/_global";
export async function updateVillage(req: Request) {
try {
const data = await req.json();
const update = await prisma.village.update({
where: {
id: data.id,
},
data: {
name: data.name,
desc: data.desc,
},
});
return Response.json(
{ success: true, message: "Sukses Update Village" },
{ status: 200 }
);
} catch (error) {
console.error(error);
return Response.json(
{ message: "Internal Server Error", success: false },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,3 @@
import { apiViilage } from "./api/api_village";
export { apiViilage };

View File

@@ -2,5 +2,9 @@ import { Login } from '@/types/auth/login';
import { Verification } from './auth/varification';
export type ILogin = Login
export type ILogin = {
phone: string
id: string,
}
export type IVerification = Verification