-
Notifications
You must be signed in to change notification settings - Fork 3
[feature] 동아리 클릭 게임 API 추가 #1470
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
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
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/moadong/club/controller/ClubClickController.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,40 @@ | ||
| package moadong.club.controller; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.RequiredArgsConstructor; | ||
| import moadong.club.payload.request.ClubClickRequest; | ||
| import moadong.club.payload.response.ClubClickRankingResponse; | ||
| import moadong.club.payload.response.ClubClickResponse; | ||
| import moadong.club.service.ClubClickService; | ||
| import moadong.global.payload.Response; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/game") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "Game", description = "동아리 클릭 게임 통계") | ||
| public class ClubClickController { | ||
|
|
||
| private final ClubClickService clubClickService; | ||
|
|
||
| @PostMapping("/click") | ||
| @Operation(summary = "동아리 클릭 기록", description = "자유 입력 clubName별 클릭 수를 누적합니다. 실제 동아리 DB와 무관합니다.") | ||
| public ResponseEntity<?> recordClick(@RequestBody ClubClickRequest request) { | ||
| ClubClickResponse result = clubClickService.recordClick(request.clubName()); | ||
| return Response.ok(result); | ||
| } | ||
|
|
||
| @GetMapping("/ranking") | ||
| @Operation(summary = "동아리 클릭 랭킹 조회", | ||
| description = "클릭 수 기준 내림차순 랭킹을 반환합니다. resetAt은 다음 자정(KST) 시간입니다.") | ||
| public ResponseEntity<?> getRanking() { | ||
| ClubClickRankingResponse result = clubClickService.getRanking(); | ||
| return Response.ok(result); | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/moadong/club/payload/request/ClubClickRequest.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,4 @@ | ||
| package moadong.club.payload.request; | ||
|
|
||
| public record ClubClickRequest(String clubName, String ctAt) { | ||
| } |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/moadong/club/payload/response/ClubClickRankingResponse.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,9 @@ | ||
| package moadong.club.payload.response; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record ClubClickRankingResponse(List<ClubRankItem> clubs, String resetAt) { | ||
|
|
||
| public record ClubRankItem(int rank, String clubName, long clickCount) { | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/moadong/club/payload/response/ClubClickResponse.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,4 @@ | ||
| package moadong.club.payload.response; | ||
|
|
||
| public record ClubClickResponse(String clubName, long clickCount) { | ||
| } |
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/moadong/club/service/ClubClickScheduler.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,30 @@ | ||
| package moadong.club.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| @ConditionalOnProperty(name = "scheduling.enabled", havingValue = "true", matchIfMissing = true) | ||
| public class ClubClickScheduler { | ||
|
|
||
| private final StringRedisTemplate stringRedisTemplate; | ||
|
|
||
| @Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul") | ||
| @SchedulerLock(name = "ClubClickReset", lockAtMostFor = "1m", lockAtLeastFor = "1s") | ||
| public void resetDailyClicks() { | ||
| Set<String> keys = stringRedisTemplate.keys(ClubClickService.CLICK_KEY_PATTERN); | ||
| if (keys != null && !keys.isEmpty()) { | ||
| stringRedisTemplate.delete(keys); | ||
| } | ||
| log.info("동아리 클릭 수 초기화 완료"); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
backend/src/main/java/moadong/club/service/ClubClickService.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,68 @@ | ||
| package moadong.club.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import moadong.club.payload.response.ClubClickRankingResponse; | ||
| import moadong.club.payload.response.ClubClickRankingResponse.ClubRankItem; | ||
| import moadong.club.payload.response.ClubClickResponse; | ||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ClubClickService { | ||
|
seongwon030 marked this conversation as resolved.
|
||
|
|
||
| static final String CLICK_KEY_PREFIX = "club:click:"; | ||
| static final String CLICK_KEY_PATTERN = CLICK_KEY_PREFIX + "*"; | ||
| private static final ZoneId KST = ZoneId.of("Asia/Seoul"); | ||
|
|
||
| private final StringRedisTemplate stringRedisTemplate; | ||
|
|
||
| public ClubClickResponse recordClick(String clubName) { | ||
| String key = CLICK_KEY_PREFIX + clubName; | ||
| Long clickCount = stringRedisTemplate.opsForValue().increment(key); | ||
| return new ClubClickResponse(clubName, clickCount != null ? clickCount : 0L); | ||
| } | ||
|
|
||
| public ClubClickRankingResponse getRanking() { | ||
| Set<String> keys = stringRedisTemplate.keys(CLICK_KEY_PATTERN); | ||
|
seongwon030 marked this conversation as resolved.
|
||
| List<ClubRankItem> ranked = new ArrayList<>(); | ||
|
|
||
| if (keys != null && !keys.isEmpty()) { | ||
| List<ClubRankItem> unsorted = new ArrayList<>(); | ||
| for (String key : keys) { | ||
| String clubName = key.substring(CLICK_KEY_PREFIX.length()); | ||
| String raw = stringRedisTemplate.opsForValue().get(key); | ||
| long count = raw != null ? Long.parseLong(raw) : 0L; | ||
| unsorted.add(new ClubRankItem(0, clubName, count)); | ||
| } | ||
|
|
||
| unsorted.sort(Comparator.comparingLong(ClubRankItem::clickCount).reversed()); | ||
|
|
||
| AtomicInteger rank = new AtomicInteger(1); | ||
| for (ClubRankItem item : unsorted) { | ||
| ranked.add(new ClubRankItem(rank.getAndIncrement(), item.clubName(), item.clickCount())); | ||
| } | ||
| } | ||
|
|
||
| return new ClubClickRankingResponse(ranked, nextMidnightKst()); | ||
| } | ||
|
|
||
| public String nextMidnightKst() { | ||
| ZonedDateTime nextMidnight = ZonedDateTime.now(KST) | ||
| .toLocalDate() | ||
| .plusDays(1) | ||
| .atStartOfDay(KST); | ||
| return nextMidnight.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
| } | ||
| } | ||
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.