From 52f65b9fee30d0860a3b1615a9d1c3637bb7bc71 Mon Sep 17 00:00:00 2001 From: Butter - ThinkWin Date: Tue, 10 Dec 2024 19:03:26 -0500 Subject: [PATCH 1/4] Updated ServiceAtLocation Repository with Attribute and Metadata Functions --- .../ServiceAtLocationRepository.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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); } From 69f5c457f5931a9426fbb3144875640883f98dcc Mon Sep 17 00:00:00 2001 From: Butter - ThinkWin Date: Tue, 10 Dec 2024 19:33:03 -0500 Subject: [PATCH 2/4] Implemented ServiceAtLocationServiceImpl functions --- .../service/ServiceAtLocationServiceImpl.java | 83 ++++++++++++++++--- 1 file changed, 70 insertions(+), 13 deletions(-) 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); } } From 6a8c467637abccb965d237c23dbcfbdb81356dff Mon Sep 17 00:00:00 2001 From: Butter - ThinkWin Date: Tue, 10 Dec 2024 19:35:47 -0500 Subject: [PATCH 3/4] Implemented CRUD for ServiceAtLocationServiceController exlcuding GetAll --- .../controller/ServiceAtLocationController.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java b/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java index 9de6383..9b06f48 100644 --- a/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java +++ b/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java @@ -31,17 +31,20 @@ public ResponseEntity> getAllServiceAtLocati @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}") From 0663107fe7f565c49d6894e01753f32b65df2e77 Mon Sep 17 00:00:00 2001 From: Butter - ThinkWin Date: Tue, 10 Dec 2024 19:45:08 -0500 Subject: [PATCH 4/4] Implemented getAll for ServiceAtLocationController --- .../controller/ServiceAtLocationController.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java b/src/main/java/com/sarapis/orservice/controller/ServiceAtLocationController.java index 9b06f48..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,7 +28,19 @@ 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}")