1 | package edu.gjoko.schedlr.services;
|
---|
2 |
|
---|
3 | import edu.gjoko.schedlr.entity.Business;
|
---|
4 | import edu.gjoko.schedlr.entity.ServiceType;
|
---|
5 | import edu.gjoko.schedlr.entity.Stakeholder;
|
---|
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;
|
---|
10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
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;
|
---|
23 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
24 |
|
---|
25 | public void saveBusiness(Business business) {
|
---|
26 | saveNewServiceTypes(business);
|
---|
27 | business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
|
---|
28 | business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
|
---|
29 | business.setBusinessStatus(NEW);
|
---|
30 | businessRepository.save(business);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void saveNewServiceTypes(Business business) {
|
---|
34 | business.getServices()
|
---|
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 | });
|
---|
43 | }
|
---|
44 |
|
---|
45 | public List<Business> findAll() {
|
---|
46 | return businessRepository.findAll();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void updateBusinesses(List<Business> businessList) {
|
---|
50 | businessList
|
---|
51 | .stream()
|
---|
52 | .forEach(business -> {
|
---|
53 | businessRepository.save(business);
|
---|
54 | });
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Business findByOwner(Long ownerId) {
|
---|
58 | var owner = new Stakeholder();
|
---|
59 | owner.setId(ownerId);
|
---|
60 | return businessRepository.findBusinessByOwner(owner);
|
---|
61 | }
|
---|
62 | }
|
---|