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

[FIX/#39] 최근 길 안내 장소 조회 오류 해결 #40

Merged
merged 3 commits into from
Jan 21, 2025
Merged

Conversation

gahyuun
Copy link
Member

@gahyuun gahyuun commented Jan 20, 2025

💡 Issue

📸 Screenshot

image

📄 Description

  • 길 안내 장소의 현재 로직은 동일한 장소아이디, 멤버 아이디를 가진 행이 있으면 에러를 반환
  • 하지만 동일한 장소 아이디, 멤버 아이디를 가진 행이 있으면 업데이트를 해줘야합니다 ( 검색 기록과 로직이 동일하게 !)
  • 그래서 RecentGuidedSpotEntity가 존재하면 updatedAt을 수정하구 존재하지 않으면 새로 생성해서 save하도록 구현했습니다

💬 To Reviewers

🔗 Reference

@gahyuun gahyuun requested a review from ckkim817 as a code owner January 20, 2025 15:48
@gahyuun gahyuun added the size/S label Jan 20, 2025
@gahyuun gahyuun self-assigned this Jan 20, 2025
@gahyuun gahyuun added the 🛠️ FIX 버그, 오류 등을 수정 label Jan 20, 2025
@gahyuun gahyuun changed the title fix: 장소 안내 조회 업데이트 오류 해결 (#39) [FIX/#39] 최근 길 안내 장소 조회 오류 해결 Jan 20, 2025
Copy link
Member

@ckkim817 ckkim817 left a comment

Choose a reason for hiding this comment

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

고생하셨습니다 ~ 전체적으로 코드 잘 수정하신 것 같아요

한 가지 말씀드릴 부분이 있다면, Optional을 다룰 때 isPresent + get 방식 말고 다른 것들을 사용해 보시는 것도 권해드리고 싶네요 !

isPresent + get 방식을 사용할 경우 Optional에 대해 명령적 스타일로 흐르기 쉽기 때문입니다.


isPresent + get은 보통 다음과 같은 형태로 사용되곤 하는데요,

Optional<String> optional = Optional.of("example");
if (optional.isPresent()) {
    String value = optional.get();
    System.out.println(value);
}

이는 사실상 아래처럼 Optional을 사용하지 않은 기존의 null 검사 방식과 유사합니다.

if (value != null) {
    System.out.println(value);
}

그런데 Optional의 도입 목적은 null과 같은 명령적 코드에서 벗어나 선언적으로 값을 다루는 방식(함수형 접근)을 장려하는 데 있습니다 !

isPresent + getOptional을 명령적 스타일로 사용하는 경향을 강화하기 때문에 권장되지 않는다고 명시적으로 금지된 건 아니지만, 최근 Java 커뮤니티에서는 더 선언적이고 안전한 방식을 선호하는 것 같아요

그래서 map + orElse(혹은 orElseGet)를 사용하는 방식이나, ifPresentOrElse를 사용하는 방식을 연습해 보시면 좋을 것 같습니다 ~!


Tip) map + orElse는 값을 반환해야 하는 경우에 주로 쓰이구요, ifPresentOrElse는 값 반환이 필요 없고 작업 중심일 경우 주로 쓰입니다.

지금 코드에서는 ifPresentOrElse를 쓰면 적합할 것 같네요 ! (하라고 강요하는 거 아님 ㅋㅋ)

Optional<GuidedSpotEntity> optionalGuidedSpotEntity = guidedSpotRepository.findByMemberIdAndSpotId(memberId, spotId);

optionalGuidedSpotEntity.ifPresentOrElse(
    guidedSpotEntity -> {
        GuidedSpot guidedSpot = guidedSpotMapper.toDomain(guidedSpotEntity);
        guidedSpot.setUpdatedAt(LocalDateTime.now());
        guidedSpotRepository.save(guidedSpotMapper.toEntity(guidedSpot));
    },
    () -> guidedSpotRepository.save(
        GuidedSpotEntity.builder()
            .memberId(memberId)
            .spotId(spotId)
            .build()
    )
);

@gahyuun
Copy link
Member Author

gahyuun commented Jan 21, 2025

refactor: ifPresent get 제거 (

피드백 감사합니다 수정했습니다 ~!
8e1cdf2

@gahyuun gahyuun merged commit 32107e4 into develop Jan 21, 2025
1 check passed
@gahyuun gahyuun deleted the fix/#39 branch January 21, 2025 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️ FIX 버그, 오류 등을 수정 size/S 🦊 가현
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FIX] 최근 길 안내 장소 조회 오류 해결
2 participants