1 | package edu.gjoko.schedlr.services;
|
---|
2 |
|
---|
3 | import edu.gjoko.schedlr.entity.*;
|
---|
4 | import edu.gjoko.schedlr.repositories.*;
|
---|
5 | import lombok.AllArgsConstructor;
|
---|
6 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 | import org.springframework.util.CollectionUtils;
|
---|
9 |
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
|
---|
13 | import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | @AllArgsConstructor
|
---|
17 | public class BusinessService {
|
---|
18 |
|
---|
19 | private final BusinessRepository businessRepository;
|
---|
20 | private final ServiceTypeRepository serviceTypeRepository;
|
---|
21 | private final ServiceRepository serviceRepository;
|
---|
22 | private final StakeholderService stakeholderService;
|
---|
23 |
|
---|
24 | private final StakeholderRepository stakeholderRepository;
|
---|
25 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
26 |
|
---|
27 | public void saveBusiness(Business business) {
|
---|
28 | saveNewServiceTypes(business);
|
---|
29 | business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
|
---|
30 | business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
|
---|
31 | business.setBusinessStatus(NEW);
|
---|
32 | businessRepository.save(business);
|
---|
33 | }
|
---|
34 |
|
---|
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 | public List<Business> findAll() {
|
---|
48 | return businessRepository.findAll();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void updateBusinesses(List<Business> businessList) {
|
---|
52 | businessList
|
---|
53 | .stream()
|
---|
54 | .forEach(business -> {
|
---|
55 | stakeholderService.saveOrUpdateStakeholder(business.getOwner());
|
---|
56 | saveOrUpdateServices(business.getServices());
|
---|
57 | saveOrUpdateBusiness(business);
|
---|
58 | });
|
---|
59 | }
|
---|
60 |
|
---|
61 | public Business findByOwner(Long ownerId) {
|
---|
62 | var owner = new Stakeholder();
|
---|
63 | owner.setId(ownerId);
|
---|
64 | return businessRepository.findBusinessByOwner(owner);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public List<Business> findByBusinessTypeAndActiveStatus(Long businessTypeId) {
|
---|
68 | return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId);
|
---|
69 | }
|
---|
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 | }
|
---|