source: src/main/java/edu/gjoko/schedlr/services/BusinessService.java

Last change on this file was 1413ee2, checked in by gjoko kostadinov <gjokokostadinov@…>, 11 months ago

Add all bug fixes.

  • Property mode set to 100755
File size: 2.3 KB
Line 
1package edu.gjoko.schedlr.services;
2
3import edu.gjoko.schedlr.entity.Business;
4import edu.gjoko.schedlr.entity.Stakeholder;
5import edu.gjoko.schedlr.entity.StakeholderType;
6import edu.gjoko.schedlr.repositories.BusinessRepository;
7import lombok.AllArgsConstructor;
8import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
9import org.springframework.stereotype.Service;
10
11import java.util.List;
12
13import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
14import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
15
16@Service
17@AllArgsConstructor
18public 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}
Note: See TracBrowser for help on using the repository browser.