diff --git a/pom.xml b/pom.xml index 2d8e109..1efeb99 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,15 @@ + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + org.springframework.boot spring-boot-starter-log4j2 diff --git a/postman/Complete_Employee_Management_System_API.postman_collection.json b/postman/Complete_Employee_Management_System_API.postman_collection.json new file mode 100644 index 0000000..27e1077 --- /dev/null +++ b/postman/Complete_Employee_Management_System_API.postman_collection.json @@ -0,0 +1,549 @@ +{ + "info": { + "name": "Employee Management System API", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Employee", + "item": [ + { + "name": "Create Employee", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/employees", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"name\": \"John Doe\",\n \"role\": \"Manager\"\n}" + } + } + }, + { + "name": "Get All Employees", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employees", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees" + ] + } + } + }, + { + "name": "Get Employee by ID", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employees/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees", + "1" + ] + } + } + }, + { + "name": "Update Employee", + "request": { + "method": "PUT", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/employees/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees", + "1" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"name\": \"Jane Doe\",\n \"role\": \"Director\"\n}" + } + } + }, + { + "name": "Delete Employee", + "request": { + "method": "DELETE", + "url": { + "raw": "http://localhost:8080/employees/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees", + "1" + ] + } + } + }, + { + "name": "Find Employees by City", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employees/by-city/Delhi", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees", + "by-city", + "Delhi" + ] + } + } + }, + { + "name": "Find Employees by Department Name", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employees/by-department/HR", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employees", + "by-department", + "HR" + ] + } + } + } + ] + }, + { + "name": "Address", + "item": [ + { + "name": "Create Address", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/addresses", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"street\": \"123 Main St\",\n \"city\": \"Delhi\",\n \"state\": \"Delhi\",\n \"employee\": {\n \"id\": 1\n }\n}" + } + } + }, + { + "name": "Get All Addresses", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/addresses", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses" + ] + } + } + }, + { + "name": "Get Address by ID", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/addresses/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses", + "1" + ] + } + } + }, + { + "name": "Update Address", + "request": { + "method": "PUT", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/addresses/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses", + "1" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"street\": \"456 Park Ave\",\n \"city\": \"Mumbai\",\n \"state\": \"Maharashtra\",\n \"employee\": {\n \"id\": 1\n }\n}" + } + } + }, + { + "name": "Delete Address", + "request": { + "method": "DELETE", + "url": { + "raw": "http://localhost:8080/addresses/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses", + "1" + ] + } + } + }, + { + "name": "Find Addresses by City", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/addresses/by-city/Delhi", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses", + "by-city", + "Delhi" + ] + } + } + }, + { + "name": "Find Addresses by Employee Name", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/addresses/by-employee-name/John Doe", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "addresses", + "by-employee-name", + "John Doe" + ] + } + } + } + ] + }, + { + "name": "Department", + "item": [ + { + "name": "Create Department", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/departments", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"name\": \"Engineering\",\n \"description\": \"Handles all technical aspects\"\n}" + } + } + }, + { + "name": "Get All Departments", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/departments", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments" + ] + } + } + }, + { + "name": "Get Department by ID", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/departments/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments", + "1" + ] + } + } + }, + { + "name": "Update Department", + "request": { + "method": "PUT", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/departments/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments", + "1" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"name\": \"Human Resources\",\n \"description\": \"Manages employee relations\"\n}" + } + } + }, + { + "name": "Delete Department", + "request": { + "method": "DELETE", + "url": { + "raw": "http://localhost:8080/departments/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments", + "1" + ] + } + } + }, + { + "name": "Find Departments by Name", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/departments/by-name/Engineering", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments", + "by-name", + "Engineering" + ] + } + } + }, + { + "name": "Find Departments by Employee Name", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/departments/by-employee-name/John Doe", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "departments", + "by-employee-name", + "John Doe" + ] + } + } + } + ] + }, + { + "name": "EmployeeDepartment", + "item": [ + { + "name": "Assign Employee to Department", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "url": { + "raw": "http://localhost:8080/employee-departments", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employee-departments" + ] + }, + "body": { + "mode": "raw", + "raw": "{\n \"employee\": {\n \"id\": 1\n },\n \"department\": {\n \"id\": 1\n },\n \"createdAt\": \"2024-01-01T10:00:00\",\n \"updatedAt\": \"2024-01-01T10:00:00\",\n \"isDeleted\": false\n}" + } + } + }, + { + "name": "Get All Active Employee-Department Relations", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employee-departments/active", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employee-departments", + "active" + ] + } + } + }, + { + "name": "Find Employees by Department Name", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employee-departments/by-department/HR", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employee-departments", + "by-department", + "HR" + ] + } + } + }, + { + "name": "Find Departments by Employee Name", + "request": { + "method": "GET", + "url": { + "raw": "http://localhost:8080/employee-departments/by-employee/John Doe", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employee-departments", + "by-employee", + "John Doe" + ] + } + } + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/java/com/app/controller/AddressController.java b/src/main/java/com/app/controller/AddressController.java new file mode 100644 index 0000000..f5e1806 --- /dev/null +++ b/src/main/java/com/app/controller/AddressController.java @@ -0,0 +1,63 @@ +package com.app.controller; + +import com.app.model.Address; +import com.app.service.AddressService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/addresses") +@RequiredArgsConstructor +public class AddressController { + + private final AddressService addressService; + + + // CRUD Operations + + @PostMapping + public Address createAddress(@RequestBody Address address) { + return addressService.createAddress(address); + } + + @GetMapping + public List
getAllAddresses() { + return addressService.getAllAddresses(); + } + + @GetMapping("/{id}") + public Optional
getAddressById(@PathVariable Long id) { + return addressService.getAddressById(id); + } + + @PutMapping("/{id}") + public Address updateAddress(@PathVariable Long id, @RequestBody Address addressDetails) { + return addressService.updateAddress(id, addressDetails); + } + + @DeleteMapping("/{id}") + public void deleteAddress(@PathVariable Long id) { + addressService.deleteAddress(id); + } + + // FindBy Operations + + @GetMapping("/by-city/{city}") + public List
getAddressesByCity(@PathVariable String city) { + return addressService.findByCity(city); + } + + @GetMapping("/by-state/{state}") + public List
getAddressesByState(@PathVariable String state) { + return addressService.findByState(state); + } + + @GetMapping("/by-employee-name/{employeeName}") + public List
getAddressesByEmployeeName(@PathVariable String employeeName) { + return addressService.findByEmployeeName(employeeName); + } +} + diff --git a/src/main/java/com/app/controller/DepartmentController.java b/src/main/java/com/app/controller/DepartmentController.java new file mode 100644 index 0000000..af7ac3b --- /dev/null +++ b/src/main/java/com/app/controller/DepartmentController.java @@ -0,0 +1,55 @@ +package com.app.controller; + +import com.app.model.Department; +import com.app.service.DepartmentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/departments") +@RequiredArgsConstructor +public class DepartmentController { + + private final DepartmentService departmentService; + + + @PostMapping + public Department createDepartment(@RequestBody Department department) { + return departmentService.createDepartment(department); + } + + @GetMapping + public List getAllDepartments() { + return departmentService.getAllDepartments(); + } + + @GetMapping("/{id}") + public Optional getDepartmentById(@PathVariable Long id) { + return departmentService.getDepartmentById(id); + } + + @PutMapping("/{id}") + public Department updateDepartment(@PathVariable Long id, @RequestBody Department departmentDetails) { + return departmentService.updateDepartment(id, departmentDetails); + } + + @DeleteMapping("/{id}") + public void deleteDepartment(@PathVariable Long id) { + departmentService.deleteDepartment(id); + } + + // FindBy Operations + + @GetMapping("/by-name/{name}") + public List getDepartmentsByName(@PathVariable String name) { + return departmentService.findByName(name); + } + + @GetMapping("/by-employee-name/{employeeName}") + public List getDepartmentsByEmployeeName(@PathVariable String employeeName) { + return departmentService.findByEmployeeName(employeeName); + } +} diff --git a/src/main/java/com/app/controller/EmployeeController.java b/src/main/java/com/app/controller/EmployeeController.java new file mode 100644 index 0000000..094efaa --- /dev/null +++ b/src/main/java/com/app/controller/EmployeeController.java @@ -0,0 +1,54 @@ +package com.app.controller; + +import com.app.model.Employee; +import com.app.service.EmployeeService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequestMapping("/employees") +@RequiredArgsConstructor +public class EmployeeController { + + private final EmployeeService employeeService; + + // CRUD Operations + @PostMapping + public Employee createEmployee(@RequestBody Employee employee) { + return employeeService.createEmployee(employee); + } + + @GetMapping + public List getAllEmployees() { + return employeeService.getAllEmployees(); + } + + @GetMapping("/{id}") + public Optional getEmployeeById(@PathVariable Long id) { + return employeeService.getEmployeeById(id); + } + + @PutMapping("/{id}") + public Employee updateEmployee(@PathVariable Long id, @RequestBody Employee employeeDetails) { + return employeeService.updateEmployee(id, employeeDetails); + } + + @DeleteMapping("/{id}") + public void deleteEmployee(@PathVariable Long id) { + employeeService.deleteEmployee(id); + } + + // FindBy Operations + @GetMapping("/by-city/{city}") + public List getEmployeesByCity(@PathVariable String city) { + return employeeService.findByCity(city); + } + + @GetMapping("/by-department/{departmentName}") + public List getEmployeesByDepartmentName(@PathVariable String departmentName) { + return employeeService.findByDepartmentName(departmentName); + } +} diff --git a/src/main/java/com/app/controller/EmployeeDepartmentController.java b/src/main/java/com/app/controller/EmployeeDepartmentController.java new file mode 100644 index 0000000..eb64bd3 --- /dev/null +++ b/src/main/java/com/app/controller/EmployeeDepartmentController.java @@ -0,0 +1,34 @@ +package com.app.controller; + +import com.app.model.EmployeeDepartment; +import com.app.service.EmployeeDepartmentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/employee-departments") +@RequiredArgsConstructor +public class EmployeeDepartmentController { + + private final EmployeeDepartmentService employeeDepartmentService; + + @GetMapping("/active") + public List getAllActiveRelations() { + return employeeDepartmentService.findAllActiveRelations(); + } + + @GetMapping("/by-department/{departmentName}") + public List getEmployeesByDepartment(@PathVariable String departmentName) { + return employeeDepartmentService.findEmployeesByDepartmentName(departmentName); + } + + @GetMapping("/by-employee/{employeeName}") + public List getDepartmentsByEmployee(@PathVariable String employeeName) { + return employeeDepartmentService.findDepartmentsByEmployeeName(employeeName); + } +} diff --git a/src/main/java/com/app/model/Address.java b/src/main/java/com/app/model/Address.java new file mode 100644 index 0000000..da278b7 --- /dev/null +++ b/src/main/java/com/app/model/Address.java @@ -0,0 +1,26 @@ +package com.app.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.persistence.*; +import lombok.Data; + +@Entity +@Data +public class Address { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String street; + private String city; + private String state; + + @ManyToOne + @JoinColumn(name = "employee_id") + @JsonIgnore + private Employee employee; + + // Getters and Setters +} + diff --git a/src/main/java/com/app/model/Department.java b/src/main/java/com/app/model/Department.java new file mode 100644 index 0000000..a1df32b --- /dev/null +++ b/src/main/java/com/app/model/Department.java @@ -0,0 +1,23 @@ +package com.app.model; + +import jakarta.persistence.*; +import lombok.Data; + +import java.util.Set; + +@Entity +@Data +public class Department { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private String description; + + @OneToMany(mappedBy = "department", cascade = CascadeType.ALL) + private Set employeeDepartments; + + // Constructors, Getters, and Setters +} diff --git a/src/main/java/com/app/model/Employee.java b/src/main/java/com/app/model/Employee.java new file mode 100644 index 0000000..8c0071e --- /dev/null +++ b/src/main/java/com/app/model/Employee.java @@ -0,0 +1,27 @@ +package com.app.model; + +import jakarta.persistence.*; +import lombok.Data; + +import java.util.List; +import java.util.Set; + +@Entity +@Data +public class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + private String role; + + @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true) + private List
addresses; + + @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL) + private Set employeeDepartments; + + // Constructors, Getters, and Setters +} diff --git a/src/main/java/com/app/model/EmployeeDepartment.java b/src/main/java/com/app/model/EmployeeDepartment.java new file mode 100644 index 0000000..81c03a2 --- /dev/null +++ b/src/main/java/com/app/model/EmployeeDepartment.java @@ -0,0 +1,33 @@ +package com.app.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.persistence.*; +import lombok.Data; + +import java.time.LocalDateTime; + +@Entity +@Table(name = "employee_department") +@Data +public class EmployeeDepartment { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "employee_id", nullable = false) + @JsonIgnore + private Employee employee; + + @ManyToOne + @JoinColumn(name = "department_id", nullable = false) + @JsonIgnore + private Department department; + + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + private boolean isDeleted; + + // Getters and Setters +} diff --git a/src/main/java/com/app/repository/AddressRepository.java b/src/main/java/com/app/repository/AddressRepository.java new file mode 100644 index 0000000..81d8006 --- /dev/null +++ b/src/main/java/com/app/repository/AddressRepository.java @@ -0,0 +1,18 @@ +package com.app.repository; + +import com.app.model.Address; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface AddressRepository extends JpaRepository { + + // Find addresses by city + List
findByCity(String city); + + // Find addresses by state + List
findByState(String state); + + // Find addresses by employee name + List
findByEmployee_Name(String employeeName); +} diff --git a/src/main/java/com/app/repository/DepartmentRepository.java b/src/main/java/com/app/repository/DepartmentRepository.java new file mode 100644 index 0000000..65925ae --- /dev/null +++ b/src/main/java/com/app/repository/DepartmentRepository.java @@ -0,0 +1,18 @@ +package com.app.repository; + +import com.app.model.Department; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface DepartmentRepository extends JpaRepository { + + // Find department by name + List findByName(String name); + + // Find department by description + List findByDescription(String description); + + // Find departments by employee name + List findByEmployeeDepartments_Employee_Name(String employeeName); +} diff --git a/src/main/java/com/app/repository/EmployeeDepartmentRepository.java b/src/main/java/com/app/repository/EmployeeDepartmentRepository.java new file mode 100644 index 0000000..7f980db --- /dev/null +++ b/src/main/java/com/app/repository/EmployeeDepartmentRepository.java @@ -0,0 +1,18 @@ +package com.app.repository; + +import com.app.model.EmployeeDepartment; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface EmployeeDepartmentRepository extends JpaRepository { + + // Find active employee-department relations + List findByIsDeletedFalse(); + + // Find employees by department name (ignoring deleted) + List findByDepartment_NameAndIsDeletedFalse(String departmentName); + + // Find departments by employee name (ignoring deleted) + List findByEmployee_NameAndIsDeletedFalse(String employeeName); +} diff --git a/src/main/java/com/app/repository/EmployeeRepository.java b/src/main/java/com/app/repository/EmployeeRepository.java new file mode 100644 index 0000000..22cd04a --- /dev/null +++ b/src/main/java/com/app/repository/EmployeeRepository.java @@ -0,0 +1,21 @@ +package com.app.repository; + +import com.app.model.Employee; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface EmployeeRepository extends JpaRepository { + + // Find by name + List findByName(String name); + + // Find by role + List findByRole(String role); + + // Find employees by address city + List findByAddresses_City(String city); + + // Find employees by department name (through EmployeeDepartment) + List findByEmployeeDepartments_Department_Name(String departmentName); +} diff --git a/src/main/java/com/app/service/AddressService.java b/src/main/java/com/app/service/AddressService.java new file mode 100644 index 0000000..80d8fd0 --- /dev/null +++ b/src/main/java/com/app/service/AddressService.java @@ -0,0 +1,60 @@ +package com.app.service; + +import com.app.model.Address; +import com.app.repository.AddressRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class AddressService { + + private final AddressRepository addressRepository; + + // CRUD Operations + + public Address createAddress(Address address) { + return addressRepository.save(address); + } + + public List
getAllAddresses() { + return addressRepository.findAll(); + } + + public Optional
getAddressById(Long id) { + return addressRepository.findById(id); + } + + public Address updateAddress(Long id, Address addressDetails) { + Address address = addressRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Address not found with id: " + id)); + + address.setStreet(addressDetails.getStreet()); + address.setCity(addressDetails.getCity()); + address.setState(addressDetails.getState()); + address.setEmployee(addressDetails.getEmployee()); + + return addressRepository.save(address); + } + + public void deleteAddress(Long id) { + addressRepository.deleteById(id); + } + + // FindBy Operations + + public List
findByCity(String city) { + return addressRepository.findByCity(city); + } + + public List
findByState(String state) { + return addressRepository.findByState(state); + } + + public List
findByEmployeeName(String employeeName) { + return addressRepository.findByEmployee_Name(employeeName); + } +} diff --git a/src/main/java/com/app/service/DepartmentService.java b/src/main/java/com/app/service/DepartmentService.java new file mode 100644 index 0000000..4e3dd86 --- /dev/null +++ b/src/main/java/com/app/service/DepartmentService.java @@ -0,0 +1,52 @@ +package com.app.service; + +import com.app.model.Department; +import com.app.repository.DepartmentRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class DepartmentService { + + private final DepartmentRepository departmentRepository; + + // CRUD Operations + public Department createDepartment(Department department) { + return departmentRepository.save(department); + } + + public List getAllDepartments() { + return departmentRepository.findAll(); + } + + public Optional getDepartmentById(Long id) { + return departmentRepository.findById(id); + } + + public Department updateDepartment(Long id, Department departmentDetails) { + Department department = departmentRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Department not found")); + + department.setName(departmentDetails.getName()); + department.setDescription(departmentDetails.getDescription()); + + return departmentRepository.save(department); + } + + public void deleteDepartment(Long id) { + departmentRepository.deleteById(id); + } + + // FindBy Methods + public List findByName(String name) { + return departmentRepository.findByName(name); + } + + public List findByEmployeeName(String employeeName) { + return departmentRepository.findByEmployeeDepartments_Employee_Name(employeeName); + } +} \ No newline at end of file diff --git a/src/main/java/com/app/service/EmployeeDepartmentService.java b/src/main/java/com/app/service/EmployeeDepartmentService.java new file mode 100644 index 0000000..e3b81e5 --- /dev/null +++ b/src/main/java/com/app/service/EmployeeDepartmentService.java @@ -0,0 +1,30 @@ +package com.app.service; + +import com.app.model.EmployeeDepartment; +import com.app.repository.EmployeeDepartmentRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class EmployeeDepartmentService { + private final EmployeeDepartmentRepository employeeDepartmentRepository; + + // Find active employee-department relations + public List findAllActiveRelations() { + return employeeDepartmentRepository.findByIsDeletedFalse(); + } + + // Find employees by department name + public List findEmployeesByDepartmentName(String departmentName) { + return employeeDepartmentRepository.findByDepartment_NameAndIsDeletedFalse(departmentName); + } + + // Find departments by employee name + public List findDepartmentsByEmployeeName(String employeeName) { + return employeeDepartmentRepository.findByEmployee_NameAndIsDeletedFalse(employeeName); + } + +} diff --git a/src/main/java/com/app/service/EmployeeService.java b/src/main/java/com/app/service/EmployeeService.java new file mode 100644 index 0000000..9de6f14 --- /dev/null +++ b/src/main/java/com/app/service/EmployeeService.java @@ -0,0 +1,50 @@ +package com.app.service; + +import com.app.model.Employee; +import com.app.repository.EmployeeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class EmployeeService { + + private final EmployeeRepository employeeRepository; + + + // CRUD Operations + public Employee createEmployee(Employee employee) { + return employeeRepository.save(employee); + } + + public List getAllEmployees() { + return employeeRepository.findAll(); + } + + public Optional getEmployeeById(Long id) { + return employeeRepository.findById(id); + } + + public Employee updateEmployee(Long id, Employee employeeDetails) { + Employee employee = employeeRepository.findById(id).orElseThrow(() -> new RuntimeException("Employee not found")); + employee.setName(employeeDetails.getName()); + employee.setRole(employeeDetails.getRole()); + return employeeRepository.save(employee); + } + + public void deleteEmployee(Long id) { + employeeRepository.deleteById(id); + } + + // FindBy Methods + public List findByCity(String city) { + return employeeRepository.findByAddresses_City(city); + } + + public List findByDepartmentName(String departmentName) { + return employeeRepository.findByEmployeeDepartments_Department_Name(departmentName); + } +} \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index d2f8b2d..b4ba8cf 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -2,3 +2,19 @@ app: logs: path: C:/${spring.application.name}/logs +spring: + datasource: + url: jdbc:h2:mem:testdb + driverClassName: org.h2.Driver + username: sa + password: + + jpa: + database-platform: org.hibernate.dialect.H2Dialect + show-sql: true + hibernate: + ddl-auto: update + + h2: + console: + enabled: true