1 | package edu.gjoko.schedlr.services;
|
---|
2 |
|
---|
3 | import edu.gjoko.schedlr.entity.Business;
|
---|
4 | import edu.gjoko.schedlr.entity.BusinessStatus;
|
---|
5 | import edu.gjoko.schedlr.entity.ServiceType;
|
---|
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.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.stream.Collectors;
|
---|
14 |
|
---|
15 | import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
|
---|
16 | import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | @AllArgsConstructor
|
---|
20 | public class BusinessService {
|
---|
21 |
|
---|
22 | private final BusinessRepository businessRepository;
|
---|
23 | private final ServiceTypeRepository serviceTypeRepository;
|
---|
24 |
|
---|
25 | public void saveBusiness(Business business) {
|
---|
26 | saveNewServiceTypes(business);
|
---|
27 | business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
|
---|
28 | business.setBusinessStatus(NEW);
|
---|
29 | businessRepository.save(business);
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void saveNewServiceTypes(Business business) {
|
---|
33 | business.getServices()
|
---|
34 | .stream()
|
---|
35 | .forEach(service -> {
|
---|
36 | if(service.getServiceType().getId() == null) {
|
---|
37 | ServiceType serviceType = service.getServiceType();
|
---|
38 | serviceType.setBusinessType(business.getBusinessType());
|
---|
39 | service.setServiceType(serviceTypeRepository.save(serviceType));
|
---|
40 | }
|
---|
41 | });
|
---|
42 | }
|
---|
43 |
|
---|
44 | public List<Business> getNewBusiness() {
|
---|
45 | return businessRepository.findBusinessesByBusinessStatus(NEW);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void activateBusinesses(List<Business> businessList) {
|
---|
49 | businessList
|
---|
50 | .stream()
|
---|
51 | .forEach(business -> {
|
---|
52 | business.setBusinessStatus(ACTIVE);
|
---|
53 | businessRepository.save(business);
|
---|
54 | });
|
---|
55 | }
|
---|
56 |
|
---|
57 | }
|
---|