Skip to content

Commit

Permalink
Merge pull request #36 from Digital-Hana-Starbucks/feature/pay
Browse files Browse the repository at this point in the history
Feat: 메뉴 추천 목록 조회(#35)
  • Loading branch information
mummhy0811 authored May 7, 2024
2 parents d9cf978 + 9ea5a74 commit 40a2d6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ public void updateMenu(@PathVariable int menuIdx, @RequestPart(value = "dto") Me
public List<CategoryResDto> getCategoryList() {
return categoryService.getCategoryList();
}

@GetMapping("/recommendations")
public List<MenuResDto> getRecommendationList() { return menuService.getRecommendationList(); }
}
19 changes: 17 additions & 2 deletions src/main/java/com/hanaro/starbucks/service/MenuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,4 +72,20 @@ public void updateMenu(int menuIdx, MenuReqDto menuReqDto, MultipartFile img) th
menu.update(menuReqDto, url);
menuRepository.save(menu);
}

public List<MenuResDto> getRecommendationList(){
List<Menu> menus = menuRepository.findAll();

Set<Integer> idxSet = new HashSet<>(9);
List<Menu> 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());
}
}

0 comments on commit 40a2d6b

Please sign in to comment.