From ca5d16f0384844fe70fbb0262cdcecb001d3f8a1 Mon Sep 17 00:00:00 2001 From: Vinayak Ranjan Date: Wed, 11 Dec 2024 16:03:25 -0500 Subject: [PATCH 1/2] Added ServiceArea service --- src/main/java/com/sarapis/orservice/.DS_Store | Bin 0 -> 6148 bytes .../controller/ServiceAreaController.java | 56 +++++++++++ .../repository/ServiceAreaRepository.java | 31 ++++++ .../orservice/service/ServiceAreaService.java | 20 ++++ .../service/ServiceAreaServiceImpl.java | 93 ++++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 src/main/java/com/sarapis/orservice/.DS_Store create mode 100644 src/main/java/com/sarapis/orservice/controller/ServiceAreaController.java create mode 100644 src/main/java/com/sarapis/orservice/repository/ServiceAreaRepository.java create mode 100644 src/main/java/com/sarapis/orservice/service/ServiceAreaService.java create mode 100644 src/main/java/com/sarapis/orservice/service/ServiceAreaServiceImpl.java diff --git a/src/main/java/com/sarapis/orservice/.DS_Store b/src/main/java/com/sarapis/orservice/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e75b544384a493538eb8548abcdabe5487ab5f43 GIT binary patch literal 6148 zcmeHKOKL(v5Ufsw2-&#Ia<1SF1~Dh_0)i0$ zdAwS@eht7DA1`xY24G5e#M#5x{M>zHca986;te$K&p;$bf_qQmA^+ZJ}AO$WJ zxX*(dY*Eaf^?lqruH?D)i5bc;4?U);H e$4^m|b> getAllServiceAreas() { + List serviceAreaDTOs = this.serviceAreaService.getAllServiceAreas(); + return ResponseEntity.ok(serviceAreaDTOs); + } + + @GetMapping("/{id}") + public ResponseEntity getServiceAreaById(@PathVariable String id) { + ServiceAreaDTO serviceArea = this.serviceAreaService.getServiceAreaById(id); + return ResponseEntity.ok(serviceArea); + } + + @PostMapping + public ResponseEntity createServiceArea(@RequestBody ServiceAreaDTO serviceAreaDTO) { + if (serviceAreaDTO.getId() == null) { + serviceAreaDTO.setId(UUID.randomUUID().toString()); + } + ServiceAreaDTO createdServiceArea = this.serviceAreaService.createServiceArea(serviceAreaDTO); + return ResponseEntity.ok(createdServiceArea); + } + + @PutMapping("/{id}") + public ResponseEntity updateServiceArea(@PathVariable String id, @RequestBody ServiceAreaDTO serviceAreaDTO) { + ServiceAreaDTO updatedAccessibility = this.serviceAreaService.updateServiceArea(id, serviceAreaDTO); + return ResponseEntity.ok(updatedAccessibility); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteServiceArea(@PathVariable String id) { + this.serviceAreaService.deleteServiceArea(id); + return ResponseEntity.noContent().build(); + } + +} diff --git a/src/main/java/com/sarapis/orservice/repository/ServiceAreaRepository.java b/src/main/java/com/sarapis/orservice/repository/ServiceAreaRepository.java new file mode 100644 index 0000000..f09d5dc --- /dev/null +++ b/src/main/java/com/sarapis/orservice/repository/ServiceAreaRepository.java @@ -0,0 +1,31 @@ +package com.sarapis.orservice.repository; + +import com.sarapis.orservice.entity.ServiceArea; +import com.sarapis.orservice.entity.Attribute; +import com.sarapis.orservice.entity.Metadata; +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 ServiceAreaRepository extends JpaRepository { + @Query("SELECT new Attribute(id, linkId, linkType, linkEntity, value, taxonomyTerm, label) FROM Attribute WHERE linkId = ?1") + List getAttributes(String organizationId); + + @Query("SELECT new Metadata(id, resourceId, resourceType, lastActionDate, lastActionType, fieldName, previousValue, replacementValue, updatedBy) FROM Metadata WHERE resourceId = ?1") + List getMetadata(String organizationId); + + @Modifying + @Transactional + @Query("DELETE FROM Attribute WHERE linkId = ?1") + void deleteAttributes(String organizationId); + + @Modifying + @Transactional + @Query("DELETE FROM Metadata WHERE resourceId = ?1") + void deleteMetadata(String organizationId); +} diff --git a/src/main/java/com/sarapis/orservice/service/ServiceAreaService.java b/src/main/java/com/sarapis/orservice/service/ServiceAreaService.java new file mode 100644 index 0000000..4c247e2 --- /dev/null +++ b/src/main/java/com/sarapis/orservice/service/ServiceAreaService.java @@ -0,0 +1,20 @@ +package com.sarapis.orservice.service; + +import com.sarapis.orservice.dto.ServiceAreaDTO; +import com.sarapis.orservice.dto.ServiceDTO; +import org.springframework.stereotype.Service; + +import java.util.List; + +public interface ServiceAreaService { + + List getAllServiceAreas(); + + ServiceAreaDTO getServiceAreaById(String id); + + ServiceAreaDTO createServiceArea(ServiceAreaDTO serviceAreaDTO); + + ServiceAreaDTO updateServiceArea(String id, ServiceAreaDTO serviceAreaDTO); + + void deleteServiceArea(String id); +} diff --git a/src/main/java/com/sarapis/orservice/service/ServiceAreaServiceImpl.java b/src/main/java/com/sarapis/orservice/service/ServiceAreaServiceImpl.java new file mode 100644 index 0000000..bdec6e0 --- /dev/null +++ b/src/main/java/com/sarapis/orservice/service/ServiceAreaServiceImpl.java @@ -0,0 +1,93 @@ +package com.sarapis.orservice.service; + +import com.sarapis.orservice.dto.AttributeDTO; +import com.sarapis.orservice.dto.MetadataDTO; +import com.sarapis.orservice.dto.ServiceAreaDTO; +import com.sarapis.orservice.entity.Attribute; +import com.sarapis.orservice.entity.Metadata; +import com.sarapis.orservice.entity.ServiceArea; +import com.sarapis.orservice.repository.AttributeRepository; +import com.sarapis.orservice.repository.MetadataRepository; +import com.sarapis.orservice.repository.ServiceAreaRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ServiceAreaServiceImpl implements ServiceAreaService{ + private final ServiceAreaRepository serviceAreaRepository; + private final AttributeRepository attributeRepository; + private final MetadataRepository metadataRepository; + + @Autowired + public ServiceAreaServiceImpl(ServiceAreaRepository serviceAreaRepository, + AttributeRepository attributeRepository, + MetadataRepository metadataRepository) { + this.serviceAreaRepository = serviceAreaRepository; + this.attributeRepository = attributeRepository; + this.metadataRepository = metadataRepository; + } + + @Override + public List getAllServiceAreas() { + List serviceAreaDTOs = this.serviceAreaRepository.findAll().stream().map(ServiceArea::toDTO).toList(); + serviceAreaDTOs.forEach(this::addRelatedData); + return serviceAreaDTOs; + } + + @Override + public ServiceAreaDTO getServiceAreaById(String id) { + ServiceArea serviceArea = this.serviceAreaRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Service Area not found.")); + ServiceAreaDTO serviceAreaDTO = serviceArea.toDTO(); + this.addRelatedData(serviceAreaDTO); + return serviceAreaDTO; + } + + @Override + public ServiceAreaDTO createServiceArea(ServiceAreaDTO serviceAreaDTO) { + ServiceArea serviceArea = this.serviceAreaRepository.save(serviceAreaDTO.toEntity()); + + for(AttributeDTO attributeDTO: serviceAreaDTO.getAttributes()) { + this.attributeRepository.save(attributeDTO.toEntity(serviceArea.getId())); + } + + for (MetadataDTO metadataDTO : serviceAreaDTO.getMetadata()) { + this.metadataRepository.save(metadataDTO.toEntity(serviceArea.getId())); + } + + ServiceAreaDTO savedServiceAreaDTO = this.serviceAreaRepository.save(serviceArea).toDTO(); + this.addRelatedData(serviceAreaDTO); + return savedServiceAreaDTO; + } + + @Override + public ServiceAreaDTO updateServiceArea(String id, ServiceAreaDTO serviceAreaDTO) { + ServiceArea oldServiceArea = this.serviceAreaRepository.findById(id) + .orElseThrow(() -> new RuntimeException("ServiceArea not found")); + oldServiceArea.setId(serviceAreaDTO.getId()); + oldServiceArea.setName(serviceAreaDTO.getName()); + oldServiceArea.setDescription(serviceAreaDTO.getDescription()); + oldServiceArea.setExtent(serviceAreaDTO.getExtent()); + oldServiceArea.setExtentType(serviceAreaDTO.getExtentType()); + oldServiceArea.setUri(serviceAreaDTO.getUri()); + + ServiceArea updatedServiceArea = this.serviceAreaRepository.save(oldServiceArea); + return updatedServiceArea.toDTO(); + } + + @Override + public void deleteServiceArea(String id) { + ServiceArea serviceArea = this.serviceAreaRepository.findById(id) + .orElseThrow(() -> new RuntimeException("ServiceArea not found")); + this.serviceAreaRepository.deleteAttributes(serviceArea.getId()); + this.serviceAreaRepository.deleteMetadata(serviceArea.getId()); + this.serviceAreaRepository.delete(serviceArea); + } + + private void addRelatedData(ServiceAreaDTO serviceAreaDTO) { + serviceAreaDTO.getAttributes().addAll(this.serviceAreaRepository.getAttributes(serviceAreaDTO.getId()).stream().map(Attribute::toDTO).toList()); + serviceAreaDTO.getMetadata().addAll(this.serviceAreaRepository.getMetadata(serviceAreaDTO.getId()).stream().map(Metadata::toDTO).toList()); + } +} From 04b9bc2f2a51c5415474b10aa0470fb7f796b67b Mon Sep 17 00:00:00 2001 From: Vinayak Ranjan Date: Thu, 12 Dec 2024 11:44:37 -0500 Subject: [PATCH 2/2] Removed comment, fixed serviceAreaService usages, and removed .DS_Store files. --- src/main/java/com/sarapis/orservice/.DS_Store | Bin 6148 -> 0 bytes .../controller/ServiceAreaController.java | 12 ++++++------ 2 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 src/main/java/com/sarapis/orservice/.DS_Store diff --git a/src/main/java/com/sarapis/orservice/.DS_Store b/src/main/java/com/sarapis/orservice/.DS_Store deleted file mode 100644 index e75b544384a493538eb8548abcdabe5487ab5f43..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKOKL(v5Ufsw2-&#Ia<1SF1~Dh_0)i0$ zdAwS@eht7DA1`xY24G5e#M#5x{M>zHca986;te$K&p;$bf_qQmA^+ZJ}AO$WJ zxX*(dY*Eaf^?lqruH?D)i5bc;4?U);H e$4^m|b> getAllServiceAreas() { - List serviceAreaDTOs = this.serviceAreaService.getAllServiceAreas(); + List serviceAreaDTOs = serviceAreaService.getAllServiceAreas(); return ResponseEntity.ok(serviceAreaDTOs); } @GetMapping("/{id}") public ResponseEntity getServiceAreaById(@PathVariable String id) { - ServiceAreaDTO serviceArea = this.serviceAreaService.getServiceAreaById(id); + ServiceAreaDTO serviceArea = serviceAreaService.getServiceAreaById(id); return ResponseEntity.ok(serviceArea); } @@ -37,19 +37,19 @@ public ResponseEntity createServiceArea(@RequestBody ServiceArea if (serviceAreaDTO.getId() == null) { serviceAreaDTO.setId(UUID.randomUUID().toString()); } - ServiceAreaDTO createdServiceArea = this.serviceAreaService.createServiceArea(serviceAreaDTO); + ServiceAreaDTO createdServiceArea = serviceAreaService.createServiceArea(serviceAreaDTO); return ResponseEntity.ok(createdServiceArea); } @PutMapping("/{id}") public ResponseEntity updateServiceArea(@PathVariable String id, @RequestBody ServiceAreaDTO serviceAreaDTO) { - ServiceAreaDTO updatedAccessibility = this.serviceAreaService.updateServiceArea(id, serviceAreaDTO); + ServiceAreaDTO updatedAccessibility = serviceAreaService.updateServiceArea(id, serviceAreaDTO); return ResponseEntity.ok(updatedAccessibility); } @DeleteMapping("/{id}") public ResponseEntity deleteServiceArea(@PathVariable String id) { - this.serviceAreaService.deleteServiceArea(id); + serviceAreaService.deleteServiceArea(id); return ResponseEntity.noContent().build(); }