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] - 여행기 Service 코드 리팩터링 #504

Merged
merged 44 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f73fe03
style: 메서드 순서 수정
Libienz Oct 4, 2024
459b5e7
feat: TravelogueDay의 주인 되는 여행기 속성 세터 오픈
Libienz Oct 4, 2024
9e18de6
feat: Travelogue 양방향 연관관계 편의메서드 작성
Libienz Oct 4, 2024
2e0bd21
feat: TravelogueDay의 TraveloguePlace 추가 양방향 연관관계 편의메서드 작성
Libienz Oct 4, 2024
f2afac0
feat: TraveloguePlace의 TraveloguePhoto 추가 양방향 연관관계 편의메서드 작성
Libienz Oct 4, 2024
e6358d6
refactor: 양방향 연관관계를 통해 Dto 매핑 과정에서 엔티티 연관관계를 맺도록 수정
Libienz Oct 4, 2024
4692659
refactor: 양방향 연관관계를 통해 Dto 응답 Dto 매핑 과정 단순화
Libienz Oct 4, 2024
9bb8111
refactor: 사용하지 않는 메서드 제거 개선
Libienz Oct 4, 2024
f9c116d
refactor: 반정규화된 엔티티로부터 likeCount를 조회하도록 DTO 매핑 로직 개선
Libienz Oct 4, 2024
fdaefcd
feat: 여행기에 대한 특정 멤버의 좋아요가 존재하는지 확인하는 기능 구현
Libienz Oct 4, 2024
93b5cbe
refactor: TravelogueFacadeService 양방향 연관관계를 이용한 매핑 과정 추상화 적용
Libienz Oct 4, 2024
aaebaec
refactor: TravelogueLikeService 사용하지 않는 메서드 제거 개선
Libienz Oct 4, 2024
873294b
refactor: 불필요한 서비스 호출 제거 개선
Libienz Oct 4, 2024
89efae0
refactor: 필터 여부에 따른 분기를 facade가 아닌 하위 서비스로 이동 개선
Libienz Oct 4, 2024
08b5dbf
refactor: TravelogueLikeService 사용하지 않는 메서드 제거 개선
Libienz Oct 4, 2024
8f42809
refactor: TravelogueLikeService 파사드 하위 서비스는 뷰를 다루지 않도록 개선
Libienz Oct 4, 2024
c7d8eb2
fix: 여행기 콘텐츠가 수정되지 않는 문제점 해결
Libienz Oct 4, 2024
6c30061
refactor: 파사드 하위의 TravelogueTagService가 도메인을 다루도록 계층 분리 개선
Libienz Oct 4, 2024
8752c64
refactor: 사용하지 않는 서비스 클래스 제거 개선
Libienz Oct 4, 2024
1ebae52
refactor: 과한 메서드 분리 제거 개선
Libienz Oct 4, 2024
4119f50
feat: 여행기 속 사진 컨텐츠를 영구 저장소로 옮기는 서비스 구현
Libienz Oct 4, 2024
1b3a1c1
refactor: 사용하지 않는 메서드 제거 개선
Libienz Oct 4, 2024
5f9dbb8
refactor: S3 컴포넌트를 Facade가 아닌 하위 서비스가 사용하도록 추상화
Libienz Oct 5, 2024
09c510c
test: 이미지 복사 로직 테스트 모킹 단순화 개선
Libienz Oct 5, 2024
6dc2714
chore: develop branch merge
Libienz Oct 5, 2024
ad3191d
test: 특정 여행기에 특정 멤버가 좋아요했는지 확인할 수 있는 기능 테스트 작성
Libienz Oct 5, 2024
cce9109
test: 여행기 저장 서비스 메서드 테스트 작성
Libienz Oct 5, 2024
70cb200
refactor: 사용하지 않는 메서드 제거 개선
Libienz Oct 5, 2024
3286ac9
refactor: 메서드 배치 호출 순서에 맞게 수정
Libienz Oct 7, 2024
0093013
refactor: 사용하지 않는 메서드 제거 개선
Libienz Oct 7, 2024
b9dde68
refactor: Guest용 뷰 로직 DTO 내부로 응집 개선
Libienz Oct 7, 2024
b0c2dcc
refactor: TravelogueFacadeService의 여행기 업데이트 메서드가 변경 내용을 반환하도록 개선
Libienz Oct 7, 2024
a7e5510
test: 여행기 수정 테스트 doesNotThrow -> 변경 내용 확인 테스트로 개선
Libienz Oct 7, 2024
72d9e83
refactor: ImagePermanentSaver -> PerputationService로 이름 변경 및 서비스 컴포넌트화
Libienz Oct 7, 2024
c37d8f1
refactor: memberService 메서드 이름 수정 (다른 서비스 메서드의 이름과 컨벤션 통일)
Libienz Oct 7, 2024
daa2246
test: 여행기 양방향 연관관계 편의 메서드 테스트 작성
Libienz Oct 7, 2024
842c40b
test: TravelogueDay 양방향 연관관계 편의 메서드 테스트 작성
Libienz Oct 7, 2024
a57579b
test: TraveloguePlace 양방향 연관관계 편의 메서드 테스트 작성
Libienz Oct 7, 2024
d079652
refactor: Setter update로 이름 변경 (사용처 제한 명세 역할)
Libienz Oct 9, 2024
99f67be
refactor: 오버로딩 제거 -> 메서드 시그니처에 명세 상세화
Libienz Oct 9, 2024
6fc7d99
refactor: 이미지 S3 영구 저장소 이동 로직을 Facade로 이동 (서비스 계층 분리 의미 명확하게)
Libienz Oct 9, 2024
720c5cf
Merge branch 'develop/be' into feature/be/#496
Libienz Oct 9, 2024
c27e99a
Merge branch 'develop/be' into feature/be/#496
Libienz Oct 9, 2024
8b77665
refactor: 메서드 명 변경 작업 미반영 -> 반영 조치
Libienz Oct 9, 2024
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
Expand Up @@ -20,7 +20,7 @@ public class MemberService {
private final PasswordEncryptor passwordEncryptor;

@Transactional(readOnly = true)
public Member getById(Long memberId) {
public Member getMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new BadRequestException("존재하지 않는 사용자입니다."));
}
Expand Down Expand Up @@ -53,7 +53,7 @@ private void validateNicknameDuplication(String nickname) {

@Transactional
public ProfileResponse updateProfile(ProfileUpdateRequest request, MemberAuth memberAuth) {
Member member = getById(memberAuth.memberId());
Member member = getMemberById(memberAuth.memberId());
member.changeNickname(request.nickname());

return ProfileResponse.from(member);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ public class MyPageFacadeService {

@Transactional(readOnly = true)
public ProfileResponse readProfile(MemberAuth memberAuth) {
Member member = memberService.getById(memberAuth.memberId());
Member member = memberService.getMemberById(memberAuth.memberId());
return ProfileResponse.from(member);
}

@Transactional(readOnly = true)
public Page<MyTravelogueResponse> readTravelogues(MemberAuth memberAuth, Pageable pageable) {
Member member = memberService.getById(memberAuth.memberId());
Member member = memberService.getMemberById(memberAuth.memberId());
Page<Travelogue> travelogues = travelogueService.findAllByMember(member, pageable);

return travelogues.map(MyTravelogueResponse::from);
}

@Transactional(readOnly = true)
public Page<PlanResponse> readTravelPlans(MemberAuth memberAuth, Pageable pageable) {
Member member = memberService.getById(memberAuth.memberId());
Member member = memberService.getMemberById(memberAuth.memberId());
Page<TravelPlan> travelPlans = travelPlanService.getAllByAuthor(member, pageable);

return travelPlans.map(PlanResponse::from);
Expand Down
5 changes: 5 additions & 0 deletions backend/src/main/java/kr/touroot/tag/dto/TagResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.annotations.media.Schema;
import kr.touroot.tag.domain.Tag;
import kr.touroot.travelogue.domain.TravelogueTag;
import lombok.Builder;

@Builder
Expand All @@ -16,4 +17,8 @@ public static TagResponse from(Tag tag) {
.tag(tag.getTag())
.build();
}

public static TagResponse from(TravelogueTag travelogueTag) {
return from(travelogueTag.getTag());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ResponseEntity<TravelogueLikeResponse> likeTravelogue(@PathVariable Long
})
@GetMapping("/{id}")
public ResponseEntity<TravelogueResponse> findTravelogue(@PathVariable Long id) {
return ResponseEntity.ok(travelogueFacadeService.findTravelogueById(id));
return ResponseEntity.ok(travelogueFacadeService.findTravelogueByIdForGuest(id));
}

@Operation(summary = "여행기 상세 조회")
Expand All @@ -120,7 +120,7 @@ public ResponseEntity<TravelogueResponse> findTravelogue(@PathVariable Long id)
})
@GetMapping(value = "/{id}", headers = {HttpHeaders.AUTHORIZATION})
public ResponseEntity<TravelogueResponse> findTravelogue(@PathVariable Long id, MemberAuth member) {
return ResponseEntity.ok(travelogueFacadeService.findTravelogueById(id, member));
return ResponseEntity.ok(travelogueFacadeService.findTravelogueByIdForAuthenticated(id, member));
}

@Operation(summary = "여행기 메인 페이지 조회")
Expand Down
29 changes: 21 additions & 8 deletions backend/src/main/java/kr/touroot/travelogue/domain/Travelogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

Expand Down Expand Up @@ -70,11 +69,6 @@ public Travelogue(Member author, String title, String thumbnail) {
this(null, author, title, thumbnail, 0L);
}

public void update(String title, String thumbnail) {
this.title = title;
this.thumbnail = thumbnail;
}

private void validate(Member author, String title, String thumbnail) {
validateNotNull(author, title, thumbnail);
validateNotBlank(title, thumbnail);
Expand Down Expand Up @@ -107,11 +101,30 @@ private void validateThumbnailFormat(String thumbnailUrl) {
throw new BadRequestException("이미지 url 형식이 잘못되었습니다");
}
}


public void update(String title, String thumbnail) {
this.title = title;
this.thumbnail = thumbnail;
}

public void updateThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}

public void addDay(TravelogueDay day) {
day.updateTravelogue(this);
travelogueDays.add(day);
}

public void updateDays(List<TravelogueDay> travelogueDays) {
this.travelogueDays.clear();
travelogueDays.forEach(this::addDay);
}
Comment on lines +119 to +122
Copy link

@slimsha2dy slimsha2dy Oct 8, 2024

Choose a reason for hiding this comment

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

이번 pr에서 할 일은 아닌 것 같긴 한데 양방향 연관관계를 활용해서 clear 없이 update를 수행하도록 바꾸면 더 좋겠네요.
update 로직 개선으로 추후 진행해봐도 좋을 것 같습니다!

Copy link
Author

@Libienz Libienz Oct 9, 2024

Choose a reason for hiding this comment

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

이슈 파놓았습니다~ #514

변경 감지로 진행되면 깔끔할 것 같아요 다만 프론트랑 명세는 맞추어 봐야겠네요!


public void increaseLikeCount() {
likeCount += LIKE_COUNT_WEIGHT;
}

public void decreaseLikeCount() {
likeCount -= LIKE_COUNT_WEIGHT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,13 @@ private void validateOrderRange(Integer order) {
throw new BadRequestException("여행 날짜의 순서는 음수 일 수 없습니다");
}
}

public void addPlace(TraveloguePlace place) {
traveloguePlaces.add(place);
place.updateTravelogueDay(this);
}

public void updateTravelogue(Travelogue travelogue) {
this.travelogue = travelogue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ private void validateOrderRange(Integer order) {
throw new BadRequestException("여행 장소에 속하는 사진의 순서는 음수가 될 수 없습니다");
}
}

public void updateKey(String key) {
this.key = key;
}

public void updateTraveloguePlace(TraveloguePlace traveloguePlace) {
this.traveloguePlace = traveloguePlace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,13 @@ private void validatePlaceNameLength(String placeName) {
throw new BadRequestException("장소 이름은 " + PLACE_NAME_MAX_LENGTH + "자 이하여야 합니다");
}
}

public void addPhoto(TraveloguePhoto photo) {
traveloguePhotos.add(photo);
photo.updateTraveloguePlace(this);
}

public void updateTravelogueDay(TravelogueDay travelogueDay) {
this.travelogueDay = travelogueDay;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import kr.touroot.travelogue.domain.Travelogue;
import kr.touroot.travelogue.domain.TravelogueDay;
import kr.touroot.travelogue.domain.TraveloguePlace;

public record TravelogueDayRequest(
@Schema(description = "여행기 장소 목록")
Expand All @@ -17,6 +18,16 @@ public record TravelogueDayRequest(
) {

public TravelogueDay toTravelogueDay(int order, Travelogue travelogue) {
return new TravelogueDay(order, travelogue);
TravelogueDay travelogueDay = new TravelogueDay(order, travelogue);
addTraveloguePlaces(travelogueDay);
return travelogueDay;
}

private void addTraveloguePlaces(TravelogueDay travelogueDay) {
for (int placeOrder = 0; placeOrder < places.size(); placeOrder++) {
TraveloguePlaceRequest traveloguePlaceRequest = places.get(placeOrder);
TraveloguePlace traveloguePlace = traveloguePlaceRequest.toTraveloguePlace(placeOrder, travelogueDay);
travelogueDay.addPlace(traveloguePlace);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import kr.touroot.travelogue.domain.TraveloguePhoto;
import kr.touroot.travelogue.domain.TraveloguePlace;

public record TraveloguePhotoRequest(
@Schema(description = "여행기 장소 사진 URL", example = "S3 이미지 URL")
@NotBlank(message = "여행기 장소 사진 URL 값은 비어있을 수 없습니다.")
String url
) {
public TraveloguePhoto toTraveloguePhoto(Integer order, TraveloguePlace traveloguePlace) {
return new TraveloguePhoto(order, url, traveloguePlace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.validation.constraints.Size;
import java.util.List;
import kr.touroot.travelogue.domain.TravelogueDay;
import kr.touroot.travelogue.domain.TraveloguePhoto;
import kr.touroot.travelogue.domain.TraveloguePlace;

public record TraveloguePlaceRequest(
Expand All @@ -29,6 +30,23 @@ public record TraveloguePlaceRequest(
) {

public TraveloguePlace toTraveloguePlace(int order, TravelogueDay travelogueDay) {
return new TraveloguePlace(order, description, placeName, position().lat(), position().lng(), travelogueDay);
TraveloguePlace traveloguePlace = new TraveloguePlace(
order,
description,
placeName,
position().lat(),
position().lng(),
travelogueDay
);
addTraveloguePhotos(traveloguePlace);
return traveloguePlace;
}

private void addTraveloguePhotos(TraveloguePlace traveloguePlace) {
for (int photoOrder = 0; photoOrder < photoUrls.size(); photoOrder++) {
TraveloguePhotoRequest traveloguePhotoRequest = photoUrls.get(photoOrder);
TraveloguePhoto traveloguePhoto = traveloguePhotoRequest.toTraveloguePhoto(photoOrder, traveloguePlace);
traveloguePlace.addPhoto(traveloguePhoto);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
import kr.touroot.member.domain.Member;
import kr.touroot.travelogue.domain.Travelogue;
import kr.touroot.travelogue.domain.TravelogueDay;

public record TravelogueRequest(
@Schema(description = "여행기 제목", example = "서울 강남 여행기")
Expand All @@ -28,7 +30,28 @@ public record TravelogueRequest(
List<TravelogueDayRequest> days
) {

public Travelogue toTravelogueOf(Member author, String url) {
return new Travelogue(author, title, url);
public Travelogue toTravelogue(Member author) {
Travelogue travelogue = new Travelogue(author, title, thumbnail);
addTravelogueDays(travelogue);
return travelogue;
}

private void addTravelogueDays(Travelogue travelogue) {
for (int dayOrder = 0; dayOrder < days.size(); dayOrder++) {
TravelogueDayRequest dayRequest = days.get(dayOrder);
TravelogueDay travelogueDay = dayRequest.toTravelogueDay(dayOrder, travelogue);
travelogue.addDay(travelogueDay);
}
}

public List<TravelogueDay> getTravelogueDays(Travelogue travelogue) {
List<TravelogueDay> travelogueDays = new ArrayList<>();
for (int dayOrder = 0; dayOrder < days.size(); dayOrder++) {
TravelogueDayRequest dayRequest = days.get(dayOrder);
TravelogueDay travelogueDay = dayRequest.toTravelogueDay(dayOrder, travelogue);
travelogueDays.add(travelogueDay);
}

return travelogueDays;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ public record TravelogueDayResponse(
List<TraveloguePlaceResponse> places
) {

public static TravelogueDayResponse of(TravelogueDay day, List<TraveloguePlaceResponse> places) {
public static TravelogueDayResponse from(TravelogueDay travelogueDay) {
return TravelogueDayResponse.builder()
.id(day.getId())
.places(places)
.id(travelogueDay.getId())
.places(getTraveloguePlaceResponse(travelogueDay))
.build();
}

private static List<TraveloguePlaceResponse> getTraveloguePlaceResponse(TravelogueDay travelogueDay) {
return travelogueDay.getTraveloguePlaces().stream()
.map(TraveloguePlaceResponse::from)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import kr.touroot.travelogue.domain.TraveloguePhoto;
import kr.touroot.travelogue.domain.TraveloguePlace;
import lombok.Builder;

Expand All @@ -17,13 +18,19 @@ public record TraveloguePlaceResponse(
List<String> photoUrls
) {

public static TraveloguePlaceResponse of(TraveloguePlace place, List<String> photoUrls) {
public static TraveloguePlaceResponse from(TraveloguePlace traveloguePlace) {
return TraveloguePlaceResponse.builder()
.id(place.getId())
.placeName(place.getName())
.description(place.getDescription())
.position(TraveloguePositionResponse.from(place.getPosition()))
.photoUrls(photoUrls)
.id(traveloguePlace.getId())
.placeName(traveloguePlace.getName())
.description(traveloguePlace.getDescription())
.position(TraveloguePositionResponse.from(traveloguePlace.getPosition()))
.photoUrls(getTraveloguePhotosResponse(traveloguePlace))
.build();
}

private static List<String> getTraveloguePhotosResponse(TraveloguePlace traveloguePlace) {
return traveloguePlace.getTraveloguePhotos().stream()
.map(TraveloguePhoto::getKey)
.toList();
}
}
Loading
Loading