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-REFACTOR] 포켓몬 정보 MongoDB 리팩토링 #336

Merged
merged 79 commits into from
Oct 11, 2024

Conversation

dwax1324
Copy link

@dwax1324 dwax1324 commented Sep 25, 2024

🍄 PR 확인 사항

PR이 다음 요구 사항을 충족하는지 확인하세요. :

  • API 명세서가 업데이트 혹은 작성이 되어 있나요?

현재 작업은 어떤 이슈를 해결한 것인지 설명해주세요.

Issue Number: #337

기존 코드에서 변경된 점이 있다면 설명해주세요. (추가 X)

  • 있음
  • 없음
  • 포켓몬 레포지토리만 몽고디비 적용
  • 데이터 기본 생성자 추가, 연관 데이터 클래스 필드 추가
  • Type은 바이옴에서 사용하고 있어서 지우지 않음
  • �포케 덱스 넘버가 DTO에서 기존에 Long이어서 일단 Integer에서 업캐스팅함
  • MongoConfiguration 추가, 타입컨버터와 아이템 컨버터 MongoConfiguration에 추가
  • 업데이트된 진화 아이템, 폼 변환 아이템 EvolutionItem이넘에 추가

@dwax1324 dwax1324 self-assigned this Sep 25, 2024
Copy link
Contributor

@jongmee jongmee left a comment

Choose a reason for hiding this comment

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

종이 고생하셨습니다!! 리뷰 남겨놨어요 😊 화이팅!!!

그리고 테스트 코드가 없는데 정말 단순한 로직이 아니라면 테스트 코드가 필요할 것 같아요~!

@Override
public EvolutionItem convert(String id) {
return EvolutionItem.findById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.EVOLUTION_NOT_FOUND));
Copy link
Contributor

Choose a reason for hiding this comment

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

not found 에러보다 convert 관련 에러가 더 명확하지 않을까요? ☺️ 상태코드는 404보다 500이 적절할 것 같고요!

Copy link
Contributor

@jinchiim jinchiim Sep 25, 2024

Choose a reason for hiding this comment

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

요거 저희끼리 정해야겠네요 🤔 오늘 정하면 어떤데 (12시 지났으니까 오늘로...^^)

Copy link
Contributor

Choose a reason for hiding this comment

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

좋아유

Copy link
Author

@dwax1324 dwax1324 Sep 26, 2024

Choose a reason for hiding this comment

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

지금 코드에서 에러는 notfound + 500이 괜찮아 보여요.
convert하다가 문제가 생긴 것도 맞지만 orElseThrow안에는 NotFound가 적절해보여요.
try-catch로 convert 관련 에러로 추상화시키는게 나을까요?🍀

Copy link
Contributor

@jinchiim jinchiim left a comment

Choose a reason for hiding this comment

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

안녕하세요 종이!

코드 잘 확인했고 고생하셨습니다!
전반적으로 리팩토링을 하기로 했기 때문에 그런 부분들과 테스트 보강을 위주로 작성했어요!

확인해주세요! ☺️

Copy link

@unifolio0 unifolio0 left a comment

Choose a reason for hiding this comment

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

폴라랑 미아가 열심히 달아줘서 간단한 것만 달았기에 approve합니다!

구현 수고하셨습니다! 👍

@dwax1324
Copy link
Author

dwax1324 commented Oct 8, 2024

리뷰 반영 완료했습니다
늦어서 정말 죄송합니다 😢

PokemonValidator 포켓몬 데이터 검증 클래스 추가

  • 데이터에 대한 메타정보를 갖고 있음 (예: 포켓몬의 총 개수)
  • PokemonValidatorTest에선 검증 로직에 관한 예외 발생 테스트를 한다.
    • 테스트 계속 추가 예정..
  • PokemonDataTest에선 실제 데이터에 대한 테스트가 통과되는지 검사한다.
    • 잘못된 데이터 때문에 통과하지 못하고 있는 테스트는 Disabled처리.

테스트, 에러메시지 추가

  • 잘못된 데이터로 인한 상태코드는 모두 500으로 변경

ImageUrl 간단한 이미지 상수 저장 이넘

  • 논의가 자세히 되지 않아� 사용만 할 수 있을정도로 구현

진화 관련

포켓몬에 대한 진화 배열이 주어졌을 때 각 포켓몬에 대한 Depth만 구하는 것으로 단순화 시킴.
Depth란 진화를 표현한 트리가 있을 때 진화 깊이를 뜻함. (예: 이상해씨=0 -> 이상해풀=1 -> 이상해꽃=2)

EvolutionService

  • PokemonService가 비대해져서 따로 분리시킴.

EvolutionContext

  • 진화와 관련된 정보를 갖고있다.
  • List<Evolution>이 주어졌을 때 간선 정보인 Map<String,List<String>> edges를 생성함.
    • 포켓몬 진화와 관련 없는 TreeDepthCalculator에게 <from,{to_1,to_2...}>의 아이디형식 간선정보를 넘기기 위함.
    • TreeDepthCalculator를 통해 depth정보를 가져온다.

TreeDepthCalculator

  • 간선 정보가 주어졌을 때 진입 차수(in-degree)를 기준으로 각 노드의 깊이를 구함.
    • 큐에 루트노드를 삽입한다.(큐가 빌 때까지 다음의 과정을 반복한다)
      • 인접 노드의 진입차수를 1씩 감소시킨다.
      • 인접 노드의 진입 차수가 0이라면 큐에 삽입한다.

BufferedQueue

  • buffer함수를 호출시키면 현재까지 삽입된 원소의 개수를 저장함.
    • 그 후 hasBufferedNext를 통해 버퍼의 크기만큼 poll을 할 수 있다.

Copy link
Contributor

@jinchiim jinchiim left a comment

Choose a reason for hiding this comment

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

개행🐶 이랑 접근제어자만 한번 봐주세요! 😋

Comment on lines 37 to 38

static void validatePokemonSize(int pokemonCount) {
Copy link
Contributor

Choose a reason for hiding this comment

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

default 고민 해봅시다 🤔

Copy link
Contributor

@jongmee jongmee left a comment

Choose a reason for hiding this comment

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

종이!!! 서비스 코드가 정말 깔끔해졌고 데이터 테스트도 완벽하네요 😊👍 저번 리뷰들은 다 resolve 처리했습니다! 추가 리뷰 몇 개 달아놨어요 😉

)
.flatMap(stream -> stream)
.findFirst()
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));
Copy link
Contributor

Choose a reason for hiding this comment

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

public Evolution getEvolutionOf(String pokemonId) {
    return evolutions.stream()
            .filter(evolution -> evolution.getTo().equals(pokemonId) || evolution.getFrom().equals(pokemonId))
            .findFirst()
            .orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));
}

이렇게 스트림 한 번 쓰는게 더 효율적일 것 같아요!

++) getter 대신 메소드 만드는 것도 제안해봅니다 ☺️

 .filter(evolution -> evolution.hasSameTo(pokemonId) || evolution.hasSameFrom(pokemonId))

Copy link
Author

@dwax1324 dwax1324 Oct 10, 2024

Choose a reason for hiding this comment

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

이부분은 모든 포켓몬의 getTo를 먼저 찾은 후, getFrom을 찾아야해서 Stream.of 연산을 사용하였습니다!
(진화 가능한 포켓몬이라면 getTo의 evolution을 가져오고, 루트 포켓몬이라면 getFrom의 evolution을 가져온다.)

제가 작성한 코드가 스트림의 순서에 의존적이고 가독성이 좋은 코드는 아닌 거 같네요!
더 좋은 방법이 있을지 고민해보겠습니다😲

Copy link
Contributor

Choose a reason for hiding this comment

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

굿! 천천히 생각해봐도 좋을 것 같아요 👍

.peek(this::decreaseIndegreeCount)
.filter(this::isIndegreeZero)
.filter(this::isAdjacentNodeExist)
.forEach(bufferedQueue::add);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍👍👍 너무 이해하기 쉽네요!!! 최고

package com.pokerogue.helper.pokemon.data;


public class FakeEvolution extends Evolution {
Copy link
Contributor

Choose a reason for hiding this comment

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

저번에 잠깐 얘기했던 대로 Fake 객체를 일일이 만드는 것보다 Collection(여기에서는 Evolution)에 생성자 등을 추가하는게 더 좋을 것 같아요 ☺️

Copy link
Contributor

Choose a reason for hiding this comment

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

Evolution이 변경되면(메소드 추가 등) Fake 객체를 항상 일관되게 맞춰줘야 하는데 놓치기 쉬울 것 같아서요!

Copy link
Author

@dwax1324 dwax1324 Oct 10, 2024

Choose a reason for hiding this comment

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

Evolution은 �AllArgs을 열고 Pokemon은 빈생성자의 접근제어를 해제하고 Setter를 열었습니다.
아무래도 포켓몬은 생성자 매개변수가 너무 많아서.. Setter를 열면 부작용이 생길까요?

Copy link
Contributor

Choose a reason for hiding this comment

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

저도 배틀 테스트하면서 그거 때문에 약간 고민했는데 .. 사실 우리 서비스 로직에서 실수로라도 setter를 사용할 일이 거의 없다고 보기 때문에 일단 괜찮을 것 같습니다 🤔 하지만 다같이 또 논의해봐요!

@dwax1324
Copy link
Author

/noti
리뷰반영완

Copy link
Contributor

@jinchiim jinchiim left a comment

Choose a reason for hiding this comment

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

고생하셨어요 종이!
미아까지 머지 이후 테스트 작성하면서 궁금하거나 리팩토링 욕심이 나는 부분들은 따로 연락드리도록 할게요!

130개의 코멘트 동안 수고하셨습니다 ㅋㅋㅋㅋ 🤣

Copy link
Contributor

@jongmee jongmee left a comment

Choose a reason for hiding this comment

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

고생하셨습니다 !!! 머지 고고.

@dwax1324 dwax1324 merged commit 1b52135 into be/develop Oct 11, 2024
1 check passed
@dwax1324 dwax1324 deleted the be/refactor/mongodb-pokemon branch October 11, 2024 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants