From 6890b72e446f655cfe505410c0d290108bb66eb0 Mon Sep 17 00:00:00 2001 From: Nhat-Original Date: Tue, 14 May 2024 15:16:57 +0700 Subject: [PATCH] feat: added search by query param option for menu item module --- .../spring/controller/MenuItemController.java | 5 +++-- .../spring/repository/MenuItemRepository.java | 3 +++ .../nhatoriginal/spring/service/MenuItemService.java | 10 ++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java b/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java index b8f582b..5cb9a00 100644 --- a/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java +++ b/src/main/java/com/github/nhatoriginal/spring/controller/MenuItemController.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.github.nhatoriginal.spring.constant.Endpoint; import com.github.nhatoriginal.spring.dto.menuItem.MenuItemDetailDto; @@ -19,8 +20,8 @@ public class MenuItemController { private MenuItemService menuItemService; @GetMapping(Endpoint.MenuItem.GET_ALL) - public List findAll() { - return menuItemService.findAll(); + public List findAll(@RequestParam("name") String name) { + return menuItemService.findAll(name); } @GetMapping(Endpoint.MenuItem.GET_ONE) diff --git a/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java b/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java index 1dbe36d..77317b7 100644 --- a/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java +++ b/src/main/java/com/github/nhatoriginal/spring/repository/MenuItemRepository.java @@ -3,8 +3,11 @@ import org.springframework.stereotype.Repository; import org.springframework.data.jpa.repository.JpaRepository; import com.github.nhatoriginal.spring.model.MenuItem; + +import java.util.List; import java.util.UUID; @Repository public interface MenuItemRepository extends JpaRepository { + List findByNameContaining(String name); } diff --git a/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java b/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java index 8174b37..c465338 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/MenuItemService.java @@ -18,8 +18,14 @@ public class MenuItemService { @Autowired private MenuItemRepository menuItemRepository; - public List findAll() { - List menuItems = menuItemRepository.findAll(); + public List findAll(String name) { + List menuItems; + + if (name.isEmpty()) { + menuItems = menuItemRepository.findAll(); + } else { + menuItems = menuItemRepository.findByNameContaining(name); + } return menuItems.stream().map( menuItem -> new MenuItemDto(menuItem.getId(), menuItem.getName(), menuItem.getImageUrl())).toList();