Search in sources :

Example 1 with Category

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()));
}
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 2 with Category

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);
    }
}
Also used : CategoryDTO(com.rafaelvieira.letmebuy.dto.CategoryDTO) Category(com.rafaelvieira.letmebuy.entities.Category)

Example 3 with 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);
}
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)

Example 4 with Category

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);
}
Also used : CategoryDTO(com.rafaelvieira.letmebuy.dto.CategoryDTO) Category(com.rafaelvieira.letmebuy.entities.Category) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Category

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);
    }
}
Also used : CategoryDTO(com.rafaelvieira.letmebuy.dto.CategoryDTO) Category(com.rafaelvieira.letmebuy.entities.Category) EntityNotFoundException(javax.persistence.EntityNotFoundException) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

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