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: redirectUrl 추상화 #679

Merged
merged 9 commits into from
Dec 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.zzang.chongdae.offeringmember.exception.OfferingMemberErrorCode;
import com.zzang.chongdae.offeringmember.repository.OfferingMemberRepository;
import com.zzang.chongdae.offeringmember.repository.entity.OfferingMemberEntity;
import com.zzang.chongdae.storage.service.StorageService;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -39,6 +40,7 @@ public class CommentService {
private final CommentRepository commentRepository;
private final OfferingRepository offeringRepository;
private final OfferingMemberRepository offeringMemberRepository;
private final StorageService storageService;

@WriterDatabase
public Long saveComment(CommentSaveRequest request, MemberEntity member) {
Expand Down Expand Up @@ -91,9 +93,10 @@ public CommentRoomInfoResponse getCommentRoomInfo(Long offeringId, MemberEntity
OfferingMemberEntity offeringMember = offeringMemberRepository.findByOfferingIdAndMember(offeringId, member)
.orElseThrow(() -> new MarketException(OfferingMemberErrorCode.NOT_FOUND));
if (offeringRepository.existsById(offeringId)) {
return new CommentRoomInfoResponse(offeringMember.getOffering(), offeringMember.getMember());
return new CommentRoomInfoResponse(offeringMember.getOffering(), offeringMember.getMember(),
storageService.getResourceHost());
}
return new CommentRoomInfoResponse(offeringMember);
return new CommentRoomInfoResponse(offeringMember, storageService.getResourceHost());
}

@WriterDatabase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;
import com.zzang.chongdae.offeringmember.repository.entity.OfferingMemberEntity;
import java.util.Arrays;
import org.springframework.web.util.UriComponentsBuilder;

public record CommentRoomInfoResponse(CommentRoomStatus status,
String imageUrl,
Expand All @@ -21,18 +22,18 @@ public record CommentRoomInfoResponse(CommentRoomStatus status,
String title,
boolean isProposer) {

public CommentRoomInfoResponse(OfferingEntity offering, MemberEntity member) {
public CommentRoomInfoResponse(OfferingEntity offering, MemberEntity member, String resourceHost) {
this(offering.getRoomStatus(),
ViewMapper.toImage(offering.getRoomStatus()),
ViewMapper.toImage(offering.getRoomStatus(), resourceHost),
ViewMapper.toButton(offering.getRoomStatus()),
ViewMapper.toMessage(offering.getRoomStatus()),
offering.getTitle(),
offering.isProposedBy(member));
}

public CommentRoomInfoResponse(OfferingMemberEntity offeringMember) {
public CommentRoomInfoResponse(OfferingMemberEntity offeringMember, String resourceHost) {
this(DELETED,
ViewMapper.toImage(DELETED),
ViewMapper.toImage(DELETED, resourceHost),
ViewMapper.toButton(DELETED),
ViewMapper.toMessage(DELETED),
"삭제된 공동구매입니다.",
Expand All @@ -59,8 +60,13 @@ private enum ViewMapper {
this.message = message;
}

private static String toImage(CommentRoomStatus status) {
return findViewMapper(status).image;
private static String toImage(CommentRoomStatus status, String resourceHost) {
return UriComponentsBuilder.newInstance()
.scheme("https")
.host(resourceHost)
.path(findViewMapper(status).image)
.build(false)
.toString();
Copy link
Contributor

Choose a reason for hiding this comment

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

이 메서드에서 findViewMapper(status).image를 필드로 추출하는 것은 어떨까요? 처음 코드를 읽었을 때 이 메서드의 의도를 한번에 빠르게 파악하기는 어려웠던 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

밑에 에버가 코멘트 드린 메서드 형태처럼 리팩터링 한 것으로 해당 의견은 반영한 것으로 이해하면 될까요? 😄

이부분이 좀 더러워보여서 아래처럼 리팩터링 진행완료했습니다~

}

private static String toButton(CommentRoomStatus status) {
Expand All @@ -79,7 +85,7 @@ private static ViewMapper findViewMapper(CommentRoomStatus status) {
}

private static String imageUrl(String status) {
String imageUrlFormat = "https://d3a5rfnjdz82qu.cloudfront.net/chongdae-market/images/common/%s.png";
String imageUrlFormat = "/common/%s.png";
return String.format(imageUrlFormat, status);
}
}
Expand Down