diff --git a/src/main/java/com/sarapis/orservice/controller/ServiceAreaController.java b/src/main/java/com/sarapis/orservice/controller/ServiceAreaController.java new file mode 100644 index 0000000..a9f978e --- /dev/null +++ b/src/main/java/com/sarapis/orservice/controller/ServiceAreaController.java @@ -0,0 +1,56 @@ +package com.sarapis.orservice.controller; + +import com.sarapis.orservice.dto.ServiceAreaDTO; +import com.sarapis.orservice.service.ServiceAreaService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@RestController +@RequestMapping("/api/service_areas") +public class ServiceAreaController { + + private final ServiceAreaService serviceAreaService; + + @Autowired + public ServiceAreaController(ServiceAreaService serviceAreaService) { + this.serviceAreaService = serviceAreaService; + } + + @GetMapping + public ResponseEntity> getAllServiceAreas() { + List serviceAreaDTOs = serviceAreaService.getAllServiceAreas(); + return ResponseEntity.ok(serviceAreaDTOs); + } + + @GetMapping("/{id}") + public ResponseEntity getServiceAreaById(@PathVariable String id) { + ServiceAreaDTO serviceArea = 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 = serviceAreaService.createServiceArea(serviceAreaDTO); + return ResponseEntity.ok(createdServiceArea); + } + + @PutMapping("/{id}") + public ResponseEntity updateServiceArea(@PathVariable String id, @RequestBody ServiceAreaDTO serviceAreaDTO) { + ServiceAreaDTO updatedAccessibility = serviceAreaService.updateServiceArea(id, serviceAreaDTO); + return ResponseEntity.ok(updatedAccessibility); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteServiceArea(@PathVariable String id) { + 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()); + } +}