Search in sources :

Example 1 with Product

use of com.rafaelvieira.letmebuy.entities.Product in project letmebuy by rafaelrok.

the class ProductRepositoryTests method saveShouldPersistWithAutoincrementWhenIdIsNull.

@Test
public void saveShouldPersistWithAutoincrementWhenIdIsNull() {
    Product product = Factory.createProduct();
    product.setId(null);
    product = repository.save(product);
    Optional<Product> result = repository.findById(product.getId());
    Assertions.assertNotNull(product.getId());
    Assertions.assertEquals(countTotalProducts + 1L, product.getId());
    Assertions.assertTrue(result.isPresent());
    Assertions.assertSame(result.get(), product);
}
Also used : Product(com.rafaelvieira.letmebuy.entities.Product) Test(org.junit.jupiter.api.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 2 with Product

use of com.rafaelvieira.letmebuy.entities.Product 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 3 with Product

use of com.rafaelvieira.letmebuy.entities.Product in project letmebuy by rafaelrok.

the class ProductService method findAllPaged.

/*
    @Transactional(readOnly = true)
    public Page<ProductDTO> findAllPaged(Pageable pageable) {
        Page<Product> list = productRepo.findAll(pageable);
        return list.map(x -> new ProductDTO(x));
    }
     */
@Transactional(readOnly = true)
public Page<ProductDTO> findAllPaged(Long categoryId, String name, Pageable pageable) {
    List<Category> categories = (categoryId == 0) ? null : Arrays.asList(categoryRepo.getOne(categoryId));
    Page<Product> page = productRepo.find(categories, name, pageable);
    productRepo.findProductsWithCategories(page.getContent());
    return page.map(x -> new ProductDTO(x, x.getCategories()));
}
Also used : Category(com.rafaelvieira.letmebuy.entities.Category) Product(com.rafaelvieira.letmebuy.entities.Product) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Product

use of com.rafaelvieira.letmebuy.entities.Product 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 5 with Product

use of com.rafaelvieira.letmebuy.entities.Product in project letmebuy by rafaelrok.

the class ProductService method save.

@Transactional
public ProductDTO save(ProductDTO dto) {
    Product entity = new Product();
    copyDtoToEntity(dto, entity);
    entity = productRepo.save(entity);
    return new ProductDTO(entity);
}
Also used : Product(com.rafaelvieira.letmebuy.entities.Product) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Product (com.rafaelvieira.letmebuy.entities.Product)6 ProductDTO (com.rafaelvieira.letmebuy.dto.ProductDTO)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Category (com.rafaelvieira.letmebuy.entities.Category)2 ResourceNotFoundException (com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 Test (org.junit.jupiter.api.Test)1 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)1