use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.
the class ProductControllerIT method updateShouldReturnProductDTOWhenIdExists.
@Test
public void updateShouldReturnProductDTOWhenIdExists() throws Exception {
String accessToken = tokenUtil.obtainAccessToken(mockMvc, username, password);
ProductDTO productDTO = Factory.createProductDTO();
String jsonBody = objectMapper.writeValueAsString(productDTO);
String expectedName = productDTO.getName();
String expectedDescription = productDTO.getDescription();
ResultActions result = mockMvc.perform(put("/products/{id}", existingId).header("Authorization", "Bearer " + accessToken).content(jsonBody).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON));
result.andExpect(status().isOk());
result.andExpect(jsonPath("$.id").value(existingId));
result.andExpect(jsonPath("$.name").value(expectedName));
result.andExpect(jsonPath("$.description").value(expectedDescription));
}
use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.
the class ProductServiceIT method findAllPagedShouldReturnEmptyPageWhenPageDoesNotExist.
@Test
public void findAllPagedShouldReturnEmptyPageWhenPageDoesNotExist() {
PageRequest pageRequest = PageRequest.of(50, 10);
Page<ProductDTO> result = service.findAllPaged(0L, "", pageRequest);
Assertions.assertTrue(result.isEmpty());
}
use of com.rafaelvieira.letmebuy.dto.ProductDTO 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.dto.ProductDTO 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.dto.ProductDTO 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);
}
}
Aggregations