Skip to content

Commit

Permalink
Merge pull request #49 from wafflestudio/feat/chat
Browse files Browse the repository at this point in the history
Feat/chat
  • Loading branch information
ws11222 authored Jan 22, 2025
2 parents 70dc969 + 41c122c commit 2602d17
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ class ArticleController(
fun postArticle(
@RequestBody request: PostArticleRequest,
@AuthUser user: User,
): ResponseEntity<Article> {
): ResponseEntity<ArticleResponse> {
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}")
fun editArticle(
@RequestBody request: PostArticleRequest,
@PathVariable articleId: Long,
@AuthUser user: User,
): ResponseEntity<Article> {
): ResponseEntity<ArticleResponse> {
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}")
Expand Down Expand Up @@ -80,9 +82,10 @@ class ArticleController(
fun getArticle(
@PathVariable articleId: Long,
@AuthUser user: User,
): ResponseEntity<Article> {
): ResponseEntity<ArticleResponse> {
val article = articleService.getArticle(articleId, user.id)
return ResponseEntity.ok(article)
val chattingUsers = articleService.getChattingUsersByArticle(article)
return ResponseEntity.ok(ArticleResponse(article, chattingUsers))
}

@GetMapping("/home")
Expand Down Expand Up @@ -149,3 +152,8 @@ data class PostArticleRequest(
data class UpdateStatusRequest(
val status: Int,
)

data class ArticleResponse(
val article: Article,
val chattingUsers: List<User>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -249,4 +254,12 @@ class ArticleService(
fun getArticleEntityById(articleId: Long): ArticleEntity {
return articleRepository.findByIdOrNull(articleId) ?: throw ArticleNotFoundException()
}

@Transactional
fun getChattingUsersByArticle(article: Article): List<User> {
val chatRoomEntities: List<ChatRoomEntity> = chatRoomRepository.findAllByArticleId(article.id)
return chatRoomEntities
.map { chatRoomEntity -> ChatRoom.fromEntity(chatRoomEntity, "") }
.map { chatRoom -> chatRoom.buyer }
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,9 +33,9 @@ class ChatRoomController(
@PathVariable chatRoomId: Long,
@AuthUser user: User,
@RequestParam("createdAt") createdAt: Instant,
): ResponseEntity<List<ChatMessage>> {
val chatRoom = chatRoomService.getChatRoom(chatRoomId, user, createdAt)
return ResponseEntity.ok(chatRoom)
): ResponseEntity<ChatRoomResponse> {
val chatRoomResponse = chatRoomService.getChatRoom(chatRoomId, user, createdAt)
return ResponseEntity.ok(chatRoomResponse)
}

@PostMapping("/chat/create")
Expand All @@ -51,3 +52,8 @@ data class CreateChatRoomRequest(
val sellerId: String,
val buyerId: String,
)

data class ChatRoomResponse(
val article: Article,
val messages: List<ChatMessage>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository
import java.time.Instant

interface ChatMessageRepository : JpaRepository<ChatMessageEntity, Long> {
fun findTop10ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
fun findTop30ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
chatRoomId: Long,
createdAt: Instant,
): List<ChatMessageEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface ChatRoomRepository : JpaRepository<ChatRoomEntity, Long> {
buyerId: String,
updatedAt: Instant,
): List<ChatRoomEntity>

fun findAllByArticleId(articleId: Long): List<ChatRoomEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,17 +63,21 @@ class ChatRoomService(
chatRoomId: Long,
user: User,
createdAt: Instant,
): List<ChatMessage> {
): ChatRoomResponse {
val chatRoomEntity = chatRoomRepository.findById(chatRoomId).orElseThrow { ChatRoomNotFoundException() }
val chatRoom = ChatRoom.fromEntity(chatRoomEntity, "")
if (chatRoom.buyer != user && chatRoom.seller != user) throw ThisRoomIsNotYoursException()

val chatMessageEntities: List<ChatMessageEntity> =
chatMessageRepository.findTop10ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
chatMessageRepository.findTop30ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
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
Expand Down

0 comments on commit 2602d17

Please sign in to comment.