92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
import axiosInstance from "../api/axiosConfig";
|
|
|
|
// Get all employees
|
|
export const getAllEmployees = async () => {
|
|
try {
|
|
const response = await axiosInstance.get("/employees");
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error; // The error is handled globally by the axiosInstance
|
|
}
|
|
};
|
|
|
|
// Create a new employee
|
|
export const createEmployee = async (employeePayload) => {
|
|
try {
|
|
const response = await axiosInstance.post("/employees", employeePayload);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error; // The error is handled globally by the axiosInstance
|
|
}
|
|
};
|
|
|
|
// Get an employee by ID
|
|
export const getEmployeeById = async (id) => {
|
|
try {
|
|
const response = await axiosInstance.get(`/employees/${id}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error; // The error is handled globally by the axiosInstance
|
|
}
|
|
};
|
|
|
|
// Update an employee's details
|
|
export const updateEmployee = async (id, employeePayload) => {
|
|
try {
|
|
const response = await axiosInstance.patch(
|
|
`/employees/${id}`,
|
|
employeePayload
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error; // The error is handled globally by the axiosInstance
|
|
}
|
|
};
|
|
|
|
// Delete an employee
|
|
export const deleteEmployee = async (id) => {
|
|
try {
|
|
await axiosInstance.delete(`/employees/${id}`);
|
|
return; // No content to return on successful delete
|
|
} catch (error) {
|
|
throw error; // The error is handled globally by the axiosInstance
|
|
}
|
|
};
|
|
|
|
// Example function to use the employee service
|
|
export const exampleEmployeeUsage = async () => {
|
|
try {
|
|
// Example: Get all employees
|
|
const employees = await getAllEmployees();
|
|
console.log("Employees:", employees);
|
|
|
|
// Example: Create a new employee
|
|
const newEmployee = {
|
|
name: "John Doe",
|
|
email: "john.doe@example.com",
|
|
position: "Software Engineer",
|
|
};
|
|
const createdEmployee = await createEmployee(newEmployee);
|
|
console.log("Created Employee:", createdEmployee);
|
|
|
|
// Example: Get an employee by ID
|
|
const employeeId = "123";
|
|
const employee = await getEmployeeById(employeeId);
|
|
console.log("Employee by ID:", employee);
|
|
|
|
// Example: Update an employee
|
|
const updatedEmployeePayload = { position: "Senior Software Engineer" };
|
|
const updatedEmployee = await updateEmployee(
|
|
employeeId,
|
|
updatedEmployeePayload
|
|
);
|
|
console.log("Updated Employee:", updatedEmployee);
|
|
|
|
// Example: Delete an employee
|
|
await deleteEmployee(employeeId);
|
|
console.log("Employee deleted successfully");
|
|
} catch (error) {
|
|
console.error("Error occurred while using employees service:", error);
|
|
}
|
|
};
|