stefan1anuby / Employee-Management-And-Payroll-System

0 stars 0 forks source link

Create Department model, repo and service #39

Closed stefan1anuby closed 1 week ago

stefan1anuby commented 1 week ago

It could look something like this:

@Entity
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String location;

    @OneToMany(mappedBy = "department")
    private List<Employee> employees;

    // Getters and Setters
}

Abstract repo:

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
}

Service:

@Service
public class DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    public List<Department> getAllDepartments() {
        return departmentRepository.findAll();
    }

    public Department getDepartmentById(Long id) {
        return departmentRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Department not found"));
    }

    public Department saveDepartment(Department department) {
        return departmentRepository.save(department);
    }
}