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

Feat/ServiceAtLocation #27

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/service_at_locations")
public class ServiceAtLocationController {
Expand All @@ -26,22 +28,37 @@ public ServiceAtLocationController(ServiceAtLocationService serviceAtLocationSer

@GetMapping
public ResponseEntity<PaginationDTO<ServiceAtLocationDTO>> getAllServiceAtLocations() {
return null;
List<ServiceAtLocationDTO> servLocDTOs = this.serviceAtLocationService.getAllServicesAtLocation();
PaginationDTO<ServiceAtLocationDTO> paginationDTO = PaginationDTO.of(
servLocDTOs.size(),
1,
1,
servLocDTOs.size(),
true,
false,
false,
servLocDTOs
);

return ResponseEntity.ok(paginationDTO);
}

@GetMapping("/{id}")
public ResponseEntity<ServiceAtLocationDTO> getServiceAtLocationById(@PathVariable String id) {
return null;
ServiceAtLocationDTO servLocDTO = this.serviceAtLocationService.getServiceAtLocationById(id);
return ResponseEntity.ok(servLocDTO);
}

@PostMapping
public ResponseEntity<ServiceAtLocationDTO> createServiceAtLocation(@RequestBody ServiceAtLocationDTO serviceAtLocationDTO) {
return null;
ServiceAtLocationDTO createdServLocDTO = this.serviceAtLocationService.createServiceAtLocation(serviceAtLocationDTO);
return ResponseEntity.ok(createdServLocDTO);
}

@PutMapping("/{id}")
public ResponseEntity<ServiceAtLocationDTO> updateServiceAtLocation(@PathVariable String id, @RequestBody ServiceAtLocationDTO serviceAtLocationDTO) {
return null;
ServiceAtLocationDTO updatedServLocDTO = this.serviceAtLocationService.updateServiceAtLocation(id, serviceAtLocationDTO);
return ResponseEntity.ok(updatedServLocDTO);
}

@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
package com.sarapis.orservice.repository;

import com.sarapis.orservice.entity.Attribute;
import com.sarapis.orservice.entity.Metadata;
import com.sarapis.orservice.entity.core.ServiceAtLocation;
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 ServiceAtLocationRepository extends JpaRepository<ServiceAtLocation, String> {
@Query("SELECT new Attribute(id, linkId, linkType, linkEntity, value, taxonomyTerm, label) FROM Attribute WHERE linkId = ?1")
List<Attribute> getAttributes(String id);

@Query("SELECT new Metadata(id, resourceId, resourceType, lastActionDate, lastActionType, fieldName, previousValue, replacementValue, updatedBy) FROM Metadata WHERE resourceId = ?1")
List<Metadata> getMetadata(String id);

@Modifying
@Transactional
@Query("DELETE FROM Attribute WHERE linkId = ?1")
void deleteAttributes(String id);

@Modifying
@Transactional
@Query("DELETE FROM Metadata WHERE resourceId = ?1")
void deleteMetadata(String id);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.sarapis.orservice.service;

import com.sarapis.orservice.dto.AttributeDTO;
import com.sarapis.orservice.dto.ContactDTO;
import com.sarapis.orservice.dto.MetadataDTO;
import com.sarapis.orservice.dto.PhoneDTO;
import com.sarapis.orservice.dto.ScheduleDTO;
import com.sarapis.orservice.dto.ServiceAreaDTO;
import com.sarapis.orservice.dto.ServiceAtLocationDTO;
import com.sarapis.orservice.entity.Attribute;
import com.sarapis.orservice.entity.Metadata;
import com.sarapis.orservice.entity.core.ServiceAtLocation;
import com.sarapis.orservice.repository.AttributeRepository;
import com.sarapis.orservice.repository.MetadataRepository;
import com.sarapis.orservice.repository.ServiceAtLocationRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -10,43 +20,90 @@
@Service
public class ServiceAtLocationServiceImpl implements ServiceAtLocationService {
private final ServiceAtLocationRepository serviceAtLocationRepository;
private final AttributeRepository attributeRepository;
private final MetadataRepository metadataRepository;

@Autowired
public ServiceAtLocationServiceImpl(ServiceAtLocationRepository serviceAtLocationService) {
public ServiceAtLocationServiceImpl(ServiceAtLocationRepository serviceAtLocationService, AttributeRepository attributeRepository, MetadataRepository metadataRepository) {
this.serviceAtLocationRepository = serviceAtLocationService;
}

private ServiceAtLocationDTO mapToDTO(ServiceAtLocation serviceAtLocation) {
return null;
}

private ServiceAtLocation mapToEntity(ServiceAtLocationDTO serviceAtLocationDTO) {
return null;
this.attributeRepository = attributeRepository;
this.metadataRepository = metadataRepository;
}

@Override
public List<ServiceAtLocationDTO> getAllServicesAtLocation() {
return List.of();
List<ServiceAtLocationDTO> servLocsDTOs = this.serviceAtLocationRepository.findAll().stream()
.map(ServiceAtLocation::toDTO)
.toList();
servLocsDTOs.forEach(this::addRelatedData);
return servLocsDTOs;
}

@Override
public ServiceAtLocationDTO getServiceAtLocationById(String id) {
return null;
ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Service At Location not found."));

ServiceAtLocationDTO servLocDTO = servLoc.toDTO();
this.addRelatedData(servLocDTO);
return servLocDTO;
}

@Override
public ServiceAtLocationDTO createServiceAtLocation(ServiceAtLocationDTO serviceAtLocationDTO) {
return null;
ServiceAtLocation servLoc = this.serviceAtLocationRepository.save(serviceAtLocationDTO.toEntity());

for (AttributeDTO attributeDTO : serviceAtLocationDTO.getAttributes()) {
this.attributeRepository.save(attributeDTO.toEntity(servLoc.getId()));
}

for (MetadataDTO metadataDTO : serviceAtLocationDTO.getMetadata()) {
this.metadataRepository.save(metadataDTO.toEntity(servLoc.getId()));
}

ServiceAtLocationDTO savedServLocDTO = this.serviceAtLocationRepository.save(servLoc).toDTO();
this.addRelatedData(savedServLocDTO);
return savedServLocDTO;
}

@Override
public ServiceAtLocationDTO updateServiceAtLocation(String id,
ServiceAtLocationDTO serviceAtLocationDTO) {
return null;
ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Service At Location not found"));

servLoc.setDescription(serviceAtLocationDTO.getDescription());
servLoc.setServiceAreas(serviceAtLocationDTO.getServiceAreas().stream().map(ServiceAreaDTO::toEntity).toList());
servLoc.setContacts(serviceAtLocationDTO.getContacts().stream().map(ContactDTO::toEntity).toList());
servLoc.setPhones(serviceAtLocationDTO.getPhones().stream().map(PhoneDTO::toEntity).toList());
servLoc.setSchedules(serviceAtLocationDTO.getSchedules().stream().map(ScheduleDTO::toEntity).toList());
servLoc.setLocation(serviceAtLocationDTO.getLocation().toEntity());

ServiceAtLocation updatedServLoc = this.serviceAtLocationRepository.save(servLoc);
return updatedServLoc.toDTO();
}

@Override
public void deleteServiceAtLocation(String id) {
ServiceAtLocation servLoc = this.serviceAtLocationRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Service At Location not found."));

this.serviceAtLocationRepository.delete(servLoc);
}

private void addRelatedData(ServiceAtLocationDTO serviceAtLocationDTO) {
String id = serviceAtLocationDTO.getId();
List<AttributeDTO> attributeDTOs = this.serviceAtLocationRepository.getAttributes(id)
.stream()
.map(Attribute::toDTO)
.toList();

List<MetadataDTO> metadataDTOs = this.serviceAtLocationRepository.getMetadata(id)
.stream()
.map(Metadata::toDTO)
.toList();

serviceAtLocationDTO.getAttributes().addAll(attributeDTOs);
serviceAtLocationDTO.getMetadata().addAll(metadataDTOs);
}
}
Loading