Skip to content

Commit

Permalink
refactor: 바이옴 포켓몬 정렬 적용 (쿼리 스트링 추가)
Browse files Browse the repository at this point in the history
  • Loading branch information
jongmee committed Oct 13, 2024
1 parent 7548b68 commit 2e292e0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.pokerogue.helper.biome.controller;

import com.pokerogue.helper.biome.dto.BiomeResponse;
import static com.pokerogue.helper.biome.service.NativePokemonComparator.ASCENDING;
import static com.pokerogue.helper.biome.service.NativePokemonComparator.DESCENDING;

import com.pokerogue.helper.biome.dto.BiomeDetailResponse;
import com.pokerogue.helper.biome.dto.BiomeResponse;
import com.pokerogue.helper.biome.service.BiomeService;
import com.pokerogue.helper.util.dto.ApiResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
Expand All @@ -24,14 +28,17 @@ public ApiResponse<List<BiomeResponse>> biomeList() {
}

@GetMapping("/api/v1/biome/{id}")
public ApiResponse<BiomeDetailResponse> biomeDetails(@PathVariable("id") String id) {
public ApiResponse<BiomeDetailResponse> biomeDetails(@PathVariable("id") String id,
@RequestParam(value = "boss", defaultValue = DESCENDING) String bossPokemonOrder,
@RequestParam(value = "wild", defaultValue = ASCENDING) String wildPokemonOrder) {
log.info(
"---- URI : {}, Param : {}, ThreadName : {}",
"/api/v1/biome/{id}",
id,
Thread.currentThread().getName()
);

return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.", biomeService.findBiome(id));
return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.",
biomeService.findBiome(id, bossPokemonOrder, wildPokemonOrder));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,34 @@ public List<BiomeResponse> findBiomes() {
.toList();
}

public BiomeDetailResponse findBiome(String id) {
public BiomeDetailResponse findBiome(String id, String bossPokemonOrder, String wildPokemonOrder) {
Biome biome = biomeRepository.findById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.BIOME_NOT_FOUND));

return BiomeDetailResponse.of(
biome,
s3Service.getBiomeImageFromS3(biome.getId()),
getWildPokemons(biome.getNativePokemons()),
getBossPokemons(biome.getNativePokemons()),
getWildPokemons(biome.getNativePokemons(), wildPokemonOrder),
getBossPokemons(biome.getNativePokemons(), bossPokemonOrder),
getTrainerPokemons(biome),
getNextBiomes(biome)
);
}

private List<BiomeAllPokemonResponse> getWildPokemons(List<NativePokemon> nativePokemons) {
private List<BiomeAllPokemonResponse> getWildPokemons(List<NativePokemon> nativePokemons, String wildPokemonOrder) {
return nativePokemons.stream()
.filter(NativePokemon::isWild)
.sorted(NativePokemonComparator.of(wildPokemonOrder))
.map(nativePokemon -> BiomeAllPokemonResponse.of(
nativePokemon,
getBiomePokemons(nativePokemon.getPokemonIds())))
.toList();
}

private List<BiomeAllPokemonResponse> getBossPokemons(List<NativePokemon> nativePokemons) {
private List<BiomeAllPokemonResponse> getBossPokemons(List<NativePokemon> nativePokemons, String bossPokemonOrder) {
return nativePokemons.stream()
.filter(NativePokemon::isBoss)
.sorted(NativePokemonComparator.of(bossPokemonOrder))
.map(nativePokemon -> BiomeAllPokemonResponse.of(
nativePokemon,
getBiomePokemons(nativePokemon.getPokemonIds())))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.pokerogue.helper.biome.service;

import static com.pokerogue.helper.biome.service.NativePokemonComparator.ASCENDING;
import static com.pokerogue.helper.biome.service.NativePokemonComparator.DESCENDING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.pokerogue.environment.service.ServiceTest;
import com.pokerogue.helper.biome.dto.BiomeAllPokemonResponse;
import com.pokerogue.helper.biome.dto.BiomeDetailResponse;
import com.pokerogue.helper.biome.dto.BiomeResponse;
import com.pokerogue.helper.global.exception.ErrorMessage;
Expand All @@ -30,7 +33,7 @@ void findBoimes() {
@Test
@DisplayName("단일 바이옴 정보를 불러온다")
void findBiome() {
BiomeDetailResponse biomeDetailResponse = biomeService.findBiome("fairy_cave");
BiomeDetailResponse biomeDetailResponse = biomeService.findBiome("fairy_cave", ASCENDING, DESCENDING);

assertAll(
() -> assertThat(biomeDetailResponse.id()).isEqualTo("fairy_cave"),
Expand All @@ -45,8 +48,25 @@ void findBiome() {
@Test
@DisplayName("해당 id의 바이옴이 없는 경우 예외를 발생시킨다")
void notExistBiome() {
assertThatThrownBy(() -> biomeService.findBiome("test"))
assertThatThrownBy(() -> biomeService.findBiome("test", ASCENDING, DESCENDING))
.isInstanceOf(GlobalCustomException.class)
.hasMessage(ErrorMessage.BIOME_NOT_FOUND.getMessage());
}

@Test
@DisplayName("바이옴 포켓몬의 티어를 희귀도 순으로 정렬한다.")
void sortBiomeNativePokemons() {
String bossPokemonOrder = DESCENDING;
String wildPokemonOrder = ASCENDING;

BiomeDetailResponse biomeDetailResponse = biomeService.findBiome("fairy_cave", bossPokemonOrder,
wildPokemonOrder);

assertAll(() -> {
assertThat(biomeDetailResponse.wildPokemons()).extracting(BiomeAllPokemonResponse::tier)
.containsExactly("보통", "드묾", "레어", "슈퍼 레어", "울트라 레어");
assertThat(biomeDetailResponse.bossPokemons()).extracting(BiomeAllPokemonResponse::tier)
.containsExactly("슈퍼 울트라 레어 보스", "슈퍼 레어 보스", "레어 보스", "보스");
});
}
}

0 comments on commit 2e292e0

Please sign in to comment.