Skip to content

Commit

Permalink
[�feat] : 채팅방 메시지 조회 API 개선 및 응답값 추가 (#111)
Browse files Browse the repository at this point in the history
* [refactor] : 채팅방으로 메시지 조회 메서드 수정

* [test] : setUp 로직 수정

* [test] : chatRoom 생성 시 다른 입찰 대입해 비즈니스 요구사항 지키기

* [feat] : 경매 조회 fetch join 수정

* [test] : 경매 조회 fetch join 수정 반영

* [style] : 코드 리포멧팅

* [test] : 통합 테스트 path variable -> chatRoomId

* [style] : 컨벤션에 맞게 상태 변경 메서드명 수정

* [refactor] : 조회 쿼리 오래된 순 명시

* [feat] : 채팅방 상세 조회 시 경매 이미지 URL 추가
  • Loading branch information
hyun2371 authored Mar 19, 2024
1 parent cbcb5d4 commit 6f8086e
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
@DisplayName("[ChatRoom 통합 테스트]")
class ChatRoomApiControllerTest extends ApiTestSupport {

private final User seller = user; // loginUser
private final User bidder = UserFixture.user2();
private User seller, bidder;
private ProductCategory productCategory;
private Auction auction;
private Bidding bidding;
Expand All @@ -57,14 +56,18 @@ class ChatRoomApiControllerTest extends ApiTestSupport {

@BeforeEach
void setUp() {
seller = user;
bidder = UserFixture.user2();
userRepository.saveAll(List.of(bidder, seller));

productCategory = ProductFixture.productCategory("디지털 기기");
productCategoryRepository.save(productCategory);

auction = AuctionFixture.auction(seller, productCategory);
ReflectionTestUtils.setField(auction, "status", AuctionStatus.TRADING);
auctionRepository.save(auction);
bidding = BiddingFixture.bidding(auction, bidder);
biddingRepository.save(bidding);

biddingRepository.save(bidding = BiddingFixture.bidding(auction, bidder));
}

@DisplayName("[기존 채팅방이 없으면, 채팅방을 생성할 수 있다.]")
Expand Down Expand Up @@ -226,8 +229,10 @@ void getChatRoomWithBiddingId_fails() throws Exception {
@Test
void getChatRoomMessages() throws Exception {
//given
User user1 = userRepository.save(UserFixture.user1());
Bidding bidding2 = biddingRepository.save(BiddingFixture.bidding(auction, user1));
ChatRoom chatRoom1 = ChatRoomFixture.chatRoom(seller, bidding);
ChatRoom chatRoom2 = ChatRoomFixture.chatRoom(seller, bidding);
ChatRoom chatRoom2 = ChatRoomFixture.chatRoom(seller, bidding2);
chatRoomRepository.saveAll(List.of(chatRoom1, chatRoom2));

ChatMessage chatMessage1 = ChatMessageFixture.chatMessage(chatRoom1, bidder);
Expand All @@ -236,7 +241,7 @@ void getChatRoomMessages() throws Exception {
chatMessageRepository.saveAll(List.of(chatMessage1, chatMessage2, otherChatMessage3));

//when, then
mockMvc.perform(get("/api/auctions/chat-rooms/{chatRoomId}/messages", bidding.getId())
mockMvc.perform(get("/api/auctions/chat-rooms/{chatRoomId}/messages", chatRoom1.getId())
.header(AUTHORIZATION, "Bearer " + accessToken)
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/dev/handsup/auction/domain/Auction.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ public static Auction of(Long id, User seller, String title, ProductCategory pro
);
}

public void changeAuctionStatusTrading() {
public void updateAuctionStatusTrading() {
status = AuctionStatus.TRADING;
}

public void changeAuctionStatusCompleted() {
public void updateAuctionStatusCompleted() {
if (status != TRADING) {
throw new ValidationException(AuctionErrorCode.CAN_NOT_COMPLETE_AUCTION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public interface BiddingRepository extends JpaRepository<Bidding, Long> {
+ "WHERE b.id = :id")
Optional<Bidding> findBiddingWithAuctionAndBidder(@Param("id") Long id);

@Query("SELECT b FROM Bidding b "
+ "JOIN FETCH b.auction "
+ "WHERE b.id = :id")
Optional<Bidding> findBiddingWithAuction(@Param("id") Long id);

@Query("SELECT MAX(b.biddingPrice) FROM Bidding b WHERE b.auction.id = :auctionId")
Integer findMaxBiddingPriceByAuctionId(Long auctionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ public BiddingResponse completeTrading(Long biddingId, User seller) {

bidding.updateTradingStatusComplete();
bidding.getAuction().updateBuyer(bidding.getBidder());

// 거래 완료 알림 추가
bidding.getAuction().changeAuctionStatusCompleted();

bidding.getAuction().updateAuctionStatusCompleted();
sendMessage(seller, bidding, NotificationType.COMPLETED_PURCHASE_TRADING);

return BiddingMapper.toBiddingResponse(bidding);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static ChatRoomDetailResponse toChatRoomDetailResponse(ChatRoom chatRoom,
bidding.getBiddingPrice(),
bidding.getTradingStatus().getLabel(),
bidding.getAuction().getTitle(),
bidding.getAuction().getProduct().getImages().get(0).getImageUrl(),
receiver.getId(),
receiver.getNickname(),
receiver.getScore(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record ChatRoomDetailResponse(
int currentBiddingPrice,
String tradingStatus,
String auctionTitle,
String auctionImageUrl,
Long receiverId,
String receiverNickName,
int receiverScore,
Expand All @@ -19,6 +20,7 @@ public static ChatRoomDetailResponse of(
int currentBiddingPrice,
String tradingStatus,
String auctionTitle,
String auctionImageUrl,
Long receiverId,
String receiverNickName,
int receiverScore,
Expand All @@ -31,6 +33,7 @@ public static ChatRoomDetailResponse of(
currentBiddingPrice,
tradingStatus,
auctionTitle,
auctionImageUrl,
receiverId,
receiverNickName,
receiverScore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class ChatMessageService {
@Transactional
public ChatMessageResponse registerChatMessage(Long chatRoomId, ChatMessageRequest request) {
ChatRoom chatRoom = getChatRoomById(chatRoomId);

ChatMessage chatMessage = ChatMessageMapper.toChatMessage(chatRoom, request);
ChatMessage savedChatMessage = chatMessageRepository.save(chatMessage);

Expand Down
13 changes: 9 additions & 4 deletions core/src/main/java/dev/handsup/chat/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ChatRoomService {

@Transactional
public RegisterChatRoomResponse registerChatRoom(Long auctionId, Long biddingId, User user) {
Bidding bidding = getBiddingById(biddingId);
Bidding bidding = getBiddingWithAuctionAndBidderById(biddingId);
validateAuthorization(user, bidding);
validateAuctionTrading(bidding.getAuction());

Expand Down Expand Up @@ -72,15 +72,15 @@ public PageResponse<ChatRoomSimpleResponse> getUserChatRooms(User user, Pageable
@Transactional(readOnly = true)
public ChatRoomDetailResponse getChatRoomWithId(Long chatRoomId, User user) {
ChatRoom chatRoom = getChatRoomById(chatRoomId);
Bidding currentBidding = getBiddingById(chatRoom.getCurrentBiddingId());
Bidding currentBidding = getBiddingWithAuctionById(chatRoom.getCurrentBiddingId());
User receiver = getReceiver(user, chatRoom);
return ChatRoomMapper.toChatRoomDetailResponse(chatRoom, currentBidding, receiver);
}

// 입찰자 목록에서 조회
@Transactional(readOnly = true)
public ChatRoomDetailResponse getChatRoomWithBiddingId(Long biddingId, User user) {
Bidding bidding = getBiddingById(biddingId);
Bidding bidding = getBiddingWithAuctionAndBidderById(biddingId);
validateAuthorization(user, bidding);
User receiver = bidding.getBidder();
ChatRoom chatRoom = getChatRoomByCurrentBidding(bidding);
Expand Down Expand Up @@ -116,11 +116,16 @@ private void validateAuctionTrading(Auction auction) {
}
}

private Bidding getBiddingById(Long biddingId) {
private Bidding getBiddingWithAuctionAndBidderById(Long biddingId) {
return biddingRepository.findBiddingWithAuctionAndBidder(biddingId)
.orElseThrow(() -> new NotFoundException(BiddingErrorCode.NOT_FOUND_BIDDING));
}

private Bidding getBiddingWithAuctionById(Long biddingId) {
return biddingRepository.findBiddingWithAuction(biddingId)
.orElseThrow(() -> new NotFoundException(BiddingErrorCode.NOT_FOUND_BIDDING));
}

private ChatRoom getChatRoomById(Long chatRoomId) {
return chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new NotFoundException(ChatRoomErrorCode.NOT_FOUND_CHAT_ROOM));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void searchAuction_isProgress_filter() {
Auction auction1 = AuctionFixture.auction(category1);
Auction auction2 = AuctionFixture.auction(category2);
Auction auction3 = AuctionFixture.auction(category2);
auction1.changeAuctionStatusTrading();
auction1.updateAuctionStatusTrading();
auctionRepository.saveAll(List.of(auction1, auction2, auction3));

AuctionSearchCondition condition = AuctionSearchCondition.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void getChatRoomWithId() {
Bidding bidding = BiddingFixture.bidding(auction, bidder, TradingStatus.PREPARING);
ChatRoom chatRoom = ChatRoomFixture.chatRoom(bidding);

given(biddingRepository.findBiddingWithAuctionAndBidder(anyLong())).willReturn(Optional.of(bidding));
given(biddingRepository.findBiddingWithAuction(anyLong())).willReturn(Optional.of(bidding));
given(chatRoomRepository.findById(chatRoom.getId())).willReturn(Optional.of(chatRoom));

//when
Expand Down

0 comments on commit 6f8086e

Please sign in to comment.