Skip to content
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
@@ -0,0 +1,37 @@
package inha.gdgoc.domain.game.controller;

import static inha.gdgoc.domain.game.controller.message.Rythm8beatScoreMessage.ADMIN_SCORES_RETRIEVED;

import inha.gdgoc.domain.game.dto.response.Rythm8beatAdminScoreResponse;
import inha.gdgoc.domain.game.service.Rythm8beatScoreService;
import inha.gdgoc.domain.user.enums.UserRole;
import inha.gdgoc.global.config.jwt.TokenProvider;
import inha.gdgoc.global.dto.response.ApiResponse;
import inha.gdgoc.global.security.AccessGuard;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/v1/admin/game/rythm8beat")
@RestController
@RequiredArgsConstructor
public class AdminRythm8beatScoreController {

private final Rythm8beatScoreService rythm8beatScoreService;
private final AccessGuard accessGuard;

@GetMapping("/scores")
public ResponseEntity<ApiResponse<List<Rythm8beatAdminScoreResponse>, Void>> getAllScores(
@AuthenticationPrincipal TokenProvider.CustomUserDetails me
) {
accessGuard.require(me, AccessGuard.AccessCondition.atLeast(UserRole.CORE));
return ResponseEntity.ok(ApiResponse.ok(
ADMIN_SCORES_RETRIEVED,
rythm8beatScoreService.getAllScores()
));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inha.gdgoc.domain.game.controller.message;

public class Rythm8beatScoreMessage {
public static final String ADMIN_SCORES_RETRIEVED = "κ΄€λ¦¬μžμš© 8bit 점수 λͺ©λ‘μ„ μ‘°νšŒν–ˆμŠ΅λ‹ˆλ‹€.";
public static final String SCORE_SUBMITTED = "μ μˆ˜κ°€ λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.";
public static final String RANKING_RETRIEVED = "λž­ν‚Ήμ„ μ‘°νšŒν–ˆμŠ΅λ‹ˆλ‹€.";
public static final String SCORES_RESET = "λͺ¨λ“  μ μˆ˜κ°€ μ΄ˆκΈ°ν™”λ˜μ—ˆμŠ΅λ‹ˆλ‹€.";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package inha.gdgoc.domain.game.dto.response;

import inha.gdgoc.domain.game.entity.Rythm8beatScore;
import java.time.Instant;
import lombok.Getter;

@Getter
public class Rythm8beatAdminScoreResponse {
private final int rank;
private final Long id;
private final String phoneNumber;
private final String nickname;
private final int score;
private final int stageReached;
private final Instant createdAt;
private final Instant updatedAt;

public Rythm8beatAdminScoreResponse(int rank, Rythm8beatScore score) {
this.rank = rank;
this.id = score.getId();
this.phoneNumber = score.getPhoneNumber();
this.nickname = score.getNickname();
this.score = score.getScore();
this.stageReached = score.getStageReached();
this.createdAt = score.getCreatedAt();
this.updatedAt = score.getUpdatedAt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface Rythm8beatScoreRepository extends JpaRepository<Rythm8beatScore

Optional<Rythm8beatScore> findByPhoneNumber(String phoneNumber);

List<Rythm8beatScore> findAllByOrderByScoreDescUpdatedAtAsc();

List<Rythm8beatScore> findTop3ByOrderByScoreDescUpdatedAtAsc();

@Query("SELECT COUNT(r) FROM Rythm8beatScore r WHERE r.score > :score")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package inha.gdgoc.domain.game.service;

import inha.gdgoc.domain.game.dto.request.Rythm8beatScoreRequest;
import inha.gdgoc.domain.game.dto.response.Rythm8beatAdminScoreResponse;
import inha.gdgoc.domain.game.dto.response.Rythm8beatRankItemResponse;
import inha.gdgoc.domain.game.dto.response.Rythm8beatRankingResponse;
import inha.gdgoc.domain.game.entity.Rythm8beatScore;
import inha.gdgoc.domain.game.repository.Rythm8beatScoreRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -56,6 +58,28 @@ public Rythm8beatRankingResponse getRanking(String phoneNumber) {
return new Rythm8beatRankingResponse(top3Response, userRank);
}

@Transactional(readOnly = true)
public List<Rythm8beatAdminScoreResponse> getAllScores() {
List<Rythm8beatScore> scores = rythm8beatScoreRepository.findAllByOrderByScoreDescUpdatedAtAsc();
List<Rythm8beatAdminScoreResponse> responses = new ArrayList<>();

Integer previousScore = null;
int previousRank = 0;

for (int index = 0; index < scores.size(); index++) {
Rythm8beatScore score = scores.get(index);
int rank = previousScore != null && previousScore == score.getScore()
? previousRank
: index + 1;

responses.add(new Rythm8beatAdminScoreResponse(rank, score));
previousScore = score.getScore();
previousRank = rank;
}

return responses;
}

public void resetAll() {
rythm8beatScoreRepository.deleteAll();
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/inha/gdgoc/global/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ public CorsConfigurationSource corsConfigurationSource() {
"https://gdgocinha.com",
"https://dev.gdgocinha.com",
"https://www.gdgocinha.com",
"https://typing-game-alpha-umber.vercel.app",
"https://api.gdgocinha.com",
"https://*.gdgocinha.com",
"https://smpringles24.github.io"
"https://*.gdgocinha.com"
));
config.setAllowedMethods(List.of("GET","POST","PUT","DELETE","OPTIONS","PATCH"));
config.setAllowedHeaders(List.of("Origin","X-Requested-With","Content-Type","Accept","Authorization"));
Expand Down
Loading