Skip to content

Commit

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

private final CategoryServiceImpl categoryService;
private static final String URI_CATEGORIES_ID = "/{id}";
private static final String URI_CATEGORIES_FILTER = "/filter/{categoryName}";

@GetMapping
public List<CategoryDTO> getAll() {
Expand All @@ -38,6 +39,11 @@ public CategoryDTO update(@PathVariable final Long id, @RequestBody @Validated f
return categoryService.update(id, categoryDTO);
}

@GetMapping(URI_CATEGORIES_FILTER)
public List<CategoryEntity> getFilterLatsName(@PathVariable final String categoryName) {
return categoryService.getFilterCategory(categoryName);
}

@DeleteMapping(URI_CATEGORIES_ID)
public void delete(@PathVariable final Long id) {
categoryService.deleteId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
public interface CategoryService {

List<CategoryDTO> getAll();

CategoryEntity getById(Long id);

CategoryDTO getOneById(Long id);

CategoryDTO create(CategoryDTO categoryDTO);

CategoryDTO update(Long categoryId, CategoryDTO categoryDTO);

void deleteId(Long id);
List<CategoryEntity> getFilterCategory(String categoryName);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.example.backend.web.Category;

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 @@ -12,6 +18,7 @@ public class CategoryServiceImpl implements CategoryService {

private final CategoryRepository categoryRepository;
private final CategoryFactory categoryFactory;
private final EntityManager em;

@Override
public List<CategoryDTO> getAll() {
Expand Down Expand Up @@ -59,4 +66,23 @@ public void deleteId(final Long id) {
public CategoryEntity getIdCategory(final Long id) {
return categoryRepository.getReferenceById(id);
}

@Override
public List<CategoryEntity> getFilterCategory(final String categoryName) {

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();

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

Root<CategoryEntity> categoryEntityRootRoot = criteriaQuery.from(CategoryEntity.class);

Predicate categoryNamePredicate = criteriaBuilder.equal(
categoryEntityRootRoot.get("categoryName"), categoryName);

criteriaQuery.where(categoryNamePredicate);

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

return typedQuery.getResultList();
}
}

0 comments on commit 54f755d

Please sign in to comment.