Skip to content

Commit

Permalink
Merge pull request #133 from woowacourse-teams/be/refactor/#130-impro…
Browse files Browse the repository at this point in the history
…ve-pokemon-query

[BE-REFACTOR] Pokemon 쿼리 개선
  • Loading branch information
jongmee authored Aug 5, 2024
2 parents a6eb1da + cd7552e commit 428ac98
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Backend-CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
sudo mkdir -p log/nginx && sudo touch log/nginx/error.log && sudo touch log/nginx/access.log
sudo touch docker-compose.yml
echo "${{ vars.DOCKER_COMPOSE }}" | sudo tee docker-compose.yml > /dev/null
echo "${{ secrets.DOCKER_COMPOSE_PROD }}" | sudo tee docker-compose.yml > /dev/null
sudo chmod 666 /var/run/docker.sock
Expand Down
27 changes: 25 additions & 2 deletions .github/workflows/Backend-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ permissions:
contents: read

jobs:
build:
test:
runs-on: ubuntu-latest
steps:

Expand All @@ -26,13 +26,36 @@ jobs:
java-version: '17'
distribution: 'temurin'

- name: Set up MySQL
- name: Set up Local MySQL
uses: mirromutth/[email protected]
with:
mysql database: 'pokerogue'
mysql user: ${{ secrets.TEST_DB_USERNAME }}
mysql password: ${{ secrets.TEST_DB_PASSWORD }}

- name: Set up Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Set up Test MySQL
run: |
sudo touch docker-compose.yml
echo "${{ secrets.DOCKER_COMPOSE_TEST_DB }}" | sudo tee docker-compose.yml > /dev/null
docker-compose -f docker-compose.yml up -d
- name: Wait for Test MySQL to be ready
run: |
for i in {1..30}; do
if docker-compose exec -T pokerogue-db mysqladmin ping --host 127.0.0.1 --user ${{ secrets.TEST_DB_USERNAME }} --password=${{ secrets.TEST_DB_PASSWORD }} --silent; then
echo "MySQL is up and running!"
break
else
echo "Waiting for MySQL to be ready..."
sleep 5
fi
done
- name: Run Test
run: SPRING_PROFILES_ACTIVE=local-mysql ./gradlew clean test
working-directory: ./backend/pokerogue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@


import com.pokerogue.helper.pokemon.domain.Pokemon;
import java.util.Optional;
import lombok.NonNull;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface PokemonRepository extends JpaRepository<Pokemon, Long> {

@Override
@NonNull
@EntityGraph(attributePaths = {"pokemonAbilityMappings"})
Optional<Pokemon> findById(@NonNull Long id);
@Query("""
select p from Pokemon p
join fetch p.pokemonAbilityMappings pam
join fetch pam.pokemonAbility pa
where p.id = :id
""")
Optional<Pokemon> findDetailsById(@Param("id") Long id);

@Query("""
select p from Pokemon p
join fetch p.pokemonTypeMappings ptm
join fetch ptm.pokemonType
""")
List<Pokemon> findAllWithTypes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PokemonService {
private final PokemonRepository pokemonRepository;

public List<PokemonResponse> findPokemons() {
List<Pokemon> pokemons = pokemonRepository.findAll();
List<Pokemon> pokemons = pokemonRepository.findAllWithTypes();

return pokemons.stream()
.map(this::toPokemonResponse)
Expand All @@ -43,7 +43,7 @@ private PokemonResponse toPokemonResponse(Pokemon pokemon) {

@Transactional(readOnly = true)
public PokedexResponse findPokedexDetails(Long id) {
Pokemon pokemon = pokemonRepository.findById(id)
Pokemon pokemon = pokemonRepository.findDetailsById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));

return toPokedexResponse(pokemon);
Expand Down
2 changes: 1 addition & 1 deletion backend/pokerogue/src/main/resources
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.pokerogue.helper.pokemon.repository;

import com.pokerogue.environment.repository.RepositoryTest;
import com.pokerogue.helper.pokemon.domain.Pokemon;
import com.pokerogue.helper.pokemon.domain.PokemonAbilityMapping;
import com.pokerogue.helper.pokemon.domain.PokemonTypeMapping;
import com.pokerogue.helper.type.domain.PokemonType;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

class PokemonRepositoryTest extends RepositoryTest {

@Autowired
private PokemonRepository pokemonRepository;

@Test
@DisplayName("모든 포켓몬을 타입 정보와 함께 조회한다.")
void findAllWithTypes() {
List<Pokemon> pokemons = pokemonRepository.findAllWithTypes();

List<PokemonType> pokemonTypes = selectedPokemonTypes(pokemons);
assertAll(() -> {
assertThat(pokemons).isNotEmpty();
assertThat(pokemonTypes).isNotEmpty();
});
}

private List<PokemonType> selectedPokemonTypes(List<Pokemon> pokemons) {
return pokemons.stream()
.map(Pokemon::getPokemonTypeMappings)
.flatMap(pokemonTypeMappings -> pokemonTypeMappings.stream()
.map(PokemonTypeMapping::getPokemonType))
.toList();
}

@Test
@DisplayName("id로 포켓몬 상세 정보를 타입, 특성 정보와 함께 조회한다.")
void findDetailsById() {
Pokemon pokemon = pokemonRepository.findAllWithTypes().get(0);
Long firstPokemonId = pokemon.getId();

Pokemon queriedPokemon = pokemonRepository.findDetailsById(firstPokemonId).get();

assertAll(() -> {
assertThat(queriedPokemon.getPokemonAbilityMappings().stream()
.map(PokemonAbilityMapping::getPokemonAbility).toList()).isNotEmpty();
assertThat(queriedPokemon.getPokemonTypeMappings().stream()
.map(PokemonTypeMapping::getPokemonType).toList()).isNotEmpty();
});
}
}

0 comments on commit 428ac98

Please sign in to comment.