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

refactor: 코드 리팩토링 #35

Merged
merged 16 commits into from
Apr 10, 2024
Merged

refactor: 코드 리팩토링 #35

merged 16 commits into from
Apr 10, 2024

Conversation

yj-leez
Copy link
Member

@yj-leez yj-leez commented Apr 1, 2024

PR

PR 요약

  • 리팩토링 및 클린 코드 준수

변경된 점

  • 테스트가 repeatable 하기 위해서 h2 in memory db 사용하도록 환경설정 수정하였습니다. @ActiveProfiles("test") 어노테이션 이용하면 됩니다.

  • 리뷰 통계 엔티티에는 낙관적 락을, 향수 엔티티의 특정 속성에는 비관적 락을 적용하여 동시성 처리하였습니다.

  • 예외 처리 세부적으로 추가하였고 System.out.println대신 로그로 수정하였습니다.

  • 메소드 명이나 엔티티 명이 의미가 더 잘 드러나도록 수정하였습니다.

  • 그 외 자잘한 코드 리팩토링을 거쳤습니다.

  • 모든 테스트 통과하도록 수정하였습니다. 확인해보고 안되면 말해주세욥!

이슈 번호

close #34

@yj-leez yj-leez linked an issue Apr 1, 2024 that may be closed by this pull request
4 tasks
Copy link
Member

@yoonsseo yoonsseo left a comment

Choose a reason for hiding this comment

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

고생하셨어요 윤주님!!
의미 있는 이름으로 변경하고 함수를 분리하고자 하신 부분이 돋보이네용👍🏻👍🏻
동시성 관련된 부분도 인상적입니다😮😮
수고하셨어욥ㅎㅎ

public List<HomeFragrance> searchV1(@RequestParam String family){
return homeService.search(family);
public List<HomeFragranceDto> searchMainFragranceV1(@RequestParam String family){
return homeService.searchMainFragrance(family);
Copy link
Member

Choose a reason for hiding this comment

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

의미 있는 이름으로 변경해주셔서 어떤 일을 수행하는 메소드인지 더 잘 파악할 수 있는 것 같아용👍🏻👍🏻


@Repository
public interface FamilyRepository extends JpaRepository<Family, Long>{
boolean existsByKorName(String korName);
Family findByKorName(String korName);
Optional<Family> findByKorName(String korName);
Copy link
Member

@yoonsseo yoonsseo Apr 4, 2024

Choose a reason for hiding this comment

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

헉 옵셔널로 감싸두지 않았었군요..!
옵셔널로 감싸는 이유는 뭐가 있을까요??😆😆

Copy link
Member Author

@yj-leez yj-leez Apr 9, 2024

Choose a reason for hiding this comment

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

NULL 체크를 빼먹거나 아니면 불필요하게 체크하는 일을 방지하기 위해서죠!
그 전에는 급하게 구현하다보니 Optional로 감싸는 걸 까먹었습니다..🥲

Page<DisplayFragrance> searchByFilter(FragranceFilterCond cond, String additionalFamily, Pageable pageable);
Page<FragranceCatalogDto> searchByBrandAndFamily(FragranceFilterCond cond, Pageable pageable);
Copy link
Member

Choose a reason for hiding this comment

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

여기에서 addtionalFamily가 빠진 이유는 무엇인가욥??👀👀

Copy link
Member

Choose a reason for hiding this comment

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

아아 컨디션 DTO에 넣으셨군용

Comment on lines -80 to +78
public DisplayIngredient getIngredientContent(String ingredient) {
// 향료가 존재하는지 확인 후 조회
if(!ingredientRepository.existsByKorName(ingredient)) throw new CustomException(ErrorCode.INGREDIENT_NOT_FOUND);
Ingredient findIngredient = ingredientRepository.findByKorName(ingredient);

// 향료에 해당하는 향료 타입이 존재하는지 확인 후 조회
IngredientType findIngredientType = ingredientTypeRepository.findById(findIngredient.getIngredientType().getId())
.orElseThrow(()-> new CustomException(ErrorCode.INGREDIENT_TYPE_NOT_FOUND));
public IngredientDetailsDto getIngredientContent(String ingredient) {
Ingredient findIngredient = ingredientRepository.findByKorName(ingredient)
.orElseThrow(() -> new CustomException(ErrorCode.INGREDIENT_NOT_FOUND));
Copy link
Member

Choose a reason for hiding this comment

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

코드가 훨씬 더 간결해지고 깔끔해진 것 같아요~😆😆

@Repository
public interface IngredientRepository extends JpaRepository<Ingredient, Long>, IngredientRepositoryCustom {

Ingredient findByKorName(String korName);
Optional<Ingredient> findByKorName(String korName);
Copy link
Member

Choose a reason for hiding this comment

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

이전에는 Optional을 붙여주지 않았던 이유가 뭔가욥??🤔🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

위에서 말했듯 급하게 작성하다보니 누락하였습니당ㅜ..

Comment on lines +32 to +33
@Version
private Long version;
Copy link
Member

Choose a reason for hiding this comment

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

낙관적 락을 사용하셨군욥😊😊

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fragrance_id")
private Fragrance fragrance;

public void updateVariable(String intensity, int value){
Copy link
Member

Choose a reason for hiding this comment

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

해당 메소드를 만들어주신 이유는 뭔가용??👀👀

Copy link
Member Author

@yj-leez yj-leez Apr 9, 2024

Choose a reason for hiding this comment

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

String intensity = review.getIntensity().toString().toLowerCase(); // 향의 세기
reviewUpdateRepository.updateIntensity(intensity, review.getFragrance().getId(), -1);

원래 이렇게 작성하였는데 객체 내에 setter로직을 사용하면 데이터 변경 시 엔티티의 일관성을 유지하기 쉽고 객체지향적인 설계를 유지할 수 있다 생각하여 변경하였습니다!

다만 두 가지 방법 각각 장단점이 있어 같이 이야기해봐도 좋을 것 같아요!

class ReviewServiceTest {

@Autowired ReviewService reviewService;
@Autowired FragranceRepository fragranceRepository;
@Autowired ReviewSeasonRepository reviewSeasonRepository;

@DisplayName("리뷰 작성/ 삭제가 여러 스레드에서 일어날 때 reviewCnt 일관성 유지 테스트")
Copy link
Member

Choose a reason for hiding this comment

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

동시성 테스트는 생각할 게 많네용🥲🥲

@yj-leez yj-leez merged commit db191e0 into main Apr 10, 2024
1 check passed
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.

[FEATURE] 리팩토링
2 participants