-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: 어드민 인기 메뉴 기능 구현 #119
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b0ebfc9
feat(event): event 모듈 추가
Jjiggu bfa73da
feat(event): 인기 메뉴 이벤트, 리스너 추가
Jjiggu aa8d0e4
feat(event): MenuCounterService 구현
Jjiggu 416c8a2
feat(event): PopularMenuDto 추가
Jjiggu 359182f
feat(event): PopularMenuRedisService 구현
Jjiggu dc692e6
feat(event): 인기 메뉴 관련 키 추가
Jjiggu 45fd931
feat(event): 인기 메뉴 관련 엔드 포인트 추가
Jjiggu 31468eb
refactor(Statistics): StoreRankingDto 변수 간격 수정
Jjiggu e5adca2
refactor(Order): 주문 COOKED로 변경 시 Redis에 저장하는 이벤트 추가
Jjiggu 8d05372
refactor(Statistics): 유효성 검사 추가
Jjiggu 619d8ae
fix(Statistics): N + 1 문제 해결
Jjiggu f865597
feat(Statistics): MenuCounter 예외처리 추가
Jjiggu b8ceeb1
refactor(Statistics): TTL 더 안전한 방식으로 변경
Jjiggu 704d39e
refactor(Statistics): 사용하지 않는 의존성 제거
Jjiggu 3c716de
refactor(Statistics): 생성자 매개변수 삭제
Jjiggu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...app-admin-api/src/main/java/com/nowait/applicationadmin/statistic/dto/PopularMenuDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.nowait.applicationadmin.statistic.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class PopularMenuDto { | ||
| private Long menuId; | ||
| private String menuName; | ||
| private Long soldCount; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
.../src/main/java/com/nowait/applicationadmin/statistic/service/PopularMenuRedisService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.nowait.applicationadmin.statistic.service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import com.nowait.applicationadmin.statistic.dto.PopularMenuDto; | ||
| import com.nowait.domaincorerdb.user.entity.MemberDetails; | ||
|
|
||
| public interface PopularMenuRedisService { | ||
| List<PopularMenuDto> getTodayTop5(MemberDetails memberDetails); | ||
| } |
66 changes: 66 additions & 0 deletions
66
.../java/com/nowait/applicationadmin/statistic/service/impl/PopularMenuRedisServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.nowait.applicationadmin.statistic.service.impl; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.springframework.data.redis.core.ZSetOperations; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import com.nowait.applicationadmin.statistic.dto.PopularMenuDto; | ||
| import com.nowait.applicationadmin.statistic.service.PopularMenuRedisService; | ||
| import com.nowait.common.enums.Role; | ||
| import com.nowait.domainadminrdb.statistic.exception.StatisticViewUnauthorizedException; | ||
| import com.nowait.domaincorerdb.menu.entity.Menu; | ||
| import com.nowait.domaincorerdb.menu.repository.MenuRepository; | ||
| import com.nowait.domaincorerdb.user.entity.MemberDetails; | ||
| import com.nowait.domaincorerdb.user.entity.User; | ||
| import com.nowait.domaincorerdb.user.exception.UserNotFoundException; | ||
| import com.nowait.domaincorerdb.user.repository.UserRepository; | ||
| import com.nowait.domaincoreredis.rank.service.MenuCounterService; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PopularMenuRedisServiceImpl implements PopularMenuRedisService { | ||
|
|
||
| private final MenuCounterService menuCounterService; | ||
| private final MenuRepository menuRepository; | ||
| private final UserRepository userRepository; | ||
|
|
||
| @Override | ||
| public List<PopularMenuDto> getTodayTop5(MemberDetails memberDetails) { | ||
|
|
||
| User user = userRepository.findById(memberDetails.getId()).orElseThrow(UserNotFoundException::new); | ||
| Long storeId = user.getStoreId(); | ||
|
|
||
| if (!Role.SUPER_ADMIN.equals(user.getRole()) && !user.getStoreId().equals(storeId)) { | ||
| throw new StatisticViewUnauthorizedException(); | ||
| } | ||
|
|
||
| Set<ZSetOperations.TypedTuple<String>> tuples = menuCounterService.getTopMenus(storeId, 5); | ||
|
|
||
| List<Long> menuIds = tuples.stream() | ||
| .map(tuple -> Long.parseLong(tuple.getValue())) | ||
| .toList(); | ||
|
|
||
| Map<Long, String> menuIdToNameMap = menuRepository.findAllById(menuIds) | ||
| .stream() | ||
| .collect(Collectors.toMap( | ||
| Menu::getId, | ||
| Menu::getName | ||
| )); | ||
|
|
||
|
|
||
| return tuples.stream() | ||
| .map(tuple -> { | ||
| Long menuId = Long.parseLong(tuple.getValue()); | ||
| String menuName = menuIdToNameMap.getOrDefault(menuId, "Unknown Menu"); | ||
|
|
||
| return new PopularMenuDto(menuId, menuName, tuple.getScore().longValue()); | ||
| }) | ||
| .toList(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...s/src/main/java/com/nowait/domaincoreredis/rank/exception/MenuCounterUpdateException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.nowait.domaincoreredis.rank.exception; | ||
|
|
||
| import com.nowait.common.exception.ErrorMessage; | ||
|
|
||
| public class MenuCounterUpdateException extends RuntimeException { | ||
| public MenuCounterUpdateException() { super(ErrorMessage.MENU_COUNTER_UPDATE.getMessage()); } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...redis/src/main/java/com/nowait/domaincoreredis/rank/listener/CookingCompleteListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.nowait.domaincoreredis.rank.listener; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| import com.nowait.domaincoreredis.rank.service.MenuCounterService; | ||
| import com.nowait.nowaitevent.order.event.CookingCompleteEvent; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CookingCompleteListener { | ||
| private final MenuCounterService menuCounterService; | ||
|
|
||
| @TransactionalEventListener( | ||
| classes = CookingCompleteEvent.class, | ||
| phase = TransactionPhase.AFTER_COMMIT | ||
| ) | ||
| public void onCookingComplete(CookingCompleteEvent event) { | ||
| Long storeId = event.getStoreId(); | ||
| for (CookingCompleteEvent.Item item : event.getItems()) { | ||
| menuCounterService.incrementMenuCounter( | ||
| item.getMenuId(), | ||
| storeId, | ||
| item.getQuantity() | ||
| ); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
...omain-redis/src/main/java/com/nowait/domaincoreredis/rank/service/MenuCounterService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.nowait.domaincoreredis.rank.service; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import org.springframework.data.redis.core.ZSetOperations; | ||
|
|
||
| public interface MenuCounterService { | ||
|
|
||
| void incrementMenuCounter(Long menuId, Long storeId, int qty); | ||
|
|
||
| Set<ZSetOperations.TypedTuple<String>> getTopMenus(Long storeId, int topN); | ||
| } |
61 changes: 61 additions & 0 deletions
61
...n-redis/src/main/java/com/nowait/domaincoreredis/rank/service/MenuCounterServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.nowait.domaincoreredis.rank.service; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Set; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.data.redis.core.ZSetOperations; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import com.nowait.domaincoreredis.common.util.RedisKeyUtils; | ||
| import com.nowait.domaincoreredis.rank.exception.MenuCounterUpdateException; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class MenuCounterServiceImpl implements MenuCounterService { | ||
|
|
||
| private final String KEY_FMT = RedisKeyUtils.buildMenuKey(); | ||
| private final DateTimeFormatter DTF = RedisKeyUtils.buildMenuDateKey(); | ||
| private final RedisTemplate<String, String> redis; | ||
|
|
||
| @Override | ||
| public void incrementMenuCounter(Long menuId, Long storeId, int qty) { | ||
|
|
||
| try { | ||
| String date = LocalDate.now().format(DTF); | ||
| String key = String.format(KEY_FMT, storeId, date); | ||
|
|
||
| redis.opsForZSet().incrementScore(key, menuId.toString(), qty); | ||
|
|
||
| Long expirationTime = redis.getExpire(key); | ||
| if (expirationTime == null || expirationTime < 0) { | ||
| long secondsUntilMidnight = Duration.between( | ||
| LocalDateTime.now(), | ||
| LocalDate.now().plusDays(1).atStartOfDay() | ||
| ).getSeconds(); | ||
|
|
||
| redis.expire(key, Duration.ofSeconds(secondsUntilMidnight)); | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| log.error("Failed to increment menu counter for menuId: {}, storeId: {}", + menuId, storeId, e); | ||
| throw new MenuCounterUpdateException(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Set<ZSetOperations.TypedTuple<String>> getTopMenus(Long storeId, int topN) { | ||
| String date = LocalDate.now().format(DTF); | ||
| String key = String.format(KEY_FMT, storeId, date); | ||
|
|
||
| return redis.opsForZSet().reverseRangeWithScores(key, 0, topN - 1); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.