Skip to content

Commit

Permalink
Merge pull request #108 from Tech-Harbor/Bezsmertnyi
Browse files Browse the repository at this point in the history
Bezsmertnyi | Added filter productName
  • Loading branch information
Vladik-gif authored Apr 15, 2024
2 parents 54f755d + a726339 commit c48d31b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ProductController {

private final ProductServiceImpl productService;
private static final String URI_PRODUCTS_ID = "/{id}";
private static final String URI_PRODUCTS_FILTER = "/filter/{name}";
private static final String URL_DELETE_ALL = "/deleteAll";

@PostMapping(URI_PRODUCTS_ID)
Expand Down Expand Up @@ -46,6 +47,11 @@ public void deleteIdProduct(@PathVariable final Long id) {
productService.deleteIdProduct(id);
}

@GetMapping(URI_PRODUCTS_FILTER)
public List<ProductEntity> getFilterName(@PathVariable final String name) {
return productService.getFilterProductName(name);
}

@DeleteMapping(URL_DELETE_ALL)
public void deleteAllProduct() {
productService.deleteAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface ProductService {
ProductDTO getOneProduct(Long id);
ProductDTO editProduct(Long id, ProductDTO entity);
void deleteIdProduct(Long id);
List<ProductEntity> getFilterProductName(String name);
void deleteAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import com.example.backend.web.Category.CategoryService;
import com.example.backend.web.User.UserEntity;
import com.example.backend.web.User.UserService;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -14,10 +20,11 @@
@AllArgsConstructor
public class ProductServiceImpl implements ProductService {

private ProductRepository productRepository;
private UserService userService;
private ProductFactory productFactory;
private CategoryService categoryService;
private final ProductRepository productRepository;
private final CategoryService categoryService;
private final ProductFactory productFactory;
private final UserService userService;
private final EntityManager em;

@Override
public ProductDTO createProduct(final Long id, final ProductDTO product) {
Expand Down Expand Up @@ -75,6 +82,24 @@ public void deleteAll() {
productRepository.deleteAll();
}

@Override
public List<ProductEntity> getFilterProductName(final String name) {

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();

CriteriaQuery<ProductEntity> criteriaQuery = criteriaBuilder.createQuery(ProductEntity.class);

Root<ProductEntity> productEntityRoot = criteriaQuery.from(ProductEntity.class);

Predicate nameProductPredicate = criteriaBuilder.equal(productEntityRoot.get("name"), name);

criteriaQuery.where(nameProductPredicate);

TypedQuery<ProductEntity> typedQuery = em.createQuery(criteriaQuery);

return typedQuery.getResultList();
}

private ProductEntity getIdProduct(final Long id) {
return productRepository.getReferenceById(id);
}
Expand Down

0 comments on commit c48d31b

Please sign in to comment.