Skip to content

Commit

Permalink
refactor: Entity 필드 명 변경
Browse files Browse the repository at this point in the history
- Product, User 객체를 받는 필드 productId -> product, buyerId -> buyer, sellerId -> seller로 변경

- 필드명에 맞춰 Repository 수정

Resolves: #71
  • Loading branch information
JunBe committed Feb 14, 2025
1 parent da49e02 commit 9d2eb08
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ChatRoomController {

@PostMapping()
public ResponseEntity<ChatRoomResponse> createChatRoom(@RequestBody ChatRoomRequest request) {
System.out.println("request.getSellerId() = " + request.getSellerId());
ChatRoomResponse chatRoom = chatRoomService.createOrGetChatRoom(request.getProductId(), request.getBuyerId(), request.getSellerId());

return ResponseEntity.ok(chatRoom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class ChatRoomResponse {
public static ChatRoomResponse fromEntity(ChatRoom chatRoom, String lastMessage, LocalDateTime lastMessageTime) {
return ChatRoomResponse.builder()
.roomId(chatRoom.getId())
.productId(chatRoom.getProductId().getId())
.productTitle(chatRoom.getProductId().getTitle())
.buyerId(chatRoom.getBuyerId().getId())
.sellerId(chatRoom.getSellerId().getId())
.productId(chatRoom.getProduct().getId())
.productTitle(chatRoom.getProduct().getTitle())
.buyerId(chatRoom.getBuyer().getId())
.sellerId(chatRoom.getSeller().getId())
.lastMessage(lastMessage)
.lastMessageTime(lastMessageTime)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public class ChatRoom {

@ManyToOne
@JoinColumn(name = "product_id")
private Product productId;
private Product product;

@ManyToOne
@JoinColumn(name = "buyer_id")
private User buyerId;
private User buyer;

@ManyToOne
@JoinColumn(name = "seller_id")
private User sellerId;
private User seller;

@OneToMany(mappedBy = "chatRoom", cascade = CascadeType.ALL)
private List<ChatMessage> message = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Optional;

public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
Optional<ChatRoom> findByProductIdAndBuyerIdAndSellerId(Product product, User buyer, User seller);
Optional<ChatRoom> findByProductAndBuyerAndSeller(Product product, User buyer, User seller);

List<ChatRoom> findByBuyerIdOrSellerId(User buyer, User seller);
List<ChatRoom> findByBuyerOrSeller(User buyer, User seller);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public ChatRoomResponse createOrGetChatRoom(Long productId, Long buyerId, Long s
User seller = userRepository.findById(sellerId)
.orElseThrow(() -> new IllegalArgumentException("판매자를 찾을 수 없습니다."));

ChatRoom chatRoom = chatRoomRepository.findByProductIdAndBuyerIdAndSellerId(product, buyer, seller)
ChatRoom chatRoom = chatRoomRepository.findByProductAndBuyerAndSeller(product, buyer, seller)
.orElseGet(() -> {
ChatRoom newRoom = ChatRoom.builder()
.productId(product)
.buyerId(buyer)
.sellerId(seller)
.product(product)
.buyer(buyer)
.seller(seller)
.build();

return chatRoomRepository.save(newRoom);
Expand All @@ -57,7 +57,7 @@ public List<ChatRoomResponse> getUserChatRooms(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("유저가 없습니다."));

List<ChatRoom> chatRooms = chatRoomRepository.findByBuyerIdOrSellerId(user, user);
List<ChatRoom> chatRooms = chatRoomRepository.findByBuyerOrSeller(user, user);

return chatRooms.stream()
.map(chatRoom -> {
Expand Down

0 comments on commit 9d2eb08

Please sign in to comment.