-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from stevensblueprint/feature/service_area
Added ServiceArea service
- Loading branch information
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/com/sarapis/orservice/controller/ServiceAreaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/sarapis/orservice/repository/ServiceAreaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sarapis/orservice/service/ServiceAreaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/sarapis/orservice/service/ServiceAreaServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |