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);
}
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());
}
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()));
}
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);
}
}
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);
}
Aggregations