|
1 | 1 | package com.sarapis.orservice.service;
|
2 | 2 |
|
| 3 | +import com.sarapis.orservice.dto.AttributeDTO; |
| 4 | +import com.sarapis.orservice.dto.ContactDTO; |
| 5 | +import com.sarapis.orservice.dto.MetadataDTO; |
| 6 | +import com.sarapis.orservice.dto.PhoneDTO; |
| 7 | +import com.sarapis.orservice.dto.ScheduleDTO; |
| 8 | +import com.sarapis.orservice.dto.ServiceAreaDTO; |
3 | 9 | import com.sarapis.orservice.dto.ServiceAtLocationDTO;
|
| 10 | +import com.sarapis.orservice.entity.Attribute; |
| 11 | +import com.sarapis.orservice.entity.Metadata; |
4 | 12 | import com.sarapis.orservice.entity.core.ServiceAtLocation;
|
| 13 | +import com.sarapis.orservice.repository.AttributeRepository; |
| 14 | +import com.sarapis.orservice.repository.MetadataRepository; |
5 | 15 | import com.sarapis.orservice.repository.ServiceAtLocationRepository;
|
6 | 16 | import java.util.List;
|
7 | 17 | import org.springframework.beans.factory.annotation.Autowired;
|
|
10 | 20 | @Service
|
11 | 21 | public class ServiceAtLocationServiceImpl implements ServiceAtLocationService {
|
12 | 22 | private final ServiceAtLocationRepository serviceAtLocationRepository;
|
| 23 | + private final AttributeRepository attributeRepository; |
| 24 | + private final MetadataRepository metadataRepository; |
13 | 25 |
|
14 | 26 | @Autowired
|
15 |
| - public ServiceAtLocationServiceImpl(ServiceAtLocationRepository serviceAtLocationService) { |
| 27 | + public ServiceAtLocationServiceImpl(ServiceAtLocationRepository serviceAtLocationService, AttributeRepository attributeRepository, MetadataRepository metadataRepository) { |
16 | 28 | this.serviceAtLocationRepository = serviceAtLocationService;
|
17 |
| - } |
18 |
| - |
19 |
| - private ServiceAtLocationDTO mapToDTO(ServiceAtLocation serviceAtLocation) { |
20 |
| - return null; |
21 |
| - } |
22 |
| - |
23 |
| - private ServiceAtLocation mapToEntity(ServiceAtLocationDTO serviceAtLocationDTO) { |
24 |
| - return null; |
| 29 | + this.attributeRepository = attributeRepository; |
| 30 | + this.metadataRepository = metadataRepository; |
25 | 31 | }
|
26 | 32 |
|
27 | 33 | @Override
|
28 | 34 | public List<ServiceAtLocationDTO> getAllServicesAtLocation() {
|
29 |
| - return List.of(); |
| 35 | + List<ServiceAtLocationDTO> servLocsDTOs = this.serviceAtLocationRepository.findAll().stream() |
| 36 | + .map(ServiceAtLocation::toDTO) |
| 37 | + .toList(); |
| 38 | + servLocsDTOs.forEach(this::addRelatedData); |
| 39 | + return servLocsDTOs; |
30 | 40 | }
|
31 | 41 |
|
32 | 42 | @Override
|
33 | 43 | public ServiceAtLocationDTO getServiceAtLocationById(String id) {
|
34 |
| - return null; |
| 44 | + ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id) |
| 45 | + .orElseThrow(() -> new RuntimeException("Service At Location not found.")); |
| 46 | + |
| 47 | + ServiceAtLocationDTO servLocDTO = servLoc.toDTO(); |
| 48 | + this.addRelatedData(servLocDTO); |
| 49 | + return servLocDTO; |
35 | 50 | }
|
36 | 51 |
|
37 | 52 | @Override
|
38 | 53 | public ServiceAtLocationDTO createServiceAtLocation(ServiceAtLocationDTO serviceAtLocationDTO) {
|
39 |
| - return null; |
| 54 | + ServiceAtLocation servLoc = this.serviceAtLocationRepository.save(serviceAtLocationDTO.toEntity()); |
| 55 | + |
| 56 | + for (AttributeDTO attributeDTO : serviceAtLocationDTO.getAttributes()) { |
| 57 | + this.attributeRepository.save(attributeDTO.toEntity(servLoc.getId())); |
| 58 | + } |
| 59 | + |
| 60 | + for (MetadataDTO metadataDTO : serviceAtLocationDTO.getMetadata()) { |
| 61 | + this.metadataRepository.save(metadataDTO.toEntity(servLoc.getId())); |
| 62 | + } |
| 63 | + |
| 64 | + ServiceAtLocationDTO savedServLocDTO = this.serviceAtLocationRepository.save(servLoc).toDTO(); |
| 65 | + this.addRelatedData(savedServLocDTO); |
| 66 | + return savedServLocDTO; |
40 | 67 | }
|
41 | 68 |
|
42 | 69 | @Override
|
43 | 70 | public ServiceAtLocationDTO updateServiceAtLocation(String id,
|
44 | 71 | ServiceAtLocationDTO serviceAtLocationDTO) {
|
45 |
| - return null; |
| 72 | + ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id) |
| 73 | + .orElseThrow(() -> new RuntimeException("Service At Location not found")); |
| 74 | + |
| 75 | + servLoc.setDescription(serviceAtLocationDTO.getDescription()); |
| 76 | + servLoc.setServiceAreas(serviceAtLocationDTO.getServiceAreas().stream().map(ServiceAreaDTO::toEntity).toList()); |
| 77 | + servLoc.setContacts(serviceAtLocationDTO.getContacts().stream().map(ContactDTO::toEntity).toList()); |
| 78 | + servLoc.setPhones(serviceAtLocationDTO.getPhones().stream().map(PhoneDTO::toEntity).toList()); |
| 79 | + servLoc.setSchedules(serviceAtLocationDTO.getSchedules().stream().map(ScheduleDTO::toEntity).toList()); |
| 80 | + servLoc.setLocation(serviceAtLocationDTO.getLocation().toEntity()); |
| 81 | + |
| 82 | + ServiceAtLocation updatedServLoc = this.serviceAtLocationRepository.save(servLoc); |
| 83 | + return updatedServLoc.toDTO(); |
46 | 84 | }
|
47 | 85 |
|
48 | 86 | @Override
|
49 | 87 | public void deleteServiceAtLocation(String id) {
|
| 88 | + ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id) |
| 89 | + .orElseThrow(() -> new RuntimeException("Service At Location not found.")); |
| 90 | + |
| 91 | + this.serviceAtLocationRepository.delete(servLoc); |
| 92 | + } |
| 93 | + |
| 94 | + private void addRelatedData(ServiceAtLocationDTO serviceAtLocationDTO) { |
| 95 | + String id = serviceAtLocationDTO.getId(); |
| 96 | + List<AttributeDTO> attributeDTOs = this.serviceAtLocationRepository.getAttributes(id) |
| 97 | + .stream() |
| 98 | + .map(Attribute::toDTO) |
| 99 | + .toList(); |
| 100 | + |
| 101 | + List<MetadataDTO> metadataDTOs = this.serviceAtLocationRepository.getMetadata(id) |
| 102 | + .stream() |
| 103 | + .map(Metadata::toDTO) |
| 104 | + .toList(); |
50 | 105 |
|
| 106 | + serviceAtLocationDTO.getAttributes().addAll(attributeDTOs); |
| 107 | + serviceAtLocationDTO.getMetadata().addAll(metadataDTOs); |
51 | 108 | }
|
52 | 109 | }
|
0 commit comments