Colocado tratamento de erro ao salvar e buscar produtos no banco.
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
package br.com.stackpanel.duck_api.exception;
|
||||||
|
|
||||||
|
public class ProductInvalidException extends RuntimeException {
|
||||||
|
|
||||||
|
public ProductInvalidException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,18 +1,22 @@
|
|||||||
package br.com.stackpanel.duck_api.service.impl;
|
package br.com.stackpanel.duck_api.service.impl;
|
||||||
|
|
||||||
|
|
||||||
import br.com.stackpanel.duck_api.entity.Product;
|
import br.com.stackpanel.duck_api.entity.Product;
|
||||||
import br.com.stackpanel.duck_api.entity.dto.ProductDTO;
|
import br.com.stackpanel.duck_api.entity.dto.ProductDTO;
|
||||||
import br.com.stackpanel.duck_api.entity.mapper.ProductMapper;
|
import br.com.stackpanel.duck_api.entity.mapper.ProductMapper;
|
||||||
|
import br.com.stackpanel.duck_api.exception.ProductInvalidException;
|
||||||
import br.com.stackpanel.duck_api.repository.ProductRepository;
|
import br.com.stackpanel.duck_api.repository.ProductRepository;
|
||||||
import br.com.stackpanel.duck_api.service.ProductService;
|
import br.com.stackpanel.duck_api.service.ProductService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProductServiceImpl implements ProductService {
|
public class ProductServiceImpl implements ProductService {
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(ProductServiceImpl.class);
|
||||||
|
|
||||||
private final ProductRepository productRepository;
|
private final ProductRepository productRepository;
|
||||||
private final ProductMapper productMapper;
|
private final ProductMapper productMapper;
|
||||||
|
|
||||||
@@ -23,18 +27,35 @@ public class ProductServiceImpl implements ProductService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ProductDTO> buscarTodosProdutos() {
|
public List<ProductDTO> buscarTodosProdutos() {
|
||||||
|
try {
|
||||||
return productRepository.findAll().stream()
|
return productRepository.findAll().stream()
|
||||||
.map(productMapper::toProductDTO)
|
.map(productMapper::toProductDTO)
|
||||||
.toList();
|
.toList();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Ocorreu um erro ao buscar produtos. Erro original: {}", e.getMessage());
|
||||||
|
|
||||||
|
throw new RuntimeException("Ocorreu um erro interno ao buscar produtos.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProductDTO salvarProduto(ProductDTO dto) {
|
public ProductDTO salvarProduto(ProductDTO dto) {
|
||||||
|
try{
|
||||||
|
Product produto = productRepository.save(productMapper.toProductEntity(dto));
|
||||||
|
|
||||||
Product produto = productMapper.toProductEntity(dto);
|
log.info("Produto salvo com sucesso. ID: {}", produto.getCodigoProduto());
|
||||||
|
|
||||||
Product produtoSalvo = productRepository.save(produto);
|
return productMapper.toProductDTO(produto);
|
||||||
|
} catch (DataIntegrityViolationException e) {
|
||||||
|
String message = e.getMostSpecificCause().getMessage();
|
||||||
|
|
||||||
return productMapper.toProductDTO(produtoSalvo);
|
log.error("Erro de integridade ao salvar o produto '{}'. Motivo {}", dto.getNomeProduto(), message);
|
||||||
|
throw new ProductInvalidException("Os dados do produto sao invalidos ou ja existem no sistema.")
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Erro inesperado ao salvar produto '{}'. Erro original {}", dto.getNomeProduto(), e.getMessage());
|
||||||
|
|
||||||
|
throw new RuntimeException("Ocorreu um erro interno ao salvar o produto.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user