Search in sources :

Example 1 with ResourceNotFoundException

use of com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException in project letmebuy by rafaelrok.

the class ProductService method findById.

@Transactional(readOnly = true)
public ProductDTO findById(Long id) {
    Optional<Product> obj = productRepo.findById(id);
    Product entity = obj.orElseThrow(() -> new ResourceNotFoundException("Produto não encontrada"));
    return new ProductDTO(entity, entity.getCategories());
}
Also used : Product(com.rafaelvieira.letmebuy.entities.Product) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ResourceNotFoundException

use of com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException in project letmebuy by rafaelrok.

the class ProductService method update.

@Transactional
public ProductDTO update(Long id, ProductDTO dto) {
    try {
        // getOne salva em memoria o objeto para não acessr 2 vezes a base
        Product entity = productRepo.getOne(id);
        copyDtoToEntity(dto, entity);
        entity = productRepo.save(entity);
        return new ProductDTO(entity);
    } catch (EntityNotFoundException e) {
        throw new ResourceNotFoundException("Produto não encontrada " + id);
    }
}
Also used : Product(com.rafaelvieira.letmebuy.entities.Product) EntityNotFoundException(javax.persistence.EntityNotFoundException) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ResourceNotFoundException

use of com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException in project letmebuy by rafaelrok.

the class UserService method update.

@Transactional
public UserDTO update(Long id, UserUpdateDTO dto) {
    try {
        User entity = repository.getOne(id);
        copyDtoToEntity(dto, entity);
        entity = repository.save(entity);
        return new UserDTO(entity);
    } catch (EntityNotFoundException e) {
        throw new ResourceNotFoundException("Id not found " + id);
    }
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) EntityNotFoundException(javax.persistence.EntityNotFoundException) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ResourceNotFoundException

use of com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException in project letmebuy by rafaelrok.

the class UserService method findById.

@Transactional(readOnly = true)
public UserDTO findById(Long id) {
    authService.validateSelfOrAdmin(id);
    Optional<User> obj = repository.findById(id);
    User entity = obj.orElseThrow(() -> new ResourceNotFoundException("Entity not found"));
    return new UserDTO(entity);
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ResourceNotFoundException

use of com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException in project letmebuy by rafaelrok.

the class CategoryService method findById.

@Transactional(readOnly = true)
public CategoryDTO findById(Long id) {
    Optional<Category> obj = categoryRepo.findById(id);
    Category entity = obj.orElseThrow(() -> new ResourceNotFoundException("Categoria não encontrada"));
    return new CategoryDTO(entity);
}
Also used : CategoryDTO(com.rafaelvieira.letmebuy.dto.CategoryDTO) Category(com.rafaelvieira.letmebuy.entities.Category) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ResourceNotFoundException (com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException)6 Transactional (org.springframework.transaction.annotation.Transactional)6 EntityNotFoundException (javax.persistence.EntityNotFoundException)3 CategoryDTO (com.rafaelvieira.letmebuy.dto.CategoryDTO)2 ProductDTO (com.rafaelvieira.letmebuy.dto.ProductDTO)2 UserDTO (com.rafaelvieira.letmebuy.dto.UserDTO)2 Category (com.rafaelvieira.letmebuy.entities.Category)2 Product (com.rafaelvieira.letmebuy.entities.Product)2 User (com.rafaelvieira.letmebuy.entities.User)2