Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 메뉴 추천 목록 조회(#35) #36

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading