From 9ea5a745bc0f8b26a382fee8457a27ed0d7e4973 Mon Sep 17 00:00:00 2001 From: mummhy0811 Date: Tue, 7 May 2024 17:11:33 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=A9=94=EB=89=B4=20=EC=B6=94=EC=B2=9C?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C(#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starbucks/controller/MenuController.java | 3 +++ .../hanaro/starbucks/service/MenuService.java | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hanaro/starbucks/controller/MenuController.java b/src/main/java/com/hanaro/starbucks/controller/MenuController.java index 063c438..ce6343b 100644 --- a/src/main/java/com/hanaro/starbucks/controller/MenuController.java +++ b/src/main/java/com/hanaro/starbucks/controller/MenuController.java @@ -51,4 +51,7 @@ public void updateMenu(@PathVariable int menuIdx, @RequestPart(value = "dto") Me public List getCategoryList() { return categoryService.getCategoryList(); } + + @GetMapping("/recommendations") + public List getRecommendationList() { return menuService.getRecommendationList(); } } diff --git a/src/main/java/com/hanaro/starbucks/service/MenuService.java b/src/main/java/com/hanaro/starbucks/service/MenuService.java index 5ae4469..17cbc47 100644 --- a/src/main/java/com/hanaro/starbucks/service/MenuService.java +++ b/src/main/java/com/hanaro/starbucks/service/MenuService.java @@ -12,8 +12,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; @Service @@ -73,4 +72,20 @@ public void updateMenu(int menuIdx, MenuReqDto menuReqDto, MultipartFile img) th menu.update(menuReqDto, url); menuRepository.save(menu); } + + public List getRecommendationList(){ + List menus = menuRepository.findAll(); + + Set idxSet = new HashSet<>(9); + List menuList = new ArrayList<>(9); + + Random random = new Random(); + + while (idxSet.size() < 9) { + int idx = random.nextInt(menus.size()); + idxSet.add(idx); + menuList.add(menus.get(idx)); + } + return menuList.stream().map(MenuResDto::new).collect(Collectors.toList()); + } }