Skip to content

Commit

Permalink
이미지url에 id 대신 파일 이름을 사용하도록 변경 (#717)
Browse files Browse the repository at this point in the history
* refactor: 이미지 상대 url 기반으로 url을 계산하는 메서드 파라미터로 이미지 id 대신 이미지 이름을 받도록 변경

* refactor: 이미지 절대 url 기반으로 url을 계산하는 메서드 파라미터로 이미지 id 대신 이미지 이름을 받도록 변경

* refactor: 경매 조회 시 판매자 프로필 이미지 조회로 인한 n+1 개선

* refactor: 입찰 목록 조회 시 입찰자 정보 조회로 인한 n+1 개선

* refactor: 채팅방 관련 조회 시 참여자 프로필 이미지 조회로 인한 n+1 개선

* refactor: qna 관련 조회 시 사용자 프로필 이미지 조회로 인한 n+1 개선

* refactor: 신고 관련 조회 시 사용자 프로필 이미지 조회로 인한 n+1 개선

* refactor: 평가 관련 조회 시 사용자 프로필 이미지 조회로 인한 n+1 개선

* refactor: 사용자 정보 조회 시 사용자 프로필 이미지 조회로 인한 n+1 개선

* refactor: 메시지 알림 시 작성자 프로필 이미지 조회로 인한 n+1 개선

* feat: 이미지 이름이 포함된 url로 조회 시 이미지 파일을 반환하는 기능 추가

* docs: 문서 최신화

* style: 개행 수정 및 누락된 final 추가

* rename: test에 사용되는 fixture 클래스 이름 변경

* ci: 충돌 해결
  • Loading branch information
kwonyj1022 authored Nov 17, 2023
1 parent 8cc9218 commit 07bad46
Show file tree
Hide file tree
Showing 83 changed files with 410 additions and 393 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.ddang.ddang.auction.application.dto;

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.image.application.util.ImageStoreNameProcessor;

public record CreateInfoAuctionDto(
Long id,
String title,
Long auctionImageId,
String auctionStoreName,
int startPrice
) {

public static CreateInfoAuctionDto from(final Auction auction) {
return new CreateInfoAuctionDto(
auction.getId(),
auction.getTitle(),
auction.getAuctionImages().get(0).getId(),
ImageStoreNameProcessor.process(auction.getAuctionImages().get(0)),
auction.getStartPrice().getValue()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.auction.domain.AuctionStatus;
import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.image.application.util.ImageIdProcessor;
import com.ddang.ddang.image.domain.AuctionImage;
import com.ddang.ddang.image.application.util.ImageStoreNameProcessor;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -20,12 +19,12 @@ public record ReadAuctionDto(
LocalDateTime registerTime,
LocalDateTime closingTime,
List<ReadRegionsDto> auctionRegions,
List<Long> auctionImageIds,
List<String> auctionImageStoreNames,
int auctioneerCount,
String mainCategory,
String subCategory,
Long sellerId,
Long sellerProfileId,
String sellerProfileImageStoreName,
String sellerName,
double sellerReliability,
boolean isSellerDeleted,
Expand All @@ -45,12 +44,12 @@ public static ReadAuctionDto of(final Auction auction, final LocalDateTime targe
auction.getCreatedTime(),
auction.getClosingTime(),
convertReadRegionsDto(auction),
convertImageIds(auction),
convertImageStoreNames(auction),
auction.getAuctioneerCount(),
auction.getSubCategory().getMainCategory().getName(),
auction.getSubCategory().getName(),
auction.getSeller().getId(),
ImageIdProcessor.process(auction.getSeller().getProfileImage()),
ImageStoreNameProcessor.process(auction.getSeller().getProfileImage()),
auction.getSeller().getName(),
auction.getSeller().getReliability().getValue(),
auction.getSeller().isDeleted(),
Expand All @@ -59,10 +58,10 @@ public static ReadAuctionDto of(final Auction auction, final LocalDateTime targe
);
}

private static List<Long> convertImageIds(final Auction auction) {
private static List<String> convertImageStoreNames(final Auction auction) {
return auction.getAuctionImages()
.stream()
.map(AuctionImage::getId)
.map(ImageStoreNameProcessor::process)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public interface JpaAuctionRepository extends JpaRepository<Auction, Long> {
LEFT JOIN FETCH a.lastBid
JOIN FETCH a.subCategory sc
JOIN FETCH sc.mainCategory
JOIN FETCH a.seller
JOIN FETCH a.seller seller
LEFT JOIN FETCH seller.profileImage
WHERE a.deleted = false AND a.id = :id
""")
Optional<Auction> findTotalAuctionById(final Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private List<Auction> findAuctionsByIdsAndOrderSpecifiers(
.join(auction.subCategory, category).fetchJoin()
.join(category.mainCategory).fetchJoin()
.join(auction.seller).fetchJoin()
.join(auction.seller.profileImage).fetchJoin()
.where(auction.id.in(targetIds.toArray(Long[]::new)))
.orderBy(orderSpecifiers.toArray(OrderSpecifier[]::new))
.fetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public static AuctionDetailResponse from(final ReadAuctionDto dto) {
}

private static List<String> convertImageFullUrls(final ReadAuctionDto dto) {
return dto.auctionImageIds()
return dto.auctionImageStoreNames()
.stream()
.map(id -> ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, id))
.map(imageStoreName -> ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, imageStoreName))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public static CreateAuctionResponse from(final CreateInfoAuctionDto dto) {
return new CreateAuctionResponse(
dto.id(),
dto.title(),
convertAuctionImageUrl(dto.auctionImageId()),
convertAuctionImageUrl(dto.auctionStoreName()),
dto.startPrice(),
AuctionStatus.UNBIDDEN.name(),
0
);
}

private static String convertAuctionImageUrl(final Long id) {
return ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, id);
private static String convertAuctionImageUrl(final String storeName) {
return ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, storeName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static ReadAuctionResponse from(final ReadAuctionDto dto) {
return new ReadAuctionResponse(
dto.id(),
dto.title(),
ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, dto.auctionImageIds().get(0)),
ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, dto.auctionImageStoreNames().get(0)),
processAuctionPrice(dto.startPrice(), dto.lastBidPrice()),
dto.auctionStatus().name(),
dto.auctioneerCount()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static ReadUserInAuctionQuestionResponse from(final ReadUserInQnaDto writ
return new ReadUserInAuctionQuestionResponse(
writerDto.id(),
NameProcessor.process(writerDto.isDeleted(), writerDto.name()),
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, writerDto.profileImageId())
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, writerDto.profileImageStoreName())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public record SellerResponse(
public static SellerResponse from(final ReadAuctionDto auctionDto) {
return new SellerResponse(
auctionDto.sellerId(),
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, auctionDto.sellerProfileId()),
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, auctionDto.sellerProfileImageStoreName()),
NameProcessor.process(auctionDto.isSellerDeleted(), auctionDto.sellerName()),
ReliabilityProcessor.process(auctionDto.sellerReliability())
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.ddang.ddang.bid.application.dto;

import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.image.application.util.ImageIdProcessor;
import com.ddang.ddang.image.application.util.ImageStoreNameProcessor;
import com.ddang.ddang.user.domain.User;

import java.time.LocalDateTime;

public record ReadBidDto(
String name,
Long profileImageId,
String profileImageStoreName,
boolean isDeletedUser,
int price,
LocalDateTime bidTime
Expand All @@ -19,7 +19,7 @@ public static ReadBidDto from(final Bid bid) {

return new ReadBidDto(
bidder.getName(),
ImageIdProcessor.process(bidder.getProfileImage()),
ImageStoreNameProcessor.process(bidder.getProfileImage()),
bidder.isDeleted(),
bid.getPrice().getValue(),
bid.getCreatedTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

public interface JpaBidRepository extends JpaRepository<Bid, Long> {

@Query("""
SELECT bid
FROM Bid bid
JOIN FETCH bid.bidder bidder
LEFT JOIN FETCH bidder.profileImage
WHERE bid.auction.id = :auctionId ORDER BY bid.id ASC
""")
List<Bid> findAllByAuctionIdOrderByIdAsc(final Long auctionId);

@Query("SELECT b FROM Bid b WHERE b.auction.id = :auctionId ORDER BY b.id DESC LIMIT 1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static ReadBidResponse from(final ReadBidDto dto) {
final String name = NameProcessor.process(dto.isDeletedUser(), dto.name());
return new ReadBidResponse(
name,
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, dto.profileImageId()),
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, dto.profileImageStoreName()),
dto.price(),
dto.bidTime()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Long create(final CreateMessageDto dto, final String profileImageAbsolute
final ChatRoom chatRoom = chatRoomRepository.findById(dto.chatRoomId())
.orElseThrow(() -> new ChatRoomNotFoundException(
"지정한 아이디에 대한 채팅방을 찾을 수 없습니다."));
final User writer = userRepository.findById(dto.writerId())
final User writer = userRepository.findByIdWithProfileImage(dto.writerId())
.orElseThrow(() -> new UserNotFoundException(
"지정한 아이디에 대한 발신자를 찾을 수 없습니다."));
final User receiver = userRepository.findById(dto.receiverId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.bid.domain.Bid;
import com.ddang.ddang.image.application.util.ImageStoreNameProcessor;
import com.ddang.ddang.image.domain.AuctionImage;

public record ReadAuctionInChatRoomDto(
Long id,
String title,
Integer lastBidPrice,
Long thumbnailImageId
String thumbnailImageStoreName
) {

public static ReadAuctionInChatRoomDto of(final Auction auction, final AuctionImage thumbnailImage) {
return new ReadAuctionInChatRoomDto(
auction.getId(),
auction.getTitle(),
convertPrice(auction.getLastBid()),
thumbnailImage.getId()
ImageStoreNameProcessor.process(thumbnailImage)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.ddang.ddang.chat.application.dto;

import com.ddang.ddang.image.application.util.ImageIdProcessor;
import com.ddang.ddang.image.application.util.ImageStoreNameProcessor;
import com.ddang.ddang.user.domain.User;

public record ReadUserInChatRoomDto(Long id, String name, Long profileImageId, double reliability, boolean isDeleted) {
public record ReadUserInChatRoomDto(Long id, String name, String profileImageStoreName, double reliability, boolean isDeleted) {

public static ReadUserInChatRoomDto from(final User user) {
return new ReadUserInChatRoomDto(
user.getId(),
user.getName(),
ImageIdProcessor.process(user.getProfileImage()),
ImageStoreNameProcessor.process(user.getProfileImage()),
user.getReliability().getValue(),
user.isDeleted()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ public Optional<ChatRoomAndImageDto> findChatRoomById(final Long chatRoomId) {
final ChatRoomAndImageQueryProjectionDto chatRoomAndImageQueryProjectionDto =
queryFactory.select(new QChatRoomAndImageQueryProjectionDto(chatRoom, auctionImage))
.from(chatRoom)
.leftJoin(chatRoom.buyer).fetchJoin()
.leftJoin(chatRoom.auction, auction).fetchJoin()
.leftJoin(auction.seller).fetchJoin()
.join(chatRoom.buyer).fetchJoin()
.join(chatRoom.auction, auction).fetchJoin()
.join(auction.seller).fetchJoin()
.leftJoin(auction.seller.profileImage).fetchJoin()
.leftJoin(auctionImage).on(auctionImage.id.eq(
JPAExpressions
.select(auctionImage.id.min())
.from(auctionImage)
.where(auctionImage.auction.id.eq(auction.id))
.groupBy(auctionImage.auction.id)
)).fetchJoin()
.leftJoin(auction.lastBid).fetchJoin()
.join(auction.lastBid).fetchJoin()
.where(chatRoom.id.eq(chatRoomId))
.fetchOne();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ public List<ChatRoomAndMessageAndImageDto> findAllChatRoomInfoByUserIdOrderByLas
auctionImage,
countUnreadMessages(userId, chatRoom.id)
)).from(chatRoom)
.leftJoin(chatRoom.buyer).fetchJoin()
.leftJoin(chatRoom.auction, auction).fetchJoin()
.leftJoin(auction.seller).fetchJoin()
.join(chatRoom.buyer).fetchJoin()
.join(chatRoom.auction, auction).fetchJoin()
.join(auction.seller).fetchJoin()
.leftJoin(auction.seller.profileImage).fetchJoin()
.leftJoin(auctionImage).on(auctionImage.id.eq(
JPAExpressions
.select(auctionImage.id.min())
.from(auctionImage)
.where(auctionImage.auction.id.eq(auction.id))
.groupBy(auctionImage.auction.id)
)).fetchJoin()
.leftJoin(auction.lastBid).fetchJoin()
.join(auction.lastBid).fetchJoin()
.leftJoin(message).on(message.id.eq(
JPAExpressions
.select(message.id.max())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public record ReadAuctionInChatRoomResponse(Long id, String title, String image, int price) {

public static ReadAuctionInChatRoomResponse from(final ReadAuctionInChatRoomDto dto) {
final String thumbNailImageUrl = ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, dto.thumbnailImageId());
final String thumbNailImageUrl = ImageUrlCalculator.calculateBy(ImageRelativeUrl.AUCTION, dto.thumbnailImageStoreName());

return new ReadAuctionInChatRoomResponse(dto.id(), dto.title(), thumbNailImageUrl, dto.lastBidPrice());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static ReadChatPartnerResponse from(final ReadUserInChatRoomDto dto) {
return new ReadChatPartnerResponse(
dto.id(),
name,
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, dto.profileImageId())
ImageUrlCalculator.calculateBy(ImageRelativeUrl.USER, dto.profileImageStoreName())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ddang.ddang.image.application;

import com.ddang.ddang.image.domain.AuctionImage;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.domain.repository.AuctionImageRepository;
import com.ddang.ddang.image.domain.repository.ProfileImageRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,28 +23,22 @@ public class ImageService {
private final ProfileImageRepository profileImageRepository;
private final AuctionImageRepository auctionImageRepository;

public Resource readProfileImage(final Long id) throws MalformedURLException {
final ProfileImage profileImage = profileImageRepository.findById(id)
.orElse(null);

if (profileImage == null) {
public Resource readProfileImage(final String storeName) throws MalformedURLException {
if (!profileImageRepository.existsByStoreName(storeName)) {
return null;
}

final String fullPath = findFullPath(profileImage.getImage().getStoreName());
final String fullPath = findFullPath(storeName);

return new UrlResource(FILE_PROTOCOL_PREFIX + fullPath);
}

public Resource readAuctionImage(final Long id) throws MalformedURLException {
final AuctionImage auctionImage = auctionImageRepository.findById(id)
.orElse(null);

if (auctionImage == null) {
public Resource readAuctionImage(final String storeName) throws MalformedURLException {
if (!auctionImageRepository.existsByStoreName(storeName)) {
return null;
}

final String fullPath = findFullPath(auctionImage.getImage().getStoreName());
final String fullPath = findFullPath(storeName);

return new UrlResource(FILE_PROTOCOL_PREFIX + fullPath);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 07bad46

Please sign in to comment.