[46fd0c7] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
[1413ee2] | 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;
|
---|
[46fd0c7] | 7 | import lombok.AllArgsConstructor;
|
---|
[8bcd64c] | 8 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[46fd0c7] | 9 | import org.springframework.stereotype.Service;
|
---|
| 10 |
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
[950fa0d] | 13 | import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
|
---|
[46fd0c7] | 14 | import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
|
---|
| 15 |
|
---|
| 16 | @Service
|
---|
| 17 | @AllArgsConstructor
|
---|
| 18 | public class BusinessService {
|
---|
| 19 |
|
---|
| 20 | private final BusinessRepository businessRepository;
|
---|
[1413ee2] | 21 | private final ServicesService servicesService;
|
---|
[77205be] | 22 | private final StakeholderService stakeholderService;
|
---|
[8bcd64c] | 23 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
[46fd0c7] | 24 |
|
---|
| 25 | public void saveBusiness(Business business) {
|
---|
[1413ee2] | 26 | servicesService.saveNewServiceTypes(business);
|
---|
[46fd0c7] | 27 | business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
|
---|
[8bcd64c] | 28 | business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
|
---|
[46fd0c7] | 29 | business.setBusinessStatus(NEW);
|
---|
| 30 | businessRepository.save(business);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[9050790] | 33 | public List<Business> findAll() {
|
---|
| 34 | return businessRepository.findAll();
|
---|
[46fd0c7] | 35 | }
|
---|
| 36 |
|
---|
[1413ee2] | 37 | public void updateBusinesses(List<Business> businessList, Long businessOwnerId) {
|
---|
| 38 | businessList
|
---|
| 39 | .stream()
|
---|
| 40 | .forEach(business -> {
|
---|
| 41 | stakeholderService.saveOrUpdateStakeholder(business.getOwner(), businessOwnerId);
|
---|
| 42 | });
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public void updateBusinessStatuses(List<Business> businessList) {
|
---|
[46fd0c7] | 46 | businessList
|
---|
| 47 | .stream()
|
---|
| 48 | .forEach(business -> {
|
---|
[1413ee2] | 49 | Business business1 = businessRepository.findById(business.getId()).get();
|
---|
| 50 | business1.setBusinessStatus(business.getBusinessStatus());
|
---|
| 51 | businessRepository.save(business1);
|
---|
[46fd0c7] | 52 | });
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[8bcd64c] | 55 | public Business findByOwner(Long ownerId) {
|
---|
| 56 | var owner = new Stakeholder();
|
---|
| 57 | owner.setId(ownerId);
|
---|
| 58 | return businessRepository.findBusinessByOwner(owner);
|
---|
| 59 | }
|
---|
[950fa0d] | 60 |
|
---|
[77205be] | 61 | public List<Business> findByBusinessTypeAndActiveStatus(Long businessTypeId) {
|
---|
| 62 | return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId);
|
---|
| 63 | }
|
---|
[46fd0c7] | 64 | }
|
---|