Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ServiceArea service #25

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/main/java/com/sarapis/orservice/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.sarapis.orservice.controller;
miguel-merlin marked this conversation as resolved.
Show resolved Hide resolved

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) { //stack overflow the dependency injection paste error and then @autowire
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete the stack overflow comment

this.serviceAreaService = serviceAreaService;
}

@GetMapping
public ResponseEntity<List<ServiceAreaDTO>> getAllServiceAreas() {
List<ServiceAreaDTO> serviceAreaDTOs = this.serviceAreaService.getAllServiceAreas();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use this when calling the serviceAreaService as you already set the attribute in the Constructor of the class. Please change it to serviceAreaService instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for every other usage of serviceAreaService

return ResponseEntity.ok(serviceAreaDTOs);
}

@GetMapping("/{id}")
public ResponseEntity<ServiceAreaDTO> getServiceAreaById(@PathVariable String id) {
ServiceAreaDTO serviceArea = this.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 = this.serviceAreaService.createServiceArea(serviceAreaDTO);
return ResponseEntity.ok(createdServiceArea);
}

@PutMapping("/{id}")
public ResponseEntity<ServiceAreaDTO> updateServiceArea(@PathVariable String id, @RequestBody ServiceAreaDTO serviceAreaDTO) {
ServiceAreaDTO updatedAccessibility = this.serviceAreaService.updateServiceArea(id, serviceAreaDTO);
return ResponseEntity.ok(updatedAccessibility);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteServiceArea(@PathVariable String id) {
this.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());
}
}