Skip to content

Commit

Permalink
Merge pull request #33 from Nhat-Original/feature/search-menu-item
Browse files Browse the repository at this point in the history
feat: added search by query param option for menu item module
  • Loading branch information
Nhat-Original authored May 14, 2024
2 parents 7ada203 + 6890b72 commit 2ca3aa6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,8 +20,8 @@ public class MenuItemController {
private MenuItemService menuItemService;

@GetMapping(Endpoint.MenuItem.GET_ALL)
public List<MenuItemDto> findAll() {
return menuItemService.findAll();
public List<MenuItemDto> findAll(@RequestParam("name") String name) {
return menuItemService.findAll(name);
}

@GetMapping(Endpoint.MenuItem.GET_ONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MenuItem, UUID> {
List<MenuItem> findByNameContaining(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ public class MenuItemService {
@Autowired
private MenuItemRepository menuItemRepository;

public List<MenuItemDto> findAll() {
List<MenuItem> menuItems = menuItemRepository.findAll();
public List<MenuItemDto> findAll(String name) {
List<MenuItem> 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();
Expand Down

0 comments on commit 2ca3aa6

Please sign in to comment.