Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE-FEAT] 기술 관련 api 만들기 #360

Merged
merged 17 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
@@ -1,5 +1,6 @@
package com.pokerogue.helper.move.controller;

import com.pokerogue.helper.move.dto.MoveDetailResponse;
import com.pokerogue.helper.move.dto.MoveResponse;
import com.pokerogue.helper.move.service.MoveService;
import com.pokerogue.helper.util.dto.ApiResponse;
Expand All @@ -18,6 +19,11 @@ public class MoveController {

private final MoveService moveService;

@GetMapping("/api/v1/moves1")
public ApiResponse<List<MoveResponse>> moveList() {
return new ApiResponse<>("기술 리스트 불러오기에 성공했습니다.", moveService.findMoves());
}

@GetMapping("/api/v1/moves")
public ApiResponse<List<MoveResponse>> moveListByPokedexNumber(@RequestParam("pokedex-number") Integer pokedexNumber) {
log.info(
Expand All @@ -31,14 +37,26 @@ public ApiResponse<List<MoveResponse>> moveListByPokedexNumber(@RequestParam("po
}

@GetMapping("/api/v1/move/{id}")
public ApiResponse<MoveResponse> moveDetails(@PathVariable String id) {
public ApiResponse<MoveResponse> moveDetailsByBattle(@PathVariable String id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메소드들(서비스 메소드 포함) moveDetailsInBattle 어때요 ?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다! ☺️☺️

log.info(
"---- URI : {}, Param : {}, ThreadName : {}",
"/api/v1/move/{id}",
id,
Thread.currentThread().getName()
);

return new ApiResponse<>("포켓몬의 기술 불러오기에 성공했습니다.", moveService.findMove(id));
return new ApiResponse<>("기술 정보 불러오기에 성공했습니다.", moveService.findMoveByBattle(id));
}

@GetMapping("/api/v1/move1/{id}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 사용되고 있는 move1 대신에 /api/v1/move-dex/{id}/api/v1/move/dex/{id}어떤가요

'기술 도감' 이어서 생각났어요! 배틀 관련 기술 api에 battle 붙이고 싶지만 하위호환성 때문에 그건 안될 거 같고.. 🥲

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다! ☺️☺️

public ApiResponse<MoveDetailResponse> moveDetails(@PathVariable String id) {
log.info(
"---- URI : {}, Param : {}, ThreadName : {}",
"/api/v1/move1/{id}",
id,
Thread.currentThread().getName()
);

return new ApiResponse<>("기술 정보 불러오기에 성공했습니다.", moveService.findMove(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import java.util.Arrays;
import lombok.Getter;

@Getter
public enum MoveFlag {

NONE("none"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import java.util.Arrays;
import lombok.Getter;

@Getter
public enum MoveTarget {

USER("user"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.pokerogue.helper.move.dto;

import com.pokerogue.helper.move.data.Move;
import com.pokerogue.helper.move.data.MoveCategory;
import com.pokerogue.helper.move.data.MoveFlag;
import com.pokerogue.helper.type.data.Type;
import java.util.List;

public record MoveDetailResponse(
String id,
String name,
String typeEngName,
String typeLogo,
String categoryEngName,
String categoryLogo,
String moveTarget,
Integer power,
Integer accuracy,
Integer powerPoint,
String effect,
Integer effectChance,
Integer priority,
Integer generation,
String released,
List<String> flags,
List<String> pokemonIdsWithLevelMove,
List<String> pokemonIdsWithEggMove
) {

public static MoveDetailResponse from(Move move, List<String> levelMoveIdsContains, List<String> eggMoveIdsContains) {
Type type = move.getType();
MoveCategory moveCategory = move.getMoveCategory();
List<String> moveFlags = move.getFlags().stream()
.map(MoveFlag::getId)
.toList();

return new MoveDetailResponse(
move.getId(),
move.getKoName(),
type.getName(),
type.getImage(),
moveCategory.getEngName(),
moveCategory.getImage(),
move.getMoveTarget().getId(),
move.getPower(),
move.getAccuracy(),
move.getPowerPoint(),
move.getEffect(),
move.getEffectChance(),
move.getPriority(),
move.getGeneration(),
move.getReleased(),
moveFlags,
levelMoveIdsContains,
eggMoveIdsContains
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.move.data.Move;
import com.pokerogue.helper.move.dto.MoveDetailResponse;
import com.pokerogue.helper.move.dto.MoveResponse;
import com.pokerogue.helper.move.repository.MoveRepository;
import com.pokerogue.helper.pokemon.data.LevelMove;
Expand All @@ -20,6 +21,12 @@ public class MoveService {
private final PokemonRepository pokemonRepository;
private final MoveRepository moveRepository;

public List<MoveResponse> findMoves() {
return moveRepository.findAll().stream()
.map(MoveResponse::from)
.toList();
}

public List<MoveResponse> findMovesByPokemon(Integer pokedexNumber) {
List<Pokemon> pokemons = pokemonRepository.findByPokedexNumber(pokedexNumber);
if (pokemons.isEmpty()) {
Expand Down Expand Up @@ -51,11 +58,23 @@ private static List<String> getAllMoveIds(Pokemon pokemon) {
return allMoveIds;
}

public MoveResponse findMove(String id) {
public MoveResponse findMoveByBattle(String id) {
Move move = findMoveById(id);
return MoveResponse.from(move);
}

public MoveDetailResponse findMove(String id) {
Move move = findMoveById(id);
List<String> eggMoveIdsContains = pokemonRepository.findByEggMoveIdsContains(move.getId()).stream()
.map(Pokemon::getId)
.toList();
List<String> levelMoveIdsContains = pokemonRepository.findByLevelMovesMoveId(move.getId()).stream()
jinchiim marked this conversation as resolved.
Show resolved Hide resolved
.map(Pokemon::getId)
.toList();

return MoveDetailResponse.from(move, levelMoveIdsContains, eggMoveIdsContains);
}

private Move findMoveById(String id) {
return moveRepository.findById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.MOVE_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
public interface PokemonRepository extends MongoRepository<Pokemon, String> {

List<Pokemon> findByPokedexNumber(int pokedexNumber);

List<Pokemon> findByEggMoveIdsContains(String eggMoveIds);

List<Pokemon> findByLevelMovesMoveId(String moveId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.pokerogue.environment.service.ServiceTest;
import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.move.dto.MoveDetailResponse;
import com.pokerogue.helper.move.dto.MoveResponse;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -27,9 +28,9 @@ void findMovesByPokemon() {
}

@Test
@DisplayName("단일 기술 정보를 불러온다")
void findMove() {
MoveResponse moveResponse = moveService.findMove("earth_power");
@DisplayName("배틀 서비스에서 사용할 단일 기술 정보를 불러온다")
void findMoveByBattle() {
MoveResponse moveResponse = moveService.findMoveByBattle("earth_power");

assertAll(
() -> assertThat(moveResponse.id()).isEqualTo("earth_power"),
Expand All @@ -47,8 +48,43 @@ void findMove() {
@Test
@DisplayName("id에 해당하는 기술이 없는 경우 예외를 발생시킨다")
void notExistMove() {
assertThatThrownBy(() -> moveService.findMove("test"))
assertThatThrownBy(() -> moveService.findMoveByBattle("test"))
.isInstanceOf(GlobalCustomException.class)
.hasMessage(ErrorMessage.MOVE_NOT_FOUND.getMessage());
}

@Test
@DisplayName("전체 기술 목록을 반환한다")
void findMoves() {
List<MoveResponse> movesByPokemon = moveService.findMoves();

assertThat(movesByPokemon.size()).isEqualTo(920);
}

@Test
@DisplayName("단일 기술 정보를 불러온다")
void findMove() {
MoveDetailResponse moveDetailResponse = moveService.findMove("earth_power");

assertAll(
() -> assertThat(moveDetailResponse.id()).isEqualTo("earth_power"),
() -> assertThat(moveDetailResponse.name()).isEqualTo("대지의힘"),
() -> assertThat(moveDetailResponse.typeEngName()).isEqualTo("ground"),
() -> assertThat(moveDetailResponse.typeLogo()).contains("type/ground"),
() -> assertThat(moveDetailResponse.categoryEngName()).isEqualTo("special"),
() -> assertThat(moveDetailResponse.categoryLogo()).contains("move-category/special.png"),
() -> assertThat(moveDetailResponse.moveTarget()).isEqualTo("near_other"),
() -> assertThat(moveDetailResponse.power()).isEqualTo(90),
() -> assertThat(moveDetailResponse.accuracy()).isEqualTo(100),
() -> assertThat(moveDetailResponse.powerPoint()).isEqualTo(10),
() -> assertThat(moveDetailResponse.effect()).isEqualTo("상대의 발밑에 대지의 힘을 방출한다. 상대의 특수방어를 떨어뜨릴 때가 있다."),
() -> assertThat(moveDetailResponse.effectChance()).isEqualTo(10),
() -> assertThat(moveDetailResponse.priority()).isEqualTo(0),
() -> assertThat(moveDetailResponse.generation()).isEqualTo(4),
() -> assertThat(moveDetailResponse.released()).isNull(),
() -> assertThat(moveDetailResponse.flags()).isEmpty(),
() -> assertThat(moveDetailResponse.pokemonIdsWithLevelMove()).hasSize(71),
() -> assertThat(moveDetailResponse.pokemonIdsWithEggMove()).hasSize(113)
);
}
}
Loading