[46fd0c7] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
[950fa0d] | 3 | import edu.gjoko.schedlr.entity.*;
|
---|
[46fd0c7] | 4 | import edu.gjoko.schedlr.repositories.BusinessRepository;
|
---|
[950fa0d] | 5 | import edu.gjoko.schedlr.repositories.ServiceRepository;
|
---|
[46fd0c7] | 6 | import edu.gjoko.schedlr.repositories.ServiceTypeRepository;
|
---|
[950fa0d] | 7 | import edu.gjoko.schedlr.repositories.StakeholderRepository;
|
---|
[46fd0c7] | 8 | import lombok.AllArgsConstructor;
|
---|
[8bcd64c] | 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[46fd0c7] | 10 | import org.springframework.stereotype.Service;
|
---|
| 11 |
|
---|
| 12 | import java.util.List;
|
---|
| 13 |
|
---|
[950fa0d] | 14 | import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
|
---|
[46fd0c7] | 15 | import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
|
---|
| 16 |
|
---|
| 17 | @Service
|
---|
| 18 | @AllArgsConstructor
|
---|
| 19 | public class BusinessService {
|
---|
| 20 |
|
---|
| 21 | private final BusinessRepository businessRepository;
|
---|
| 22 | private final ServiceTypeRepository serviceTypeRepository;
|
---|
[950fa0d] | 23 | private final ServiceRepository serviceRepository;
|
---|
| 24 |
|
---|
| 25 | private final StakeholderRepository stakeholderRepository;
|
---|
[8bcd64c] | 26 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
[46fd0c7] | 27 |
|
---|
| 28 | public void saveBusiness(Business business) {
|
---|
| 29 | saveNewServiceTypes(business);
|
---|
| 30 | business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
|
---|
[8bcd64c] | 31 | business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
|
---|
[46fd0c7] | 32 | business.setBusinessStatus(NEW);
|
---|
| 33 | businessRepository.save(business);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public void saveNewServiceTypes(Business business) {
|
---|
| 37 | business.getServices()
|
---|
[9050790] | 38 | .stream()
|
---|
| 39 | .forEach(service -> {
|
---|
| 40 | if (service.getServiceType().getId() == null) {
|
---|
| 41 | ServiceType serviceType = service.getServiceType();
|
---|
| 42 | serviceType.setBusinessType(business.getBusinessType());
|
---|
| 43 | service.setServiceType(serviceTypeRepository.save(serviceType));
|
---|
| 44 | }
|
---|
| 45 | });
|
---|
[46fd0c7] | 46 | }
|
---|
| 47 |
|
---|
[9050790] | 48 | public List<Business> findAll() {
|
---|
| 49 | return businessRepository.findAll();
|
---|
[46fd0c7] | 50 | }
|
---|
| 51 |
|
---|
[9050790] | 52 | public void updateBusinesses(List<Business> businessList) {
|
---|
[46fd0c7] | 53 | businessList
|
---|
| 54 | .stream()
|
---|
| 55 | .forEach(business -> {
|
---|
[950fa0d] | 56 | stakeholderRepository.save(business.getOwner());
|
---|
| 57 | serviceRepository.saveAll(business.getServices());
|
---|
[46fd0c7] | 58 | businessRepository.save(business);
|
---|
| 59 | });
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[8bcd64c] | 62 | public Business findByOwner(Long ownerId) {
|
---|
| 63 | var owner = new Stakeholder();
|
---|
| 64 | owner.setId(ownerId);
|
---|
| 65 | return businessRepository.findBusinessByOwner(owner);
|
---|
| 66 | }
|
---|
[950fa0d] | 67 |
|
---|
| 68 | public List<Business> findByBusinessTypeAndActiveStatus(BusinessType businessType) {
|
---|
| 69 | return businessRepository.findBusinessesByBusinessTypeAndBusinessStatus(businessType, ACTIVE);
|
---|
| 70 | }
|
---|
[46fd0c7] | 71 | }
|
---|