Skip to content

Commit

Permalink
Merge pull request #335 from vinho-notas/VIN-397-Refatorar-as-excecoe…
Browse files Browse the repository at this point in the history
…s-de-todo-o-projeto-seguindo-o-padrao-do-pass-in

Vin 397 refatorar as excecoes de todo o projeto seguindo o padrao do pass in
  • Loading branch information
vanderleik committed Apr 7, 2024
2 parents 65b1389 + 53a9b15 commit 84bae28
Show file tree
Hide file tree
Showing 87 changed files with 638 additions and 251 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.vinhonotas.avaliacao.application.converters.PointScaleConverter;
import com.vinhonotas.avaliacao.application.services.PointScaleService;
import com.vinhonotas.avaliacao.application.services.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.entities.PointScaleEntity;
import com.vinhonotas.avaliacao.domain.entities.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.entities.exceptions.PointScaleNotFoundException;
import com.vinhonotas.avaliacao.infraestructure.PointScaleRepository;
import com.vinhonotas.avaliacao.interfaces.dtos.inputs.PointScaleInputDTO;
import com.vinhonotas.avaliacao.utils.MessagesConstants;
Expand Down Expand Up @@ -42,15 +43,16 @@ public List<PointScaleEntity> getAll() {
List<PointScaleEntity> list = pointScaleRepository.findAll();
if (list.isEmpty()) {
log.error("getAll :: Ocorreu um erro ao buscar as avaliações: {}", MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND);
throw new BadRequestException(MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND);
throw new PointScaleNotFoundException(MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND);
}
return list;
}

@Override
public PointScaleEntity getById(UUID id) {
log.info("getById :: Buscando uma avaliação pelo id: {}", id.toString());
return pointScaleRepository.findById(id).orElseThrow(() -> new BadRequestException(MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND));
return pointScaleRepository.findById(id).orElseThrow(() ->
new PointScaleNotFoundException(MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND));
}

@Override
Expand All @@ -73,7 +75,7 @@ public void delete(UUID id) {
Optional<PointScaleEntity> pointScale = pointScaleRepository.findById(id);
if (pointScale.isEmpty()) {
log.error("delete :: Ocorreu um erro ao deletar a avaliação: {}", MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND);
throw new BadRequestException(MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND);
throw new PointScaleNotFoundException(MessagesConstants.ERROR_POINT_SCALE_NOT_FOUND);
}
try {
pointScaleRepository.deleteById(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vinhonotas.avaliacao.configuration;

import com.vinhonotas.avaliacao.domain.entities.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.entities.exceptions.PointScaleNotFoundException;
import com.vinhonotas.avaliacao.interfaces.dtos.general.ErrorResponseDTO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class PointScaleExceptionHandler {

@ExceptionHandler(PointScaleNotFoundException.class)
public ResponseEntity<ErrorResponseDTO> handlePointScaleNotFoundException(PointScaleNotFoundException exception) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponseDTO(exception.getMessage()));
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponseDTO> handleBadRequestException(BadRequestException exception) {
return ResponseEntity.badRequest().body(new ErrorResponseDTO(exception.getMessage()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vinhonotas.avaliacao.domain.entities.exceptions;

public class BadRequestException extends RuntimeException {

public BadRequestException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vinhonotas.avaliacao.domain.entities.exceptions;

public class PointScaleNotFoundException extends RuntimeException {

public PointScaleNotFoundException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.vinhonotas.avaliacao.interfaces.dtos.general;

public record ErrorResponseDTO(String message) {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.vinhonotas.avaliacao.application.services.impl;

import com.vinhonotas.avaliacao.application.converters.PointScaleConverter;
import com.vinhonotas.avaliacao.application.services.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.entities.PointScaleEntity;
import com.vinhonotas.avaliacao.domain.entities.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.enums.EnumPointScale;
import com.vinhonotas.avaliacao.infraestructure.PointScaleRepository;
import com.vinhonotas.avaliacao.interfaces.dtos.inputs.PointScaleInputDTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vinhonotas.avaliacao.application.converters.PointScaleConverter;
import com.vinhonotas.avaliacao.application.services.PointScaleService;
import com.vinhonotas.avaliacao.application.services.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.entities.PointScaleEntity;
import com.vinhonotas.avaliacao.domain.entities.exceptions.BadRequestException;
import com.vinhonotas.avaliacao.domain.enums.EnumPointScale;
import com.vinhonotas.avaliacao.interfaces.dtos.inputs.PointScaleInputDTO;
import com.vinhonotas.avaliacao.interfaces.dtos.outputs.PointScaleOutputDTO;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.vinhonotas.cadastro.application.services.impl;

import com.vinhonotas.cadastro.application.converters.AddressConverter;
import com.vinhonotas.cadastro.application.converters.CountryConverter;
import com.vinhonotas.cadastro.application.converters.StateConverter;
import com.vinhonotas.cadastro.application.services.AddressService;
import com.vinhonotas.cadastro.application.services.exceptions.BadRequestException;
import com.vinhonotas.cadastro.domain.entities.AddressEntity;
import com.vinhonotas.cadastro.domain.entities.CountryEntity;
import com.vinhonotas.cadastro.domain.entities.StateEntity;
import com.vinhonotas.cadastro.domain.entities.exceptions.AddressNotFoundException;
import com.vinhonotas.cadastro.domain.entities.exceptions.BadRequestException;
import com.vinhonotas.cadastro.infrastructure.AddressRepository;
import com.vinhonotas.cadastro.infrastructure.CountryRepository;
import com.vinhonotas.cadastro.infrastructure.StateRepository;
Expand All @@ -28,8 +27,6 @@
public class AddressServiceImpl implements AddressService {

private final AddressConverter addressConverter;
private final StateConverter stateConverter;
private final CountryConverter countryConverter;
private final AddressRepository addressRepository;
private final StateRepository stateRepository;
private final CountryRepository countryRepository;
Expand All @@ -45,7 +42,7 @@ public AddressEntity create(AddressInputDTO addressInputDTO) {
AddressEntity addressEntity = addressConverter.convertToEntity(addressInputDTO);
addressEntity.setUf(state);
addressEntity.setCountry(country);
log.info("Endereço a ser salvo: {}", addressEntity.toString());
log.info("Endereço a ser salvo: {}", addressEntity);

return addressRepository.save(addressEntity);
} catch (Exception e) {
Expand Down Expand Up @@ -74,7 +71,7 @@ public List<AddressEntity> getAll() {
List<AddressEntity> addressList = addressRepository.findAll();
if (addressList.isEmpty()) {
log.error("getAll :: Ocorreu um erro ao buscar os endereços: {}", MessagesConstants.ADDRESS_NOT_FOUND);
throw new BadRequestException(MessagesConstants.ADDRESS_NOT_FOUND);
throw new AddressNotFoundException(MessagesConstants.ADDRESS_NOT_FOUND);
}
return addressList;
}
Expand All @@ -83,7 +80,7 @@ public List<AddressEntity> getAll() {
public AddressEntity getById(UUID id) {
log.info("getById :: Buscando endereço pelo id: {}", id.toString());
return addressRepository.findById(id)
.orElseThrow(() -> new BadRequestException(MessagesConstants.ADDRESS_NOT_FOUND));
.orElseThrow(() -> new AddressNotFoundException(MessagesConstants.ADDRESS_NOT_FOUND));
}

@Override
Expand Down Expand Up @@ -114,7 +111,7 @@ public void delete(UUID id) {
log.info("Endereço encontrado: {}", address.toString());
if (address.isEmpty()) {
log.error("delete :: Ocorreu um erro ao deletar o endereço: {}", MessagesConstants.ADDRESS_NOT_FOUND);
throw new BadRequestException(MessagesConstants.ADDRESS_NOT_FOUND);
throw new AddressNotFoundException(MessagesConstants.ADDRESS_NOT_FOUND);
}
try {
addressRepository.deleteById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.vinhonotas.cadastro.application.converters.CountryConverter;
import com.vinhonotas.cadastro.application.services.CountryService;
import com.vinhonotas.cadastro.application.services.exceptions.BadRequestException;
import com.vinhonotas.cadastro.domain.entities.CountryEntity;
import com.vinhonotas.cadastro.domain.entities.exceptions.BadRequestException;
import com.vinhonotas.cadastro.domain.entities.exceptions.CountryAlreadyExistsException;
import com.vinhonotas.cadastro.domain.entities.exceptions.CountryNotFoundException;
import com.vinhonotas.cadastro.infrastructure.CountryRepository;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.CountryInputDTO;
import com.vinhonotas.cadastro.utils.MessagesConstants;
Expand Down Expand Up @@ -32,7 +34,7 @@ public CountryEntity create(CountryInputDTO countryInputDTO) {
CountryEntity entity = countryRepository.findByCountryName(countryInputDTO.getCountryName());
if (Objects.nonNull(entity)) {
log.error("create :: Ocorreu um erro: {}", MessagesConstants.COUNTRY_ALREADY_EXISTS);
throw new BadRequestException(MessagesConstants.COUNTRY_ALREADY_EXISTS);
throw new CountryAlreadyExistsException(MessagesConstants.COUNTRY_ALREADY_EXISTS);
}
try {
CountryEntity countryEntity = countryConverter.convertToEntity(countryInputDTO);
Expand All @@ -49,7 +51,7 @@ public List<CountryEntity> getAll() {
List<CountryEntity> entityList = countryRepository.findAll();
if (entityList.isEmpty()) {
log.error("getAll :: Ocorreu um erro ao buscar os países: {}", MessagesConstants.COUNTRIES_NOT_FOUND);
throw new BadRequestException(MessagesConstants.COUNTRIES_NOT_FOUND);
throw new CountryNotFoundException(MessagesConstants.COUNTRIES_NOT_FOUND);
}
return entityList;
}
Expand All @@ -58,7 +60,7 @@ public List<CountryEntity> getAll() {
public CountryEntity getById(UUID id) {
log.info("getById :: Buscando país pelo id: {}", id.toString());
return countryRepository.findById(id)
.orElseThrow(() -> new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND));
.orElseThrow(() -> new CountryNotFoundException(MessagesConstants.COUNTRY_NOT_FOUND));
}

@Override
Expand All @@ -67,7 +69,7 @@ public CountryEntity getByName(String name) {
CountryEntity country = countryRepository.findByCountryName(name);
if (Objects.isNull(country)) {
log.error("getByName :: Ocorreu um erro: {}", MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + name);
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + name);
throw new CountryNotFoundException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + name);
}
return country;
}
Expand All @@ -78,7 +80,7 @@ public List<CountryEntity> getByContinent(String continent) {
List<CountryEntity> entityList = countryRepository.findByContinentName(continent);
if (Objects.isNull(entityList) || entityList.isEmpty()) {
log.error("getByContinent :: Ocorreu um erro ao buscar os países: {}", MessagesConstants.COUNTRY_NOT_FOUND_WITH_CONTINENT + continent);
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_CONTINENT + continent);
throw new CountryNotFoundException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_CONTINENT + continent);
}
return entityList;
}
Expand All @@ -104,7 +106,7 @@ public void delete(UUID id) {
Optional<CountryEntity> entity = countryRepository.findById(id);
if (entity.isEmpty()) {
log.error("delete :: Ocorreu um erro: {}", MessagesConstants.COUNTRY_NOT_FOUND);
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND);
throw new CountryNotFoundException(MessagesConstants.COUNTRY_NOT_FOUND);
}
try {
countryRepository.deleteById(id);
Expand Down
Loading

0 comments on commit 84bae28

Please sign in to comment.