Skip to content

Commit 102d814

Browse files
github-actionsdlsrks1021
authored andcommitted
chore: Java 스타일 수정
1 parent 570162c commit 102d814

File tree

4 files changed

+86
-89
lines changed

4 files changed

+86
-89
lines changed

backend/src/main/java/io/f1/backend/domain/stat/api/StatController.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ public ResponseEntity<StatPageResponse> getRankings(
3131
return ResponseEntity.ok().body(response);
3232
}
3333

34-
@LimitPageSize
35-
@GetMapping("/rankings/{nickname}")
36-
public ResponseEntity<StatPageResponse> getRankingsByNickname(
37-
@PathVariable String nickname,
38-
@PageableDefault Pageable pageable
39-
) {
40-
StatPageResponse response = statService.getRanksByNickname(nickname, pageable.getPageSize());
41-
return ResponseEntity.ok().body(response);
42-
}
34+
@LimitPageSize
35+
@GetMapping("/rankings/{nickname}")
36+
public ResponseEntity<StatPageResponse> getRankingsByNickname(
37+
@PathVariable String nickname, @PageableDefault Pageable pageable) {
38+
StatPageResponse response =
39+
statService.getRanksByNickname(nickname, pageable.getPageSize());
40+
return ResponseEntity.ok().body(response);
41+
}
4342
}

backend/src/main/java/io/f1/backend/domain/stat/app/StatService.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import io.f1.backend.domain.stat.dao.StatRepository;
66
import io.f1.backend.domain.stat.dto.StatPageResponse;
77
import io.f1.backend.domain.stat.dto.StatWithNickname;
8-
98
import io.f1.backend.global.exception.CustomException;
10-
import io.f1.backend.global.exception.errorcode.ErrorCode;
119
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
1210

13-
import java.util.Optional;
1411
import lombok.RequiredArgsConstructor;
1512

1613
import org.springframework.data.domain.Page;
@@ -28,28 +25,30 @@ public class StatService {
2825

2926
private final StatRepository statRepository;
3027

31-
@Transactional(readOnly = true)
28+
@Transactional(readOnly = true)
3229
public StatPageResponse getRanks(Pageable pageable) {
3330
Page<StatWithNickname> stats = statRepository.findWithUser(pageable);
3431
return toStatListPageResponse(stats);
3532
}
3633

37-
@Transactional(readOnly = true)
38-
public StatPageResponse getRanksByNickname(String nickname, int pageSize) {
34+
@Transactional(readOnly = true)
35+
public StatPageResponse getRanksByNickname(String nickname, int pageSize) {
3936

40-
Page<StatWithNickname> stats = statRepository.findWithUser(
41-
getPageableFromNickname(nickname, pageSize));
37+
Page<StatWithNickname> stats =
38+
statRepository.findWithUser(getPageableFromNickname(nickname, pageSize));
4239

43-
return toStatListPageResponse(stats);
44-
}
40+
return toStatListPageResponse(stats);
41+
}
4542

46-
private Pageable getPageableFromNickname(String nickname, int pageSize) {
47-
long score = statRepository.findScoreByNickname(nickname)
48-
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
43+
private Pageable getPageableFromNickname(String nickname, int pageSize) {
44+
long score =
45+
statRepository
46+
.findScoreByNickname(nickname)
47+
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
4948

50-
long rowNum = statRepository.countByScoreGreaterThan(score);
49+
long rowNum = statRepository.countByScoreGreaterThan(score);
5150

52-
int pageNumber = rowNum > 0 ? (int) (rowNum / pageSize) : 0;
53-
return PageRequest.of(pageNumber, pageSize, Sort.by(Direction.DESC, "score"));
54-
}
51+
int pageNumber = rowNum > 0 ? (int) (rowNum / pageSize) : 0;
52+
return PageRequest.of(pageNumber, pageSize, Sort.by(Direction.DESC, "score"));
53+
}
5554
}

backend/src/main/java/io/f1/backend/domain/stat/dao/StatRepository.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import io.f1.backend.domain.stat.dto.StatWithNickname;
44
import io.f1.backend.domain.stat.entity.Stat;
55

6-
import java.util.Optional;
76
import org.springframework.data.domain.Page;
87
import org.springframework.data.domain.Pageable;
98
import org.springframework.data.jpa.repository.JpaRepository;
109
import org.springframework.data.jpa.repository.Query;
1110

11+
import java.util.Optional;
12+
1213
public interface StatRepository extends JpaRepository<Stat, Long> {
1314

1415
@Query(
@@ -21,8 +22,8 @@ public interface StatRepository extends JpaRepository<Stat, Long> {
2122
""")
2223
Page<StatWithNickname> findWithUser(Pageable pageable);
2324

24-
@Query("SELECT s.score FROM Stat s WHERE s.user.nickname = :nickname")
25-
Optional<Long> findScoreByNickname(String nickname);
25+
@Query("SELECT s.score FROM Stat s WHERE s.user.nickname = :nickname")
26+
Optional<Long> findScoreByNickname(String nickname);
2627

27-
long countByScoreGreaterThan(Long score);
28+
long countByScoreGreaterThan(Long score);
2829
}

backend/src/test/java/io/f1/backend/domain/stat/StatBrowserTest.java

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package io.f1.backend.domain.stat;
22

33
import static io.f1.backend.global.exception.errorcode.CommonErrorCode.INVALID_PAGINATION;
4-
54
import static io.f1.backend.global.exception.errorcode.RoomErrorCode.PLAYER_NOT_FOUND;
5+
66
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
77
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
88
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
99

1010
import com.github.database.rider.core.api.dataset.DataSet;
1111

12-
import io.f1.backend.global.exception.errorcode.ErrorCode;
13-
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
1412
import io.f1.backend.global.template.BrowserTestTemplate;
1513

1614
import org.junit.jupiter.api.DisplayName;
@@ -90,60 +88,60 @@ void totalRankingForThreeUserWithPageSize2() throws Exception {
9088
jsonPath("$.ranks.length()").value(1));
9189
}
9290

93-
@Test
94-
@DataSet("datasets/stat/three-user-stat.yml")
95-
@DisplayName("랭킹 페이지에서 존재하지 않는 닉네임을 검색하면 예외를 발생시킨다.")
96-
void totalRankingWithUnregisteredNickname() throws Exception {
97-
// given
98-
String nickname = "UNREGISTERED";
99-
100-
// when
101-
ResultActions result = mockMvc.perform(get("/stats/rankings/" + nickname));
102-
103-
// then
104-
result.andExpectAll(
105-
status().isNotFound(), jsonPath("$.code").value(PLAYER_NOT_FOUND.getCode()));
106-
}
107-
108-
@Test
109-
@DataSet("datasets/stat/three-user-stat.yml")
110-
@DisplayName("총 유저 수가 3명이고 페이지 크기가 2일 때 1위 유저의 닉네임을 검색하면 첫 번째 페이지에 2개의 결과를 반환한다")
111-
void totalRankingForThreeUserWithFirstRankedNickname() throws Exception {
112-
// given
113-
String nickname = "USER3";
114-
115-
// when
116-
ResultActions result = mockMvc.perform(
117-
get("/stats/rankings/" + nickname).param("size", "2"));
118-
119-
// then
120-
result.andExpectAll(
121-
status().isOk(),
122-
jsonPath("$.totalPages").value(2),
123-
jsonPath("$.currentPage").value(1),
124-
jsonPath("$.totalElements").value(2),
125-
jsonPath("$.ranks.length()").value(2),
126-
jsonPath("$.ranks[0].nickname").value(nickname));
127-
}
128-
129-
@Test
130-
@DataSet("datasets/stat/three-user-stat.yml")
131-
@DisplayName("총 유저 수가 3명이고 페이지 크기가 2일 때 3위 유저의 닉네임을 검색하면 두 번째 페이지에 1개의 결과를 반환한다")
132-
void totalRankingForThreeUserWithLastRankedNickname() throws Exception {
133-
// given
134-
String nickname = "USER1";
135-
136-
// when
137-
ResultActions result = mockMvc.perform(
138-
get("/stats/rankings/" + nickname).param("size", "2"));
139-
140-
// then
141-
result.andExpectAll(
142-
status().isOk(),
143-
jsonPath("$.totalPages").value(2),
144-
jsonPath("$.currentPage").value(2),
145-
jsonPath("$.totalElements").value(1),
146-
jsonPath("$.ranks.length()").value(1),
147-
jsonPath("$.ranks[0].nickname").value(nickname));
148-
}
91+
@Test
92+
@DataSet("datasets/stat/three-user-stat.yml")
93+
@DisplayName("랭킹 페이지에서 존재하지 않는 닉네임을 검색하면 예외를 발생시킨다.")
94+
void totalRankingWithUnregisteredNickname() throws Exception {
95+
// given
96+
String nickname = "UNREGISTERED";
97+
98+
// when
99+
ResultActions result = mockMvc.perform(get("/stats/rankings/" + nickname));
100+
101+
// then
102+
result.andExpectAll(
103+
status().isNotFound(), jsonPath("$.code").value(PLAYER_NOT_FOUND.getCode()));
104+
}
105+
106+
@Test
107+
@DataSet("datasets/stat/three-user-stat.yml")
108+
@DisplayName("총 유저 수가 3명이고 페이지 크기가 2일 때 1위 유저의 닉네임을 검색하면 첫 번째 페이지에 2개의 결과를 반환한다")
109+
void totalRankingForThreeUserWithFirstRankedNickname() throws Exception {
110+
// given
111+
String nickname = "USER3";
112+
113+
// when
114+
ResultActions result =
115+
mockMvc.perform(get("/stats/rankings/" + nickname).param("size", "2"));
116+
117+
// then
118+
result.andExpectAll(
119+
status().isOk(),
120+
jsonPath("$.totalPages").value(2),
121+
jsonPath("$.currentPage").value(1),
122+
jsonPath("$.totalElements").value(2),
123+
jsonPath("$.ranks.length()").value(2),
124+
jsonPath("$.ranks[0].nickname").value(nickname));
125+
}
126+
127+
@Test
128+
@DataSet("datasets/stat/three-user-stat.yml")
129+
@DisplayName("총 유저 수가 3명이고 페이지 크기가 2일 때 3위 유저의 닉네임을 검색하면 두 번째 페이지에 1개의 결과를 반환한다")
130+
void totalRankingForThreeUserWithLastRankedNickname() throws Exception {
131+
// given
132+
String nickname = "USER1";
133+
134+
// when
135+
ResultActions result =
136+
mockMvc.perform(get("/stats/rankings/" + nickname).param("size", "2"));
137+
138+
// then
139+
result.andExpectAll(
140+
status().isOk(),
141+
jsonPath("$.totalPages").value(2),
142+
jsonPath("$.currentPage").value(2),
143+
jsonPath("$.totalElements").value(1),
144+
jsonPath("$.ranks.length()").value(1),
145+
jsonPath("$.ranks[0].nickname").value(nickname));
146+
}
149147
}

0 commit comments

Comments
 (0)