Skip to content

Commit

Permalink
Merge pull request #25 from stevensblueprint/feature/service_area
Browse files Browse the repository at this point in the history
Added ServiceArea service
  • Loading branch information
miguel-merlin authored Dec 14, 2024
2 parents 39e76d0 + 04b9bc2 commit 0c5c01f
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<List<ServiceAreaDTO>> getAllServiceAreas() {
List<ServiceAreaDTO> serviceAreaDTOs = serviceAreaService.getAllServiceAreas();
return ResponseEntity.ok(serviceAreaDTOs);
}

@GetMapping("/{id}")
public ResponseEntity<ServiceAreaDTO> getServiceAreaById(@PathVariable String id) {
ServiceAreaDTO serviceArea = serviceAreaService.getServiceAreaById(id);
return ResponseEntity.ok(serviceArea);
}

@PostMapping
public ResponseEntity<ServiceAreaDTO> 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<ServiceAreaDTO> updateServiceArea(@PathVariable String id, @RequestBody ServiceAreaDTO serviceAreaDTO) {
ServiceAreaDTO updatedAccessibility = serviceAreaService.updateServiceArea(id, serviceAreaDTO);
return ResponseEntity.ok(updatedAccessibility);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteServiceArea(@PathVariable String id) {
serviceAreaService.deleteServiceArea(id);
return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
@@ -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<ServiceArea, String> {
@Query("SELECT new Attribute(id, linkId, linkType, linkEntity, value, taxonomyTerm, label) FROM Attribute WHERE linkId = ?1")
List<Attribute> getAttributes(String organizationId);

@Query("SELECT new Metadata(id, resourceId, resourceType, lastActionDate, lastActionType, fieldName, previousValue, replacementValue, updatedBy) FROM Metadata WHERE resourceId = ?1")
List<Metadata> 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);
}
Original file line number Diff line number Diff line change
@@ -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<ServiceAreaDTO> getAllServiceAreas();

ServiceAreaDTO getServiceAreaById(String id);

ServiceAreaDTO createServiceArea(ServiceAreaDTO serviceAreaDTO);

ServiceAreaDTO updateServiceArea(String id, ServiceAreaDTO serviceAreaDTO);

void deleteServiceArea(String id);
}
Original file line number Diff line number Diff line change
@@ -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<ServiceAreaDTO> getAllServiceAreas() {
List<ServiceAreaDTO> 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());
}
}

0 comments on commit 0c5c01f

Please sign in to comment.