[1413ee2] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
| 3 | import edu.gjoko.schedlr.entity.Business;
|
---|
| 4 | import edu.gjoko.schedlr.entity.ServiceStatus;
|
---|
| 5 | import edu.gjoko.schedlr.entity.ServiceType;
|
---|
| 6 | import edu.gjoko.schedlr.repositories.BusinessRepository;
|
---|
| 7 | import edu.gjoko.schedlr.repositories.BusinessTypeRepository;
|
---|
| 8 | import edu.gjoko.schedlr.repositories.ServiceRepository;
|
---|
| 9 | import edu.gjoko.schedlr.repositories.ServiceTypeRepository;
|
---|
| 10 | import lombok.AllArgsConstructor;
|
---|
| 11 | import org.springframework.stereotype.Service;
|
---|
| 12 | import org.springframework.util.CollectionUtils;
|
---|
| 13 |
|
---|
| 14 | import javax.transaction.Transactional;
|
---|
| 15 | import java.util.List;
|
---|
| 16 | import java.util.Optional;
|
---|
| 17 |
|
---|
| 18 | @Service
|
---|
| 19 | @AllArgsConstructor
|
---|
| 20 | public class ServicesService {
|
---|
| 21 |
|
---|
| 22 | private final ServiceRepository serviceRepository;
|
---|
| 23 | private final ServiceTypeRepository serviceTypeRepository;
|
---|
| 24 | private final BusinessRepository businessRepository;
|
---|
| 25 |
|
---|
| 26 | @Transactional
|
---|
| 27 | public void deleteByIds(List<Long> ids) {
|
---|
| 28 | ids.forEach(this::deactivateService);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public void saveOrUpdateServices(List<edu.gjoko.schedlr.entity.Service> serviceList, Long ownerId) {
|
---|
| 32 | if (!CollectionUtils.isEmpty(serviceList)) {
|
---|
| 33 | serviceList.forEach( service -> {
|
---|
| 34 | Optional<edu.gjoko.schedlr.entity.Service> foundOptional;
|
---|
| 35 | if (service.getId() != null && (foundOptional = serviceRepository.findById(service.getId())).isPresent()) {
|
---|
| 36 | var found = foundOptional.get();
|
---|
| 37 | found.setDuration(service.getDuration());
|
---|
| 38 | found.setPrice(service.getPrice());
|
---|
| 39 | found.setServiceStatus(ServiceStatus.ACTIVE);
|
---|
| 40 | serviceRepository.save(found);
|
---|
| 41 | } else {
|
---|
| 42 | var business = businessRepository.findBusinessesByOwner_Id(ownerId);
|
---|
| 43 | service.getServiceType().setBusinessType(business.getBusinessType());
|
---|
| 44 | service.setServiceStatus(ServiceStatus.ACTIVE);
|
---|
| 45 | service.setBusiness(business);
|
---|
| 46 | serviceTypeRepository.save(service.getServiceType());
|
---|
| 47 | serviceRepository.save(service);
|
---|
| 48 | }
|
---|
| 49 | });
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public void saveNewServiceTypes(Business business) {
|
---|
| 54 | business.getServices()
|
---|
| 55 | .forEach(service -> {
|
---|
| 56 | if (service.getServiceType().getId() == null) {
|
---|
| 57 | ServiceType serviceType = service.getServiceType();
|
---|
| 58 | serviceType.setBusinessType(business.getBusinessType());
|
---|
| 59 | service.setServiceType(serviceTypeRepository.save(serviceType));
|
---|
| 60 | }
|
---|
| 61 | });
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private void deactivateService(Long id) {
|
---|
| 65 | Optional<edu.gjoko.schedlr.entity.Service> serviceOptional;
|
---|
| 66 | if((serviceOptional = serviceRepository.findById(id)).isPresent()) {
|
---|
| 67 | var service = serviceOptional.get();
|
---|
| 68 | service.setServiceStatus(ServiceStatus.INACTIVE);
|
---|
| 69 | serviceRepository.save(service);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|