Skip to content

Commit

Permalink
Merge pull request #38 from stevensblueprint/bug/404exception
Browse files Browse the repository at this point in the history
add ResourceNotFoundException, replace RuntimeExceptions
  • Loading branch information
miguel-merlin authored Jan 31, 2025
2 parents 1a92e80 + 9752483 commit 9f22fdf
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sarapis.orservice.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

public class ResourceNotFoundException extends ResponseStatusException {

public ResourceNotFoundException(String message) {
super(HttpStatus.NOT_FOUND, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sarapis.orservice.dto.AccessibilityDTO;
import com.sarapis.orservice.entity.Accessibility;
import com.sarapis.orservice.entity.core.Location;
import com.sarapis.orservice.exception.ResourceNotFoundException;
import com.sarapis.orservice.repository.AccessibilityRepository;
import com.sarapis.orservice.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -39,7 +40,7 @@ public List<AccessibilityDTO> getAllAccessibilities() {
@Override
public AccessibilityDTO getAccessibilityById(String accessibilityId) {
Accessibility accessibility = this.accessibilityRepository.findById(accessibilityId)
.orElseThrow(() -> new RuntimeException("Accessibility not found."));
.orElseThrow(() -> new ResourceNotFoundException("Accessibility not found."));
AccessibilityDTO accessibilityDTO = accessibility.toDTO();
this.addRelatedData(accessibilityDTO);
return accessibilityDTO;
Expand All @@ -51,7 +52,7 @@ public AccessibilityDTO createAccessibility(AccessibilityDTO accessibilityDTO) {

if (accessibilityDTO.getLocationId() != null) {
location = this.locationRepository.findById(accessibilityDTO.getLocationId())
.orElseThrow(() -> new RuntimeException("Location not found."));
.orElseThrow(() -> new ResourceNotFoundException("Location not found."));
}

Accessibility accessibility = this.accessibilityRepository.save(accessibilityDTO.toEntity(location));
Expand All @@ -67,7 +68,7 @@ public AccessibilityDTO createAccessibility(AccessibilityDTO accessibilityDTO) {
@Override
public AccessibilityDTO updateAccessibility(String accessibilityId, AccessibilityDTO accessibilityDTO) {
Accessibility accessibility = this.accessibilityRepository.findById(accessibilityId)
.orElseThrow(() -> new RuntimeException("Accessibility not found."));
.orElseThrow(() -> new ResourceNotFoundException("Accessibility not found."));

accessibility.setDescription(accessibilityDTO.getDescription());
accessibility.setDetails(accessibilityDTO.getDetails());
Expand All @@ -80,7 +81,7 @@ public AccessibilityDTO updateAccessibility(String accessibilityId, Accessibilit
@Override
public void deleteAccessibility(String accessibilityId) {
Accessibility accessibility = this.accessibilityRepository.findById(accessibilityId)
.orElseThrow(() -> new RuntimeException("Accessibility not found."));
.orElseThrow(() -> new ResourceNotFoundException("Accessibility not found."));
this.attributeService.deleteRelatedAttributes(accessibility.getId());
this.metadataService.deleteRelatedMetadata(accessibility.getId());
this.accessibilityRepository.delete(accessibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sarapis.orservice.dto.AddressDTO;
import com.sarapis.orservice.entity.Address;
import com.sarapis.orservice.entity.core.Location;
import com.sarapis.orservice.exception.ResourceNotFoundException;
import com.sarapis.orservice.repository.AddressRepository;
import com.sarapis.orservice.repository.LocationRepository;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -37,7 +38,7 @@ public List<AddressDTO> getAllAddresses() {
@Override
public AddressDTO getAddressById(String addressId) {
Address address = this.addressRepository.findById(addressId)
.orElseThrow(() -> new RuntimeException("Address not found."));
.orElseThrow(() -> new ResourceNotFoundException("Address not found."));
AddressDTO addressDTO = address.toDTO();
this.addRelatedData(addressDTO);
return addressDTO;
Expand All @@ -49,7 +50,7 @@ public AddressDTO createAddress(AddressDTO addressDTO) {

if (addressDTO.getLocationId() != null) {
location = this.locationRepository.findById(addressDTO.getLocationId())
.orElseThrow(() -> new RuntimeException("Location not found."));
.orElseThrow(() -> new ResourceNotFoundException("Location not found."));
}

Address address = this.addressRepository.save(addressDTO.toEntity(location));
Expand All @@ -65,7 +66,7 @@ public AddressDTO createAddress(AddressDTO addressDTO) {
@Override
public AddressDTO updateAddress(String addressId, AddressDTO addressDTO) {
Address address = this.addressRepository.findById(addressId)
.orElseThrow(() -> new RuntimeException("Address not found."));
.orElseThrow(() -> new ResourceNotFoundException("Address not found."));

address.setAttention(addressDTO.getAttention());
address.setAddress_1(addressDTO.getAddress_1());
Expand All @@ -84,7 +85,7 @@ public AddressDTO updateAddress(String addressId, AddressDTO addressDTO) {
@Override
public void deleteAddress(String addressId) {
Address address = this.addressRepository.findById(addressId)
.orElseThrow(() -> new RuntimeException("Address not found."));
.orElseThrow(() -> new ResourceNotFoundException("Address not found."));
this.attributeService.deleteRelatedAttributes(address.getId());
this.metadataService.deleteRelatedMetadata(address.getId());
this.addressRepository.delete(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sarapis.orservice.dto.AttributeDTO;
import com.sarapis.orservice.entity.Attribute;
import com.sarapis.orservice.exception.ResourceNotFoundException;
import com.sarapis.orservice.repository.AttributeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -37,7 +38,7 @@ public List<AttributeDTO> getRelatedAttributes(String linkId) {
@Override
public AttributeDTO getAttributeById(String attributeId) {
Attribute attribute = this.attributeRepository.findById(attributeId)
.orElseThrow(() -> new RuntimeException("Attribute not found."));
.orElseThrow(() -> new ResourceNotFoundException("Attribute not found."));
AttributeDTO attributeDTO = attribute.toDTO();
this.addRelatedData(attributeDTO);
return attributeDTO;
Expand All @@ -55,7 +56,7 @@ public AttributeDTO createAttribute(String linkId, AttributeDTO attributeDTO) {
@Override
public AttributeDTO updateAttribute(String attributeId, AttributeDTO attributeDTO) {
Attribute attribute = this.attributeRepository.findById(attributeId)
.orElseThrow(() -> new RuntimeException("Attribute not found."));
.orElseThrow(() -> new ResourceNotFoundException("Attribute not found."));

attribute.setLinkId(attributeDTO.getLinkId());
attribute.setLinkType(attributeDTO.getLinkType());
Expand All @@ -71,7 +72,7 @@ public AttributeDTO updateAttribute(String attributeId, AttributeDTO attributeDT
@Override
public void deleteAttribute(String attributeId) {
Attribute attribute = this.attributeRepository.findById(attributeId)
.orElseThrow(() -> new RuntimeException("Attribute not found."));
.orElseThrow(() -> new ResourceNotFoundException("Attribute not found."));
this.metadataService.deleteRelatedMetadata(attribute.getId());
this.attributeRepository.delete(attribute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.sarapis.orservice.entity.core.Location;
import com.sarapis.orservice.entity.core.Organization;
import com.sarapis.orservice.entity.core.ServiceAtLocation;
import com.sarapis.orservice.exception.ResourceNotFoundException;
import com.sarapis.orservice.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -49,7 +50,7 @@ public List<ContactDTO> getAllContacts() {
@Override
public ContactDTO getContactById(String contactId) {
Contact contact = this.contactRepository.findById(contactId)
.orElseThrow(() -> new RuntimeException("Contact not found."));
.orElseThrow(() -> new ResourceNotFoundException("Contact not found."));
ContactDTO contactDTO = contact.toDTO();
this.addRelatedData(contactDTO);
return contactDTO;
Expand All @@ -61,31 +62,31 @@ public ContactDTO createContact(UpsertContactDTO upsertContactDTO) {

if (upsertContactDTO.getOrganizationId() != null) {
Organization organization = this.organizationRepository.findById(upsertContactDTO.getOrganizationId())
.orElseThrow(() -> new RuntimeException("Organization not found."));
.orElseThrow(() -> new ResourceNotFoundException("Organization not found."));
organization.getContacts().add(createdContact);
this.organizationRepository.save(organization);
createdContact.setOrganization(organization);
}

if (upsertContactDTO.getServiceId() != null) {
com.sarapis.orservice.entity.core.Service service = this.serviceRepository.findById(upsertContactDTO.getServiceId())
.orElseThrow(() -> new RuntimeException("Service not found."));
.orElseThrow(() -> new ResourceNotFoundException("Service not found."));
service.getContacts().add(createdContact);
this.serviceRepository.save(service);
createdContact.setService(service);
}

if (upsertContactDTO.getServiceAtLocationId() != null) {
ServiceAtLocation serviceAtLocation = this.serviceAtLocationRepository.findById(upsertContactDTO.getServiceAtLocationId())
.orElseThrow(() -> new RuntimeException("Service at location not found."));
.orElseThrow(() -> new ResourceNotFoundException("Service at location not found."));
serviceAtLocation.getContacts().add(createdContact);
this.serviceAtLocationRepository.save(serviceAtLocation);
createdContact.setServiceAtLocation(serviceAtLocation);
}

if (upsertContactDTO.getLocationId() != null) {
Location location = this.locationRepository.findById(upsertContactDTO.getLocationId())
.orElseThrow(() -> new RuntimeException("Location not found."));
.orElseThrow(() -> new ResourceNotFoundException("Location not found."));
location.getContacts().add(createdContact);
this.locationRepository.save(location);
createdContact.setLocation(location);
Expand All @@ -98,7 +99,7 @@ public ContactDTO createContact(UpsertContactDTO upsertContactDTO) {
@Override
public ContactDTO updateContact(String contactId, ContactDTO contactDTO) {
Contact contact = this.contactRepository.findById(contactId)
.orElseThrow(() -> new RuntimeException("Contact not found."));
.orElseThrow(() -> new ResourceNotFoundException("Contact not found."));

contact.setName(contactDTO.getName());
contact.setTitle(contactDTO.getTitle());
Expand All @@ -112,7 +113,7 @@ public ContactDTO updateContact(String contactId, ContactDTO contactDTO) {
@Override
public void deleteContact(String contactId) {
Contact contact = this.contactRepository.findById(contactId)
.orElseThrow(() -> new RuntimeException("Contact not found."));
.orElseThrow(() -> new ResourceNotFoundException("Contact not found."));
this.attributeService.deleteRelatedAttributes(contact.getId());
this.metadataService.deleteRelatedMetadata(contact.getId());
this.contactRepository.delete(contact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sarapis.orservice.entity.Language;
import com.sarapis.orservice.entity.Phone;
import com.sarapis.orservice.entity.core.Location;
import com.sarapis.orservice.exception.ResourceNotFoundException;
import com.sarapis.orservice.repository.LanguageRepository;
import com.sarapis.orservice.repository.LocationRepository;
import com.sarapis.orservice.repository.PhoneRepository;
Expand Down Expand Up @@ -45,7 +46,7 @@ public List<LanguageDTO> getAllLanguages() {
@Override
public LanguageDTO getLanguageById(String languageId) {
Language language = this.languageRepository.findById(languageId)
.orElseThrow(() -> new RuntimeException("Language not found."));
.orElseThrow(() -> new ResourceNotFoundException("Language not found."));
LanguageDTO languageDTO = language.toDTO();
this.addRelatedData(languageDTO);
return languageDTO;
Expand All @@ -59,15 +60,15 @@ public LanguageDTO createLanguage(LanguageDTO languageDTO) {

if (languageDTO.getServiceId() != null) {
service = this.serviceRepository.findById(languageDTO.getServiceId())
.orElseThrow(() -> new RuntimeException("Service not found."));
.orElseThrow(() -> new ResourceNotFoundException("Service not found."));
}
if (languageDTO.getLocationId() != null) {
location = this.locationRepository.findById(languageDTO.getLocationId())
.orElseThrow(() -> new RuntimeException("Location not found."));
.orElseThrow(() -> new ResourceNotFoundException("Location not found."));
}
if (languageDTO.getPhoneId() != null) {
phone = this.phoneRepository.findById(languageDTO.getPhoneId())
.orElseThrow(() -> new RuntimeException("Phone not found."));
.orElseThrow(() -> new ResourceNotFoundException("Phone not found."));
}

Language language = this.languageRepository.save(languageDTO.toEntity(service, location, phone));
Expand All @@ -82,7 +83,7 @@ public LanguageDTO createLanguage(LanguageDTO languageDTO) {
@Override
public LanguageDTO updateLanguage(String languageId, LanguageDTO languageDTO) {
Language language = this.languageRepository.findById(languageId)
.orElseThrow(() -> new RuntimeException("Language not found."));
.orElseThrow(() -> new ResourceNotFoundException("Language not found."));

language.setName(languageDTO.getName());
language.setCode(languageDTO.getCode());
Expand All @@ -95,7 +96,7 @@ public LanguageDTO updateLanguage(String languageId, LanguageDTO languageDTO) {
@Override
public void deleteLanguage(String languageId) {
Language language = this.languageRepository.findById(languageId)
.orElseThrow(() -> new RuntimeException("Language not found."));
.orElseThrow(() -> new ResourceNotFoundException("Language not found."));
this.attributeService.deleteRelatedAttributes(language.getId());
this.metadataService.deleteRelatedMetadata(language.getId());
this.languageRepository.delete(language);
Expand Down
Loading

0 comments on commit 9f22fdf

Please sign in to comment.