diff --git a/src/main/java/com/example/backend/web/Category/CategoryController.java b/src/main/java/com/example/backend/web/Category/CategoryController.java index cc2833e1..16940480 100644 --- a/src/main/java/com/example/backend/web/Category/CategoryController.java +++ b/src/main/java/com/example/backend/web/Category/CategoryController.java @@ -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 getAll() { @@ -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 getFilterLatsName(@PathVariable final String categoryName) { + return categoryService.getFilterCategory(categoryName); + } + @DeleteMapping(URI_CATEGORIES_ID) public void delete(@PathVariable final Long id) { categoryService.deleteId(id); diff --git a/src/main/java/com/example/backend/web/Category/CategoryService.java b/src/main/java/com/example/backend/web/Category/CategoryService.java index 16de7145..14d3ae55 100644 --- a/src/main/java/com/example/backend/web/Category/CategoryService.java +++ b/src/main/java/com/example/backend/web/Category/CategoryService.java @@ -4,14 +4,10 @@ public interface CategoryService { List getAll(); - CategoryEntity getById(Long id); - CategoryDTO getOneById(Long id); - CategoryDTO create(CategoryDTO categoryDTO); - CategoryDTO update(Long categoryId, CategoryDTO categoryDTO); - void deleteId(Long id); + List getFilterCategory(String categoryName); } \ No newline at end of file diff --git a/src/main/java/com/example/backend/web/Category/CategoryServiceImpl.java b/src/main/java/com/example/backend/web/Category/CategoryServiceImpl.java index 5903aa75..8a990ce4 100644 --- a/src/main/java/com/example/backend/web/Category/CategoryServiceImpl.java +++ b/src/main/java/com/example/backend/web/Category/CategoryServiceImpl.java @@ -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; @@ -12,6 +18,7 @@ public class CategoryServiceImpl implements CategoryService { private final CategoryRepository categoryRepository; private final CategoryFactory categoryFactory; + private final EntityManager em; @Override public List getAll() { @@ -59,4 +66,23 @@ public void deleteId(final Long id) { public CategoryEntity getIdCategory(final Long id) { return categoryRepository.getReferenceById(id); } + + @Override + public List getFilterCategory(final String categoryName) { + + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(CategoryEntity.class); + + Root categoryEntityRootRoot = criteriaQuery.from(CategoryEntity.class); + + Predicate categoryNamePredicate = criteriaBuilder.equal( + categoryEntityRootRoot.get("categoryName"), categoryName); + + criteriaQuery.where(categoryNamePredicate); + + TypedQuery typedQuery = em.createQuery(criteriaQuery); + + return typedQuery.getResultList(); + } } \ No newline at end of file