Skip to content

Commit

Permalink
[BE-REFACTOR] Ability 정보 MongoDB 사용하도록 리팩토링 (#349)
Browse files Browse the repository at this point in the history
* refactor: mongoDB로 변경

* refactor: BiomeDatabaseInitializer 삭제

* refactor: 정적 팩토리 메소드로 수정

* refactor: 메소드 분리

* refactor: 메소드 명 수정

* refactor: 메소드 분리
  • Loading branch information
unifolio0 authored Sep 27, 2024
1 parent 9b9aca7 commit 408d042
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public ApiResponse<List<AbilityResponse>> abilityList() {

@GetMapping("/api/v1/ability2/{id}")
public ApiResponse<AbilityDetailResponse> abilityDetails(@PathVariable("id") String id) {
log.info("---- URI : {}, Param : {}", "/api/v1/ability/{id}", id);
log.info(
"---- URI : {}, Param : {}, ThreadName : {}",
"/api/v1/ability/{id}",
id,
Thread.currentThread().getName()
);

return new ApiResponse<>("특성 정보 불러오기에 성공했습니다.", abilityService.findAbilityDetails(id));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.pokerogue.helper.ability.data;

import com.pokerogue.helper.pokemon.data.InMemoryPokemon;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Getter
@Document(collection = "ability")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Ability {

@Id
Expand All @@ -30,21 +32,15 @@ public class Ability {
private int generation;

@Field("pokemonIds")
private List<Integer> pokemonIds;
private List<String> pokemonIds;

@Field("isBypassFaint")
private Boolean isBypassFaint;

@Field("isIgnorable")
private Boolean isIgnorable;

// Todo: 지우기
private List<InMemoryPokemon> inMemoryPokemons;

public Ability(String id, String name, String description, List<InMemoryPokemon> inMemoryPokemons) {
this.id = id;
this.name = name;
this.description = description;
this.inMemoryPokemons = inMemoryPokemons;
public boolean isPresent() {
return !id.equals("none");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public record AbilityDetailResponse(
) {

public static AbilityDetailResponse of(Ability ability, List<AbilityPokemonResponse> pokemons) {
return new AbilityDetailResponse(ability.getName(), ability.getDescription(), pokemons);
return new AbilityDetailResponse(ability.getKoName(), ability.getDescription(), pokemons);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pokerogue.helper.ability.dto;

import com.pokerogue.helper.pokemon.data.Pokemon;
import java.util.List;

public record AbilityPokemonResponse(
Expand All @@ -9,4 +10,17 @@ public record AbilityPokemonResponse(
String image,
List<AbilityTypeResponse> pokemonTypeResponses
) {
public static AbilityPokemonResponse of(
Pokemon pokemon,
String image,
List<AbilityTypeResponse> pokemonTypeResponses
) {
return new AbilityPokemonResponse(
pokemon.getId(),
(long) pokemon.getPokedexNumber(),
pokemon.getKoName(),
image,
pokemonTypeResponses
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
public record AbilityResponse(String id, String koName, String description) {

public static AbilityResponse from(Ability ability) {
return new AbilityResponse(ability.getId(), ability.getName(), ability.getDescription());
return new AbilityResponse(ability.getId(), ability.getKoName(), ability.getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package com.pokerogue.helper.ability.dto;

import com.pokerogue.helper.type.data.Type;

public record AbilityTypeResponse(String typeLogo, String typeName) {

public static AbilityTypeResponse from(Type type) {
return new AbilityTypeResponse(type.getImage(), type.getKoName());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import com.pokerogue.helper.ability.dto.AbilityPokemonResponse;
import com.pokerogue.helper.ability.dto.AbilityResponse;
import com.pokerogue.helper.ability.dto.AbilityTypeResponse;
import com.pokerogue.helper.ability.repository.InMemoryAbilityRepository;
import com.pokerogue.helper.type.data.Type;
import com.pokerogue.helper.ability.repository.AbilityRepository;
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import java.util.ArrayList;
import com.pokerogue.helper.pokemon.data.Pokemon;
import com.pokerogue.helper.pokemon.repository.PokemonRepository;
import com.pokerogue.helper.type.data.Type;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -20,43 +21,45 @@
public class AbilityService {

private final S3Service s3Service;
private final InMemoryAbilityRepository inMemoryAbilityRepository;
private final AbilityRepository abilityRepository;
private final PokemonRepository pokemonRepository;

public List<AbilityResponse> findAbilities() {
return inMemoryAbilityRepository.findAll().stream()
return abilityRepository.findAll().stream()
.filter(Ability::isPresent)
.map(AbilityResponse::from)
.toList();
}

public AbilityDetailResponse findAbilityDetails(String id) {
Ability ability = inMemoryAbilityRepository.findById(id)
Ability ability = abilityRepository.findById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND));
List<AbilityPokemonResponse> abilityPokemonResponses = ability.getInMemoryPokemons().stream()
.map(abilityPokemon -> new AbilityPokemonResponse(
abilityPokemon.id(),
Long.parseLong(abilityPokemon.speciesId()),
abilityPokemon.koName(),
s3Service.getPokemonImageFromS3(abilityPokemon.id()),
getAbilityTypeResponses(abilityPokemon.firstType(), abilityPokemon.secondType())
List<String> abilityPokemonIds = ability.getPokemonIds();
List<Pokemon> pokemons = pokemonRepository.findAllById(abilityPokemonIds);
validateExistAllPokemonId(abilityPokemonIds, pokemons);
List<AbilityPokemonResponse> abilityPokemonResponses = pokemons.stream()
.map(pokemon -> AbilityPokemonResponse.of(
pokemon,
s3Service.getPokemonImageFromS3(pokemon.getImageId()),
getAbilityTypeResponses(pokemon.getTypes())
))
.toList();

return AbilityDetailResponse.of(ability, abilityPokemonResponses);
}

private List<AbilityTypeResponse> getAbilityTypeResponses(String firstType, String secondType) {
List<AbilityTypeResponse> abilityTypeResponses = new ArrayList<>();
if (!firstType.equals("Type.undefined") && !firstType.isEmpty()) {
abilityTypeResponses.add(new AbilityTypeResponse(Type.findByEngName(firstType)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_TYPE_NOT_FOUND)).getImage(),
firstType));
}
if (!secondType.equals("Type.undefined") && !secondType.isEmpty()) {
abilityTypeResponses.add(new AbilityTypeResponse(Type.findByEngName(secondType)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_TYPE_NOT_FOUND)).getImage(),
secondType));
private static void validateExistAllPokemonId(
List<String> abilityPokemonIds,
List<Pokemon> abilityPokemonResponses
) {
if (abilityPokemonIds.size() != abilityPokemonResponses.size()) {
throw new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND);
}
}

return abilityTypeResponses;
private List<AbilityTypeResponse> getAbilityTypeResponses(List<Type> types) {
return types.stream()
.map(AbilityTypeResponse::from)
.toList();
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.pokerogue.helper.pokemon.config;

import com.pokerogue.helper.ability.data.Ability;
import com.pokerogue.helper.ability.data.AbilityInfo;
import com.pokerogue.helper.ability.repository.InMemoryAbilityRepository;
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.pokemon.data.Evolution;
Expand Down Expand Up @@ -46,7 +43,6 @@ public class PokemonDatabaseInitializer implements ApplicationRunner {

private final InMemoryPokemonRepository inMemoryPokemonRepository;
private final EvolutionRepository evolutionRepository;
private final InMemoryAbilityRepository inMemoryAbilityRepository;
private final List<String> pokemonKeys = List.of(
"id",
"speciesId",
Expand Down Expand Up @@ -95,7 +91,6 @@ public class PokemonDatabaseInitializer implements ApplicationRunner {
public void run(ApplicationArguments args) {
save("data/pokemon/pokemon.txt", this::savePokemon);
save("data/pokemon/evolution-for-pokemon-response.txt", this::saveEvolution);
saveAbility();
}

private void save(String file, Consumer<BufferedReader> consumer) {
Expand Down Expand Up @@ -344,43 +339,4 @@ private List<String> parseToken(String line) {

return values;
}

private void saveAbility() {
List<AbilityInfo> abilityInfos = new ArrayList<>();

try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("data/ability/ability.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
while (true) {
String abilityInfo = bufferedReader.readLine();
if (abilityInfo == null) {
break;
}
abilityInfos.add(new AbilityInfo(abilityInfo));
}
} catch (IOException e) {
log.error("error message : {}", e.getStackTrace()[0]);
}

abilityInfos.stream()
.map(abilityInfo -> new Ability(
abilityInfo.getId(),
abilityInfo.getName(),
abilityInfo.getDescription(),
getAbilityPokemon(abilityInfo.getPokemons()))
).forEach(inMemoryAbilityRepository::save);
}

private List<InMemoryPokemon> getAbilityPokemon(List<String> pokemons) {
List<InMemoryPokemon> abilityInMemoryPokemons = new ArrayList<>();
for (int i = 0; i < pokemons.size(); i++) {
String pokemonId = pokemons.get(i);

InMemoryPokemon inMemoryPokemon = inMemoryPokemonRepository.findById(pokemonId)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));

abilityInMemoryPokemons.add(inMemoryPokemon);
}

return abilityInMemoryPokemons;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.pokerogue.external.s3.service.S3Service;
import com.pokerogue.helper.ability.data.Ability;
import com.pokerogue.helper.ability.repository.InMemoryAbilityRepository;
import com.pokerogue.helper.ability.repository.AbilityRepository;
import com.pokerogue.helper.biome.data.Biome;
import com.pokerogue.helper.biome.repository.BiomeRepository;
import com.pokerogue.helper.global.exception.ErrorMessage;
Expand Down Expand Up @@ -45,7 +45,7 @@ public class PokemonService {
private final MoveRepository moveRepository;
private final EvolutionRepository evolutionRepository;
private final BiomeRepository biomeRepository;
private final InMemoryAbilityRepository inMemoryAbilityRepository;
private final AbilityRepository abilityRepository;

private List<PokemonResponse> findAllCache = List.of();
private Map<String, PokemonDetailResponse> findByIdCache = new HashMap<>();
Expand Down Expand Up @@ -186,16 +186,16 @@ private List<EvolutionResponse> createStages(EvolutionChain evolutionChain) {
private List<PokemonAbilityResponse> createAbilityResponse(InMemoryPokemon inMemoryPokemon) {
List<PokemonAbilityResponse> ret = new ArrayList<>();

Ability passive = inMemoryAbilityRepository.findById(inMemoryPokemon.abilityPassive())
Ability passive = abilityRepository.findById(inMemoryPokemon.abilityPassive())
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND));
ret.add(new PokemonAbilityResponse(
inMemoryPokemon.abilityPassive(), passive.getName(), passive.getDescription(),
true, false
));

Ability ability1 = inMemoryAbilityRepository.findById(inMemoryPokemon.ability1())
Ability ability1 = abilityRepository.findById(inMemoryPokemon.ability1())
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND));
Ability ability2 = inMemoryAbilityRepository.findById(inMemoryPokemon.ability2())
Ability ability2 = abilityRepository.findById(inMemoryPokemon.ability2())
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND));
List<PokemonAbilityResponse> defaultAbilities = Stream.of(ability1, ability2)
.distinct()
Expand All @@ -208,14 +208,14 @@ private List<PokemonAbilityResponse> createAbilityResponse(InMemoryPokemon inMem
ret.addAll(defaultAbilities);

if (!inMemoryPokemon.abilityHidden().isEmpty()) {
Ability hidden = inMemoryAbilityRepository.findById(inMemoryPokemon.abilityHidden())
Ability hidden = abilityRepository.findById(inMemoryPokemon.abilityHidden())
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND));
ret.add(new PokemonAbilityResponse(inMemoryPokemon.abilityHidden(), hidden.getName(), hidden.getDescription(),
false,
true));

for (int i = 1; i < ret.size() - 1; i++) {
if (inMemoryAbilityRepository.findById(ret.get(i).id())
if (abilityRepository.findById(ret.get(i).id())
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND)) == hidden
) {
ret.remove(i);
Expand Down
Loading

0 comments on commit 408d042

Please sign in to comment.