Changeset 1413ee2 for src/main/java/edu/gjoko/schedlr/services
- Timestamp:
- 01/03/24 00:00:07 (11 months ago)
- Branches:
- master
- Children:
- 53765dd
- Parents:
- 77205be
- Location:
- src/main/java/edu/gjoko/schedlr/services
- Files:
-
- 1 added
- 2 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 } -
src/main/java/edu/gjoko/schedlr/services/StakeholderService.java
r77205be r1413ee2 4 4 import edu.gjoko.schedlr.entity.StakeholderType; 5 5 import edu.gjoko.schedlr.repositories.StakeholderRepository; 6 import lombok.RequiredArgsConstructor; 6 7 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 8 import org.springframework.stereotype.Service; 8 9 9 import java.util.Optional;10 11 10 @Service 11 @RequiredArgsConstructor 12 12 public class StakeholderService { 13 13 14 private StakeholderRepository stakeholderRepository;15 private BCryptPasswordEncoder bCryptPasswordEncoder;14 private final StakeholderRepository stakeholderRepository; 15 private final BCryptPasswordEncoder bCryptPasswordEncoder; 16 16 17 public StakeholderService(StakeholderRepository stakeholderRepository, 18 BCryptPasswordEncoder bCryptPasswordEncoder) { 19 this.stakeholderRepository = stakeholderRepository; 20 this.bCryptPasswordEncoder = bCryptPasswordEncoder; 21 } 22 23 public Stakeholder saveStakeholder(Stakeholder stakeholder) { 17 public void saveStakeholder(Stakeholder stakeholder) { 24 18 stakeholder.setPassword(bCryptPasswordEncoder.encode(stakeholder.getPassword())); 25 19 stakeholder.setStakeholderType(StakeholderType.CUSTOMER); 26 returnstakeholderRepository.save(stakeholder);20 stakeholderRepository.save(stakeholder); 27 21 } 28 22 … … 31 25 } 32 26 33 public void saveOrUpdateStakeholder(Stakeholder stakeholder) { 34 if (stakeholder.getId() != null) { 35 var found = stakeholderRepository.findById(stakeholder.getId()).get(); 36 found.setFirstName(stakeholder.getFirstName()); 37 found.setLastName(stakeholder.getLastName()); 38 found.setPhoneNumber(stakeholder.getPhoneNumber()); 39 found.setEmail(stakeholder.getEmail()); 40 found.setUsername(stakeholder.getUsername()); 41 stakeholderRepository.save(found); 42 } 27 public void saveOrUpdateStakeholder(Stakeholder stakeholder, Long businessOwnerId) { 28 var found = stakeholderRepository.findById(businessOwnerId).get(); 29 found.setFirstName(stakeholder.getFirstName()); 30 found.setLastName(stakeholder.getLastName()); 31 found.setPhoneNumber(stakeholder.getPhoneNumber()); 32 found.setEmail(stakeholder.getEmail()); 33 found.setUsername(stakeholder.getUsername()); 34 stakeholderRepository.save(found); 43 35 } 44 36 }
Note:
See TracChangeset
for help on using the changeset viewer.