- Timestamp:
- 01/03/24 00:00:07 (10 months ago)
- Branches:
- master
- Children:
- 53765dd
- Parents:
- 77205be
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/edu/gjoko/schedlr/services/BusinessService.java
r77205be r1413ee2 1 1 package edu.gjoko.schedlr.services; 2 2 3 import edu.gjoko.schedlr.entity.*; 4 import edu.gjoko.schedlr.repositories.*; 3 import edu.gjoko.schedlr.entity.Business; 4 import edu.gjoko.schedlr.entity.Stakeholder; 5 import edu.gjoko.schedlr.entity.StakeholderType; 6 import edu.gjoko.schedlr.repositories.BusinessRepository; 5 7 import lombok.AllArgsConstructor; 6 8 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 9 import org.springframework.stereotype.Service; 8 import org.springframework.util.CollectionUtils;9 10 10 11 import java.util.List; … … 18 19 19 20 private final BusinessRepository businessRepository; 20 private final ServiceTypeRepository serviceTypeRepository; 21 private final ServiceRepository serviceRepository; 21 private final ServicesService servicesService; 22 22 private final StakeholderService stakeholderService; 23 24 private final StakeholderRepository stakeholderRepository;25 23 private BCryptPasswordEncoder bCryptPasswordEncoder; 26 24 27 25 public void saveBusiness(Business business) { 28 s aveNewServiceTypes(business);26 servicesService.saveNewServiceTypes(business); 29 27 business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER); 30 28 business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword())); … … 33 31 } 34 32 35 public void saveNewServiceTypes(Business business) {36 business.getServices()37 .stream()38 .forEach(service -> {39 if (service.getServiceType().getId() == null) {40 ServiceType serviceType = service.getServiceType();41 serviceType.setBusinessType(business.getBusinessType());42 service.setServiceType(serviceTypeRepository.save(serviceType));43 }44 });45 }46 47 33 public List<Business> findAll() { 48 34 return businessRepository.findAll(); 49 35 } 50 36 51 public void updateBusinesses(List<Business> businessList ) {37 public void updateBusinesses(List<Business> businessList, Long businessOwnerId) { 52 38 businessList 53 39 .stream() 54 40 .forEach(business -> { 55 stakeholderService.saveOrUpdateStakeholder(business.getOwner()); 56 saveOrUpdateServices(business.getServices()); 57 saveOrUpdateBusiness(business); 41 stakeholderService.saveOrUpdateStakeholder(business.getOwner(), businessOwnerId); 42 }); 43 } 44 45 public void updateBusinessStatuses(List<Business> businessList) { 46 businessList 47 .stream() 48 .forEach(business -> { 49 Business business1 = businessRepository.findById(business.getId()).get(); 50 business1.setBusinessStatus(business.getBusinessStatus()); 51 businessRepository.save(business1); 58 52 }); 59 53 } … … 68 62 return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId); 69 63 } 70 71 72 73 private void saveOrUpdateBusiness(Business business) {74 if (business.getId() != null) {75 var foundBusinessEntity = businessRepository.findById(business.getId());76 business.setCreated(foundBusinessEntity.get().getCreated());77 }78 businessRepository.save(business);79 }80 81 private void saveOrUpdateServices(List<edu.gjoko.schedlr.entity.Service> serviceList) {82 if (!CollectionUtils.isEmpty(serviceList)) {83 serviceList.forEach( service -> {84 if (service.getId() != null) {85 var found = serviceRepository.findById(service.getId());86 service.setCreated(found.get().getCreated());87 }88 });89 serviceRepository.saveAll(serviceList);90 }91 }92 64 }
Note:
See TracChangeset
for help on using the changeset viewer.