Skip to content

Commit

Permalink
Merge pull request #73 from wafflestudio/feat/mypage
Browse files Browse the repository at this point in the history
리뷰/채팅방 수정
  • Loading branch information
ws11222 authored Jan 26, 2025
2 parents bfe0bfe + 7e6ac60 commit 40fbffe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -54,6 +53,6 @@ data class CreateChatRoomRequest(
)

data class ChatRoomResponse(
val article: Article,
val chatRoom: ChatRoom,
val messages: List<ChatMessage>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ChatRoomService(
)
val messages = chatMessageEntities.map { chatMessageEntity -> ChatMessage.fromEntity(chatMessageEntity) }
return ChatRoomResponse(
article = chatRoom.article,
chatRoom = chatRoom,
messages = messages,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ class ProfileService(
nickname: String,
reviewId: Long,
): List<Review> {
return reviewService.getPreviousReviews(nickname, reviewId)
val userEntity = userService.getUserEntityByNickname(nickname)
val profileEntity = profileRepository.findByUserId(userEntity.id!!) ?: throw ProfileNotFoundException()

return profileEntity.reviews
.filter { it.id!! < reviewId }
.sortedByDescending { it.createdAt }
.take(10)
.map { Review.fromEntity(it) }
}

fun getItemCount(id: String): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.toyProject7.karrot.review.controller

import com.toyProject7.karrot.review.service.ReviewService
import com.toyProject7.karrot.user.AuthUser
import com.toyProject7.karrot.user.controller.User
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
Expand All @@ -13,20 +10,21 @@ import org.springframework.web.bind.annotation.RestController
class ReviewController(
private val reviewService: ReviewService,
) {
@PostMapping("/api/{sellerNickname}/review")
@PostMapping("/api/review/post")
fun createReview(
@RequestBody request: ReviewCreateRequest,
@PathVariable sellerNickname: String,
@AuthUser user: User,
): ResponseEntity<ReviewCreateResponse> {
val review = reviewService.createReview(sellerNickname, user.nickname, request.content, request.location)
val review = reviewService.createReview(request)
return ResponseEntity.status(201).body(review)
}
}

data class ReviewCreateRequest(
val content: String,
val location: String,
val isWritedByBuyer: Boolean,
val sellerId: String,
val buyerId: String,
)

typealias ReviewCreateResponse = Review
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.toyProject7.karrot.review.service
import com.toyProject7.karrot.profile.service.ProfileService
import com.toyProject7.karrot.review.ReviewContentLengthOutOfRangeException
import com.toyProject7.karrot.review.controller.Review
import com.toyProject7.karrot.review.controller.ReviewCreateRequest
import com.toyProject7.karrot.review.persistence.ReviewEntity
import com.toyProject7.karrot.review.persistence.ReviewRepository
import com.toyProject7.karrot.user.controller.User
import com.toyProject7.karrot.user.service.UserService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -18,51 +18,36 @@ class ReviewService(
private val profileService: ProfileService,
) {
@Transactional
fun createReview(
sellerNickname: String,
buyerNickname: String,
content: String,
location: String,
): Review {
validateContent(content)
fun createReview(request: ReviewCreateRequest): Review {
validateContent(request.content)

val sellerEntity = userService.getUserEntityByNickname(sellerNickname)
val seller = User.fromEntity(sellerEntity)
val sellerProfileEntity = profileService.getProfileEntityByUserId(seller.id)
val sellerEntity = userService.getUserEntityById(request.sellerId)
val sellerProfileEntity = profileService.getProfileEntityByUserId(request.sellerId)

val buyerEntity = userService.getUserEntityByNickname(buyerNickname)
val buyer = User.fromEntity(buyerEntity)
val buyerProfileEntity = profileService.getProfileEntityByUserId(buyer.id)
val buyerEntity = userService.getUserEntityById(request.buyerId)
val buyerProfileEntity = profileService.getProfileEntityByUserId(request.buyerId)

val reviewEntity =
ReviewEntity(
seller = sellerEntity,
buyer = buyerEntity,
content = content,
location = location,
content = request.content,
location = request.location,
createdAt = Instant.now(),
updatedAt = Instant.now(),
).let {
reviewRepository.save(it)
}

sellerProfileEntity.reviews += reviewEntity
buyerProfileEntity.reviews += reviewEntity
if (request.isWritedByBuyer) {
sellerProfileEntity.reviews += reviewEntity
} else {
buyerProfileEntity.reviews += reviewEntity
}

return Review.fromEntity(reviewEntity)
}

@Transactional
fun getPreviousReviews(
nickname: String,
reviewId: Long,
): List<Review> {
return reviewRepository.findTop10BySellerNicknameOrBuyerNicknameAndIdBeforeOrderByCreatedAtDesc(nickname, nickname, reviewId).map {
reviewEntity ->
Review.fromEntity(reviewEntity)
}
}

private fun validateContent(content: String) {
if (content.isBlank()) {
throw ReviewContentLengthOutOfRangeException()
Expand Down

0 comments on commit 40fbffe

Please sign in to comment.