Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {


@Query("select p from Product p where p.productCategory.id=:categoryId")
Page<Product> getByProductCategory(@Param("categoryId") Integer categoryId , Pageable pageable);

// @Query(value = "select p from Product p left join fetch ProductCategory ")
// Page<Product> findAll(Pageable pageable);


@Query("select p from Product p where p.name=:productName")
Optional<Product> findByProductName( @Param("productName") String productName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.rms.domain.core.ProductCategory;
import com.rms.rest.dto.ProductDto;
import com.rms.rest.dto.common.PaginatedResultDto;
import com.rms.rest.exception.ErrorCodes;
import com.rms.rest.exception.ResourceNotFoundException;
import com.rms.rest.exception.ResourceRelatedException;
import com.rms.rest.exception.Response;
import com.rms.rest.exception.*;
import com.rms.rest.modelmapper.ProductMapper;
import com.rms.rest.modelmapper.common.PaginationMapper;
import com.rms.service.ProductCategoryService;
Expand All @@ -19,6 +16,7 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
@AllArgsConstructor
Expand Down Expand Up @@ -55,9 +53,13 @@ public ResponseEntity<?> getByProductCategory(Integer catId ,Integer page, Integ
}

public ResponseEntity<?> save(ProductDto productDto) {
Optional<Product> existProductName = productService.getByProductName(productDto.getName());
if (existProductName.isPresent()) {
throw new ResourceAlreadyExistsException(Product.class.getSimpleName(), "Product Name ", productDto.getName(), ErrorCodes.DUPLICATE_RESOURCE.getCode());
}

if (productDto.getProductCategoryDto() != null) {
productCategoryService.getById(productDto.getProductCategoryDto().getId())
.orElseThrow(() -> new ResourceNotFoundException(ProductCategory.class.getSimpleName(), productDto.getProductCategoryDto().getId()));
productCategoryService.getById(productDto.getProductCategoryDto().getId()).orElseThrow(() -> new ResourceNotFoundException(ProductCategory.class.getSimpleName(), productDto.getProductCategoryDto().getId()));
}
Product product = mapper.toEntity(productDto);
productService.save(product);
Expand All @@ -66,11 +68,13 @@ public ResponseEntity<?> save(ProductDto productDto) {
}

public ResponseEntity<?> update(ProductDto productDto, Integer id) {
Product product = productService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(Product.class.getSimpleName(), id));
Product product = productService.getById(id).orElseThrow(() -> new ResourceNotFoundException(Product.class.getSimpleName(), id));
Optional<Product> existProductName = productService.getByProductName(productDto.getName());
if (existProductName.isPresent()) {
throw new ResourceAlreadyExistsException(Product.class.getSimpleName(), "Product Name ", productDto.getName(), ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
if (productDto.getProductCategoryDto() != null) {
ProductCategory productCategory = productCategoryService.getById(productDto.getProductCategoryDto().getId())
.orElseThrow(() -> new ResourceNotFoundException(ProductCategory.class.getSimpleName(), productDto.getProductCategoryDto().getId()));
ProductCategory productCategory = productCategoryService.getById(productDto.getProductCategoryDto().getId()).orElseThrow(() -> new ResourceNotFoundException(ProductCategory.class.getSimpleName(), productDto.getProductCategoryDto().getId()));
product.setProductCategory(productCategory);
}
mapper.updateEntityFromDto(productDto, product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public Page<Product> getAll(Integer page , Integer size) {
public Page<Product> getByProductCategory(Integer catId ,Integer page , Integer size ) {
return productRepository.getByProductCategory(catId ,PageRequest.of(page,size));
}
public Optional<Product> getByProductName(String productName) {
return productRepository.findByProductName(productName);
}


public Product save(Product product) {
return productRepository.save(product);
Expand Down