opengis/frontend/src/api/employee.js
Flook 1f1ed79571
All checks were successful
continuous-integration/drone/push Build is passing
ปรับปรุง Employee Service และ README
2025-04-16 05:24:42 +07:00

106 lines
2.5 KiB
JavaScript

const BASE_URL = "http://localhost:8080/api/employees"; // /api/employees กรณี docker compose ทั้งหมด
export const getEmployees = async ()=>{
try{
const response = await fetch(`${BASE_URL}`);
if(!response.ok){
throw new Error("Error while fetching all employees");
}
const data = await response.json()
return data;
}
catch (e) {
console.log("Error while fetching all employees");
return null;
}
//return employees;
}
export const addEmployee = async (employee) =>{
try {
const response = await fetch(`${BASE_URL}`,{
method:"POST",
headers:{
"Content-Type":"application/json"
},
body: JSON.stringify(employee)
});
if(!response.ok){
throw new Error("Fail to create employee")
}
const data = await response.json();
return data;
}
catch (e) {
console.log("Fail to create employee");
return null;
}
}
export const updateEmployee = async (id, employee) => {
try {
const response = await fetch(`${BASE_URL}/${id}`, {
method: "PUT",
headers: {
"Content-Type":"application/json"
},
body: JSON.stringify(employee)
});
if(!response.ok){
throw new Error("Fail to update employee")
}
const data = await response.json();
return data;
}
catch (e) {
console.log("Fail to update employee");
return null;
}
}
export const deleteEmployee = async (id) => {
try {
const response = await fetch(`${BASE_URL}/${id}`,{
method : "DELETE",
headers : {
"Content-Type":"application/json"
},
});
if(!response.ok){
throw new Error("Fail to delete employee");
}
return true;
}
catch (e) {
console.log("Fail to delete employee");
return false;
}
}
export const searchEmployees = async (keyword) => {
try{
const response = await fetch(`${BASE_URL}/search?keyword=${keyword}`,{
method : "GET",
headers: {
"Content-type":"application/json"
},
});
if(!response.ok){
throw new Error("Fail to search employees");
}
const data = await response.json();
return data;
}
catch (e) {
console.log("Fail to search employees");
return null;
}
}