stefan1anuby / Employee-Management-And-Payroll-System

0 stars 0 forks source link

Create Business Model and Repository #19

Open stefan1anuby opened 2 weeks ago

stefan1anuby commented 1 week ago

this should look like this:

@Entity
public class Business {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String address;
    private String industry;

    // One-to-Many relationship with Employee
    @OneToMany(mappedBy = "business", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Employee> employees;

    // Constructors, Getters, and Setters
}

the abstract repository:

@Repository
public interface BusinessRepository extends JpaRepository<Business, Long> {
}

and the service:

@Service
public class BusinessService {

    @Autowired
    private BusinessRepository businessRepository;

    // Create a new business
    public Business createBusiness(Business business) {
        return businessRepository.save(business);
    }

    // Get all employees in a business
    public List<Employee> getEmployeesByBusiness(Long businessId) {
        Business business = businessRepository.findById(businessId)
                .orElseThrow(() -> new EntityNotFoundException("Business not found"));
        return business.getEmployees();
    }

    // Other business-related methods
}