From cd3f4c0d2e9c0c97f047cfa4ec96806aa7f26087 Mon Sep 17 00:00:00 2001 From: wonseok Date: Tue, 21 Jan 2025 18:29:24 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20message=20paging?= =?UTF-8?q?=20from=2010=20to=2030?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../karrot/chatRoom/persistence/ChatMessageRepository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatMessageRepository.kt b/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatMessageRepository.kt index 90e77a5..593855d 100644 --- a/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatMessageRepository.kt +++ b/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatMessageRepository.kt @@ -4,7 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository import java.time.Instant interface ChatMessageRepository : JpaRepository { - fun findTop10ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc( + fun findTop30ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc( chatRoomId: Long, createdAt: Instant, ): List From 64ec9bda85de0a7b8ebe010139e67632e290a1c2 Mon Sep 17 00:00:00 2001 From: wonseok Date: Tue, 21 Jan 2025 18:35:47 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20service=2010->30?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt b/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt index f85449b..12fd6be 100644 --- a/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt @@ -68,7 +68,7 @@ class ChatRoomService( if (chatRoom.buyer != user && chatRoom.seller != user) throw ThisRoomIsNotYoursException() val chatMessageEntities: List = - chatMessageRepository.findTop10ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc( + chatMessageRepository.findTop30ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc( chatRoomId = chatRoomId, createdAt = createdAt, ) From 9a1b395acb3b9dcde777cb3623f5259fda57b2be Mon Sep 17 00:00:00 2001 From: wonseok Date: Tue, 21 Jan 2025 18:48:26 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A7=20List=20to=20Cha?= =?UTF-8?q?tRoomResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../karrot/chatRoom/controller/ChatRoomController.kt | 12 +++++++++--- .../karrot/chatRoom/service/ChatRoomService.kt | 9 +++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/toyProject7/karrot/chatRoom/controller/ChatRoomController.kt b/src/main/kotlin/com/toyProject7/karrot/chatRoom/controller/ChatRoomController.kt index 44a1ae9..a59256e 100644 --- a/src/main/kotlin/com/toyProject7/karrot/chatRoom/controller/ChatRoomController.kt +++ b/src/main/kotlin/com/toyProject7/karrot/chatRoom/controller/ChatRoomController.kt @@ -1,5 +1,6 @@ package com.toyProject7.karrot.chatRoom.controller +import com.toyProject7.karrot.article.controller.Article import com.toyProject7.karrot.chatRoom.service.ChatRoomService import com.toyProject7.karrot.user.AuthUser import com.toyProject7.karrot.user.controller.User @@ -32,9 +33,9 @@ class ChatRoomController( @PathVariable chatRoomId: Long, @AuthUser user: User, @RequestParam("createdAt") createdAt: Instant, - ): ResponseEntity> { - val chatRoom = chatRoomService.getChatRoom(chatRoomId, user, createdAt) - return ResponseEntity.ok(chatRoom) + ): ResponseEntity { + val chatRoomResponse = chatRoomService.getChatRoom(chatRoomId, user, createdAt) + return ResponseEntity.ok(chatRoomResponse) } @PostMapping("/chat/create") @@ -51,3 +52,8 @@ data class CreateChatRoomRequest( val sellerId: String, val buyerId: String, ) + +data class ChatRoomResponse( + val article: Article, + val messages: List, +) diff --git a/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt b/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt index 12fd6be..4752b62 100644 --- a/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/chatRoom/service/ChatRoomService.kt @@ -6,6 +6,7 @@ import com.toyProject7.karrot.chatRoom.ChatRoomNotFoundException import com.toyProject7.karrot.chatRoom.ThisRoomIsNotYoursException import com.toyProject7.karrot.chatRoom.controller.ChatMessage import com.toyProject7.karrot.chatRoom.controller.ChatRoom +import com.toyProject7.karrot.chatRoom.controller.ChatRoomResponse import com.toyProject7.karrot.chatRoom.persistence.ChatMessageEntity import com.toyProject7.karrot.chatRoom.persistence.ChatMessageRepository import com.toyProject7.karrot.chatRoom.persistence.ChatRoomEntity @@ -62,7 +63,7 @@ class ChatRoomService( chatRoomId: Long, user: User, createdAt: Instant, - ): List { + ): ChatRoomResponse { val chatRoomEntity = chatRoomRepository.findById(chatRoomId).orElseThrow { ChatRoomNotFoundException() } val chatRoom = ChatRoom.fromEntity(chatRoomEntity, "") if (chatRoom.buyer != user && chatRoom.seller != user) throw ThisRoomIsNotYoursException() @@ -72,7 +73,11 @@ class ChatRoomService( chatRoomId = chatRoomId, createdAt = createdAt, ) - return chatMessageEntities.map { chatMessageEntity -> ChatMessage.fromEntity(chatMessageEntity) } + val messages = chatMessageEntities.map { chatMessageEntity -> ChatMessage.fromEntity(chatMessageEntity) } + return ChatRoomResponse( + article = chatRoom.article, + messages = messages, + ) } @Transactional From 41c122c356cf0a6bee37619bd316508ac9b877ce Mon Sep 17 00:00:00 2001 From: wonseok Date: Tue, 21 Jan 2025 19:13:55 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=A7=20Add=20chattingUsers=20into?= =?UTF-8?q?=20ArticleResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../article/controller/ArticleController.kt | 20 +++++++++++++------ .../karrot/article/service/ArticleService.kt | 13 ++++++++++++ .../persistence/ChatRoomRepository.kt | 2 ++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt b/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt index 8728a2b..7f2d7b8 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt @@ -24,9 +24,10 @@ class ArticleController( fun postArticle( @RequestBody request: PostArticleRequest, @AuthUser user: User, - ): ResponseEntity
{ + ): ResponseEntity { val article = articleService.postArticle(request, user.id) - return ResponseEntity.ok(article) + val chattingUsers = articleService.getChattingUsersByArticle(article) + return ResponseEntity.ok(ArticleResponse(article, chattingUsers)) } @PutMapping("/item/edit/{articleId}") @@ -34,9 +35,10 @@ class ArticleController( @RequestBody request: PostArticleRequest, @PathVariable articleId: Long, @AuthUser user: User, - ): ResponseEntity
{ + ): ResponseEntity { val article = articleService.editArticle(articleId, request, user.id) - return ResponseEntity.ok(article) + val chattingUsers = articleService.getChattingUsersByArticle(article) + return ResponseEntity.ok(ArticleResponse(article, chattingUsers)) } @DeleteMapping("/item/delete/{articleId}") @@ -80,9 +82,10 @@ class ArticleController( fun getArticle( @PathVariable articleId: Long, @AuthUser user: User, - ): ResponseEntity
{ + ): ResponseEntity { val article = articleService.getArticle(articleId, user.id) - return ResponseEntity.ok(article) + val chattingUsers = articleService.getChattingUsersByArticle(article) + return ResponseEntity.ok(ArticleResponse(article, chattingUsers)) } @GetMapping("/home") @@ -149,3 +152,8 @@ data class PostArticleRequest( data class UpdateStatusRequest( val status: Int, ) + +data class ArticleResponse( + val article: Article, + val chattingUsers: List, +) diff --git a/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt b/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt index 3207d68..bcc78c6 100644 --- a/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt +++ b/src/main/kotlin/com/toyProject7/karrot/article/service/ArticleService.kt @@ -9,8 +9,12 @@ import com.toyProject7.karrot.article.persistence.ArticleEntity import com.toyProject7.karrot.article.persistence.ArticleLikesEntity import com.toyProject7.karrot.article.persistence.ArticleLikesRepository import com.toyProject7.karrot.article.persistence.ArticleRepository +import com.toyProject7.karrot.chatRoom.controller.ChatRoom +import com.toyProject7.karrot.chatRoom.persistence.ChatRoomEntity +import com.toyProject7.karrot.chatRoom.persistence.ChatRoomRepository import com.toyProject7.karrot.image.persistence.ImageUrlEntity import com.toyProject7.karrot.image.service.ImageService +import com.toyProject7.karrot.user.controller.User import com.toyProject7.karrot.user.service.UserService import org.springframework.context.annotation.Lazy import org.springframework.data.repository.findByIdOrNull @@ -25,6 +29,7 @@ class ArticleService( private val articleLikesRepository: ArticleLikesRepository, private val userService: UserService, @Lazy private val imageService: ImageService, + private val chatRoomRepository: ChatRoomRepository, ) { @Transactional fun postArticle( @@ -249,4 +254,12 @@ class ArticleService( fun getArticleEntityById(articleId: Long): ArticleEntity { return articleRepository.findByIdOrNull(articleId) ?: throw ArticleNotFoundException() } + + @Transactional + fun getChattingUsersByArticle(article: Article): List { + val chatRoomEntities: List = chatRoomRepository.findAllByArticleId(article.id) + return chatRoomEntities + .map { chatRoomEntity -> ChatRoom.fromEntity(chatRoomEntity, "") } + .map { chatRoom -> chatRoom.buyer } + } } diff --git a/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatRoomRepository.kt b/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatRoomRepository.kt index 34efca5..631648e 100644 --- a/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatRoomRepository.kt +++ b/src/main/kotlin/com/toyProject7/karrot/chatRoom/persistence/ChatRoomRepository.kt @@ -9,4 +9,6 @@ interface ChatRoomRepository : JpaRepository { buyerId: String, updatedAt: Instant, ): List + + fun findAllByArticleId(articleId: Long): List }