1 | package edu.gjoko.schedlr.services;
|
---|
2 |
|
---|
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;
|
---|
7 | import lombok.AllArgsConstructor;
|
---|
8 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
|
---|
14 | import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
|
---|
15 |
|
---|
16 | @Service
|
---|
17 | @AllArgsConstructor
|
---|
18 | public class BusinessService {
|
---|
19 |
|
---|
20 | private final BusinessRepository businessRepository;
|
---|
21 | private final ServicesService servicesService;
|
---|
22 | private final StakeholderService stakeholderService;
|
---|
23 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
24 |
|
---|
25 | public void saveBusiness(Business business) {
|
---|
26 | servicesService.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 List<Business> findAll() {
|
---|
34 | return businessRepository.findAll();
|
---|
35 | }
|
---|
36 |
|
---|
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) {
|
---|
46 | businessList
|
---|
47 | .stream()
|
---|
48 | .forEach(business -> {
|
---|
49 | Business business1 = businessRepository.findById(business.getId()).get();
|
---|
50 | business1.setBusinessStatus(business.getBusinessStatus());
|
---|
51 | businessRepository.save(business1);
|
---|
52 | });
|
---|
53 | }
|
---|
54 |
|
---|
55 | public Business findByOwner(Long ownerId) {
|
---|
56 | var owner = new Stakeholder();
|
---|
57 | owner.setId(ownerId);
|
---|
58 | return businessRepository.findBusinessByOwner(owner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public List<Business> findByBusinessTypeAndActiveStatus(Long businessTypeId) {
|
---|
62 | return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId);
|
---|
63 | }
|
---|
64 | }
|
---|