[46fd0c7] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
| 3 | import edu.gjoko.schedlr.entity.Business;
|
---|
| 4 | import edu.gjoko.schedlr.entity.ServiceType;
|
---|
[8bcd64c] | 5 | import edu.gjoko.schedlr.entity.Stakeholder;
|
---|
[46fd0c7] | 6 | import edu.gjoko.schedlr.entity.StakeholderType;
|
---|
| 7 | import edu.gjoko.schedlr.repositories.BusinessRepository;
|
---|
| 8 | import edu.gjoko.schedlr.repositories.ServiceTypeRepository;
|
---|
| 9 | import lombok.AllArgsConstructor;
|
---|
[8bcd64c] | 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[46fd0c7] | 11 | import org.springframework.stereotype.Service;
|
---|
| 12 |
|
---|
| 13 | import java.util.List;
|
---|
| 14 |
|
---|
| 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;
|
---|
[8bcd64c] | 23 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
[46fd0c7] | 24 |
|
---|
| 25 | public void saveBusiness(Business business) {
|
---|
| 26 | saveNewServiceTypes(business);
|
---|
| 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 |
|
---|
| 33 | public void saveNewServiceTypes(Business business) {
|
---|
| 34 | business.getServices()
|
---|
[9050790] | 35 | .stream()
|
---|
| 36 | .forEach(service -> {
|
---|
| 37 | if (service.getServiceType().getId() == null) {
|
---|
| 38 | ServiceType serviceType = service.getServiceType();
|
---|
| 39 | serviceType.setBusinessType(business.getBusinessType());
|
---|
| 40 | service.setServiceType(serviceTypeRepository.save(serviceType));
|
---|
| 41 | }
|
---|
| 42 | });
|
---|
[46fd0c7] | 43 | }
|
---|
| 44 |
|
---|
[9050790] | 45 | public List<Business> findAll() {
|
---|
| 46 | return businessRepository.findAll();
|
---|
[46fd0c7] | 47 | }
|
---|
| 48 |
|
---|
[9050790] | 49 | public void updateBusinesses(List<Business> businessList) {
|
---|
[46fd0c7] | 50 | businessList
|
---|
| 51 | .stream()
|
---|
| 52 | .forEach(business -> {
|
---|
| 53 | businessRepository.save(business);
|
---|
| 54 | });
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[8bcd64c] | 57 | public Business findByOwner(Long ownerId) {
|
---|
| 58 | var owner = new Stakeholder();
|
---|
| 59 | owner.setId(ownerId);
|
---|
| 60 | return businessRepository.findBusinessByOwner(owner);
|
---|
| 61 | }
|
---|
[46fd0c7] | 62 | }
|
---|