diff --git a/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java b/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java index 9de6383..d40bbfe 100644 --- a/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java +++ b/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java @@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping("/api/service_at_locations") public class ServiceAtLocationController { @@ -26,22 +28,37 @@ public ServiceAtLocationController(ServiceAtLocationService serviceAtLocationSer @GetMapping public ResponseEntity> getAllServiceAtLocations() { - return null; + List servLocDTOs = this.serviceAtLocationService.getAllServicesAtLocation(); + PaginationDTO paginationDTO = PaginationDTO.of( + servLocDTOs.size(), + 1, + 1, + servLocDTOs.size(), + true, + false, + false, + servLocDTOs + ); + + return ResponseEntity.ok(paginationDTO); } @GetMapping("/{id}") public ResponseEntity getServiceAtLocationById(@PathVariable String id) { - return null; + ServiceAtLocationDTO servLocDTO = this.serviceAtLocationService.getServiceAtLocationById(id); + return ResponseEntity.ok(servLocDTO); } @PostMapping public ResponseEntity createServiceAtLocation(@RequestBody ServiceAtLocationDTO serviceAtLocationDTO) { - return null; + ServiceAtLocationDTO createdServLocDTO = this.serviceAtLocationService.createServiceAtLocation(serviceAtLocationDTO); + return ResponseEntity.ok(createdServLocDTO); } @PutMapping("/{id}") public ResponseEntity updateServiceAtLocation(@PathVariable String id, @RequestBody ServiceAtLocationDTO serviceAtLocationDTO) { - return null; + ServiceAtLocationDTO updatedServLocDTO = this.serviceAtLocationService.updateServiceAtLocation(id, serviceAtLocationDTO); + return ResponseEntity.ok(updatedServLocDTO); } @DeleteMapping("/{id}") diff --git a/src/main/java/com/sarapis/orservice/repository/ServiceAtLocationRepository.java b/src/main/java/com/sarapis/orservice/repository/ServiceAtLocationRepository.java index f9e9f1b..12445b2 100644 --- a/src/main/java/com/sarapis/orservice/repository/ServiceAtLocationRepository.java +++ b/src/main/java/com/sarapis/orservice/repository/ServiceAtLocationRepository.java @@ -1,9 +1,31 @@ package com.sarapis.orservice.repository; +import com.sarapis.orservice.entity.Attribute; +import com.sarapis.orservice.entity.Metadata; import com.sarapis.orservice.entity.core.ServiceAtLocation; +import jakarta.transaction.Transactional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface ServiceAtLocationRepository extends JpaRepository { + @Query("SELECT new Attribute(id, linkId, linkType, linkEntity, value, taxonomyTerm, label) FROM Attribute WHERE linkId = ?1") + List getAttributes(String id); + + @Query("SELECT new Metadata(id, resourceId, resourceType, lastActionDate, lastActionType, fieldName, previousValue, replacementValue, updatedBy) FROM Metadata WHERE resourceId = ?1") + List getMetadata(String id); + + @Modifying + @Transactional + @Query("DELETE FROM Attribute WHERE linkId = ?1") + void deleteAttributes(String id); + + @Modifying + @Transactional + @Query("DELETE FROM Metadata WHERE resourceId = ?1") + void deleteMetadata(String id); } diff --git a/src/main/java/com/sarapis/orservice/service/ServiceAtLocationServiceImpl.java b/src/main/java/com/sarapis/orservice/service/ServiceAtLocationServiceImpl.java index 05dc80e..605b20c 100644 --- a/src/main/java/com/sarapis/orservice/service/ServiceAtLocationServiceImpl.java +++ b/src/main/java/com/sarapis/orservice/service/ServiceAtLocationServiceImpl.java @@ -1,7 +1,17 @@ package com.sarapis.orservice.service; +import com.sarapis.orservice.dto.AttributeDTO; +import com.sarapis.orservice.dto.ContactDTO; +import com.sarapis.orservice.dto.MetadataDTO; +import com.sarapis.orservice.dto.PhoneDTO; +import com.sarapis.orservice.dto.ScheduleDTO; +import com.sarapis.orservice.dto.ServiceAreaDTO; import com.sarapis.orservice.dto.ServiceAtLocationDTO; +import com.sarapis.orservice.entity.Attribute; +import com.sarapis.orservice.entity.Metadata; import com.sarapis.orservice.entity.core.ServiceAtLocation; +import com.sarapis.orservice.repository.AttributeRepository; +import com.sarapis.orservice.repository.MetadataRepository; import com.sarapis.orservice.repository.ServiceAtLocationRepository; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -10,43 +20,90 @@ @Service public class ServiceAtLocationServiceImpl implements ServiceAtLocationService { private final ServiceAtLocationRepository serviceAtLocationRepository; + private final AttributeRepository attributeRepository; + private final MetadataRepository metadataRepository; @Autowired - public ServiceAtLocationServiceImpl(ServiceAtLocationRepository serviceAtLocationService) { + public ServiceAtLocationServiceImpl(ServiceAtLocationRepository serviceAtLocationService, AttributeRepository attributeRepository, MetadataRepository metadataRepository) { this.serviceAtLocationRepository = serviceAtLocationService; - } - - private ServiceAtLocationDTO mapToDTO(ServiceAtLocation serviceAtLocation) { - return null; - } - - private ServiceAtLocation mapToEntity(ServiceAtLocationDTO serviceAtLocationDTO) { - return null; + this.attributeRepository = attributeRepository; + this.metadataRepository = metadataRepository; } @Override public List getAllServicesAtLocation() { - return List.of(); + List servLocsDTOs = this.serviceAtLocationRepository.findAll().stream() + .map(ServiceAtLocation::toDTO) + .toList(); + servLocsDTOs.forEach(this::addRelatedData); + return servLocsDTOs; } @Override public ServiceAtLocationDTO getServiceAtLocationById(String id) { - return null; + ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Service At Location not found.")); + + ServiceAtLocationDTO servLocDTO = servLoc.toDTO(); + this.addRelatedData(servLocDTO); + return servLocDTO; } @Override public ServiceAtLocationDTO createServiceAtLocation(ServiceAtLocationDTO serviceAtLocationDTO) { - return null; + ServiceAtLocation servLoc = this.serviceAtLocationRepository.save(serviceAtLocationDTO.toEntity()); + + for (AttributeDTO attributeDTO : serviceAtLocationDTO.getAttributes()) { + this.attributeRepository.save(attributeDTO.toEntity(servLoc.getId())); + } + + for (MetadataDTO metadataDTO : serviceAtLocationDTO.getMetadata()) { + this.metadataRepository.save(metadataDTO.toEntity(servLoc.getId())); + } + + ServiceAtLocationDTO savedServLocDTO = this.serviceAtLocationRepository.save(servLoc).toDTO(); + this.addRelatedData(savedServLocDTO); + return savedServLocDTO; } @Override public ServiceAtLocationDTO updateServiceAtLocation(String id, ServiceAtLocationDTO serviceAtLocationDTO) { - return null; + ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Service At Location not found")); + + servLoc.setDescription(serviceAtLocationDTO.getDescription()); + servLoc.setServiceAreas(serviceAtLocationDTO.getServiceAreas().stream().map(ServiceAreaDTO::toEntity).toList()); + servLoc.setContacts(serviceAtLocationDTO.getContacts().stream().map(ContactDTO::toEntity).toList()); + servLoc.setPhones(serviceAtLocationDTO.getPhones().stream().map(PhoneDTO::toEntity).toList()); + servLoc.setSchedules(serviceAtLocationDTO.getSchedules().stream().map(ScheduleDTO::toEntity).toList()); + servLoc.setLocation(serviceAtLocationDTO.getLocation().toEntity()); + + ServiceAtLocation updatedServLoc = this.serviceAtLocationRepository.save(servLoc); + return updatedServLoc.toDTO(); } @Override public void deleteServiceAtLocation(String id) { + ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Service At Location not found.")); + + this.serviceAtLocationRepository.delete(servLoc); + } + + private void addRelatedData(ServiceAtLocationDTO serviceAtLocationDTO) { + String id = serviceAtLocationDTO.getId(); + List attributeDTOs = this.serviceAtLocationRepository.getAttributes(id) + .stream() + .map(Attribute::toDTO) + .toList(); + + List metadataDTOs = this.serviceAtLocationRepository.getMetadata(id) + .stream() + .map(Metadata::toDTO) + .toList(); + serviceAtLocationDTO.getAttributes().addAll(attributeDTOs); + serviceAtLocationDTO.getMetadata().addAll(metadataDTOs); } }