Search in sources :

Example 1 with CategoryDTO

use of com.rafaelvieira.letmebuy.dto.CategoryDTO 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 2 with CategoryDTO

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

use of com.rafaelvieira.letmebuy.dto.CategoryDTO 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 4 with CategoryDTO

use of com.rafaelvieira.letmebuy.dto.CategoryDTO 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

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