use of com.rafaelvieira.letmebuy.entities.Category 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.Category in project letmebuy by rafaelrok.
the class ProductService method copyDtoToEntity.
private void copyDtoToEntity(ProductDTO dto, Product entity) {
entity.setName(dto.getName());
entity.setDescription(dto.getDescription());
entity.setDate(dto.getDate());
entity.setImgUrl(dto.getImgUrl());
entity.setPrice(dto.getPrice());
entity.getCategories().clear();
for (CategoryDTO catDTO : dto.getCategories()) {
Category category = categoryRepo.getOne(catDTO.getId());
entity.getCategories().add(category);
}
}
use of com.rafaelvieira.letmebuy.entities.Category 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);
}
use of com.rafaelvieira.letmebuy.entities.Category in project letmebuy by rafaelrok.
the class CategoryService method save.
@Transactional
public CategoryDTO save(CategoryDTO dto) {
Category entity = new Category();
entity.setName(dto.getName());
entity = categoryRepo.save(entity);
return new CategoryDTO(entity);
}
use of com.rafaelvieira.letmebuy.entities.Category in project letmebuy by rafaelrok.
the class CategoryService method update.
@Transactional
public CategoryDTO update(Long id, CategoryDTO dto) {
try {
// getOne salva em memoria o objeto para não acessr 2 vezes a base
Category entity = categoryRepo.getOne(id);
entity.setName(dto.getName());
entity = categoryRepo.save(entity);
return new CategoryDTO(entity);
} catch (EntityNotFoundException e) {
throw new ResourceNotFoundException("Categoria não encontrada " + id);
}
}
Aggregations