Ignore:
Timestamp:
01/03/24 00:00:07 (10 months ago)
Author:
gjoko kostadinov <gjokokostadinov@…>
Branches:
master
Children:
53765dd
Parents:
77205be
Message:

Add all bug fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/edu/gjoko/schedlr/services/BusinessService.java

    r77205be r1413ee2  
    11package edu.gjoko.schedlr.services;
    22
    3 import edu.gjoko.schedlr.entity.*;
    4 import edu.gjoko.schedlr.repositories.*;
     3import edu.gjoko.schedlr.entity.Business;
     4import edu.gjoko.schedlr.entity.Stakeholder;
     5import edu.gjoko.schedlr.entity.StakeholderType;
     6import edu.gjoko.schedlr.repositories.BusinessRepository;
    57import lombok.AllArgsConstructor;
    68import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    79import org.springframework.stereotype.Service;
    8 import org.springframework.util.CollectionUtils;
    910
    1011import java.util.List;
     
    1819
    1920    private final BusinessRepository businessRepository;
    20     private final ServiceTypeRepository serviceTypeRepository;
    21     private final ServiceRepository serviceRepository;
     21    private final ServicesService servicesService;
    2222    private final StakeholderService stakeholderService;
    23 
    24     private final StakeholderRepository stakeholderRepository;
    2523    private BCryptPasswordEncoder bCryptPasswordEncoder;
    2624
    2725    public void saveBusiness(Business business) {
    28         saveNewServiceTypes(business);
     26        servicesService.saveNewServiceTypes(business);
    2927        business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
    3028        business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
     
    3331    }
    3432
    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 
    4733    public List<Business> findAll() {
    4834        return businessRepository.findAll();
    4935    }
    5036
    51     public void updateBusinesses(List<Business> businessList) {
     37    public void updateBusinesses(List<Business> businessList, Long businessOwnerId) {
    5238        businessList
    5339                .stream()
    5440                .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);
    5852                });
    5953    }
     
    6862        return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId);
    6963    }
    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     }
    9264}
Note: See TracChangeset for help on using the changeset viewer.