-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KAN-61-KAN-66-KAN-68-KAN-69-KAN-70-KAN-71] Main Merge PR (#47)
- Loading branch information
Showing
21 changed files
with
881 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/kotlin/com/restaurant/be/review/domain/entity/ReviewLikes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
package com.restaurant.be.review.domain.entity | ||
|
||
import javax.persistence.Column | ||
|
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/restaurant/be/review/domain/service/DeleteReviewService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.restaurant.be.review.domain.service | ||
|
||
import com.restaurant.be.common.exception.NotFoundReviewException | ||
import com.restaurant.be.common.exception.NotFoundUserEmailException | ||
import com.restaurant.be.common.exception.UnAuthorizedDeleteException | ||
import com.restaurant.be.review.repository.ReviewRepository | ||
import com.restaurant.be.user.repository.UserRepository | ||
import org.springframework.stereotype.Service | ||
import javax.transaction.Transactional | ||
import kotlin.jvm.optionals.getOrNull | ||
|
||
@Service | ||
class DeleteReviewService( | ||
private val reviewRepository: ReviewRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
@Transactional | ||
fun deleteReview(reviewId: Long, email: String) { | ||
val user = userRepository.findByEmail(email) ?: throw NotFoundUserEmailException() | ||
|
||
var review = reviewRepository.findById(reviewId).getOrNull() ?: throw NotFoundReviewException() | ||
|
||
if (user.id != review.user.id) throw UnAuthorizedDeleteException() | ||
|
||
reviewRepository.deleteById(reviewId) | ||
} | ||
} |
69 changes: 53 additions & 16 deletions
69
src/main/kotlin/com/restaurant/be/review/domain/service/GetReviewService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,75 @@ | ||
package com.restaurant.be.review.domain.service | ||
|
||
import com.restaurant.be.common.exception.NotFoundReviewException | ||
import com.restaurant.be.common.exception.NotFoundUserEmailException | ||
import com.restaurant.be.review.presentation.dto.GetMyReviewsResponse | ||
import com.restaurant.be.review.presentation.dto.GetReviewResponse | ||
import com.restaurant.be.review.repository.ReviewLikesRepository | ||
import com.restaurant.be.review.presentation.dto.GetReviewsResponse | ||
import com.restaurant.be.review.presentation.dto.ReviewWithLikesDto | ||
import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto | ||
import com.restaurant.be.review.repository.ReviewRepository | ||
import com.restaurant.be.user.repository.UserRepository | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class GetReviewService( | ||
private val userRepository: UserRepository, | ||
private val reviewRepository: ReviewRepository, | ||
private val reviewLikesRepository: ReviewLikesRepository | ||
private val reviewRepository: ReviewRepository | ||
|
||
) { | ||
fun getReviewListOf(page: Int, size: Int, email: String): GetReviewResponse { | ||
val pageable = PageRequest.of(page, size) | ||
val reviews = reviewRepository.findAll(pageable).content | ||
@Transactional(readOnly = true) | ||
fun getReviews(pageable: Pageable, email: String): GetReviewsResponse { | ||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
val reviewsWithLikes = reviewRepository.findReviews(user, pageable) | ||
|
||
val responseDtos = convertResponeDto(reviewsWithLikes) | ||
|
||
return GetReviewsResponse(responseDtos) | ||
} | ||
|
||
@Transactional | ||
fun getReview(reviewId: Long, email: String): GetReviewResponse { | ||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
return GetReviewResponse( | ||
reviews.map { | ||
it | ||
.toResponseDTO(doesUserLike = isReviewLikedByUser(user.id, it.id)) | ||
} | ||
val reviewWithLikes = reviewRepository.findReview(user, reviewId) | ||
?: throw NotFoundReviewException() | ||
|
||
if (reviewWithLikes.review.user.id != user.id) { | ||
reviewWithLikes.review.incrementViewCount() | ||
} | ||
|
||
val responseDto = ReviewResponseDto.toDto( | ||
reviewWithLikes.review, | ||
reviewWithLikes.isLikedByUser | ||
) | ||
|
||
return GetReviewResponse(responseDto) | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
fun getMyReviews(pageable: Pageable, email: String): GetMyReviewsResponse { | ||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
val reviewsWithLikes = reviewRepository.findMyReviews(user, pageable) | ||
|
||
val reviewResponses = convertResponeDto(reviewsWithLikes) | ||
|
||
return GetMyReviewsResponse(reviewResponses) | ||
} | ||
|
||
fun isReviewLikedByUser(userId: Long?, reviewId: Long?): Boolean { | ||
if (userId != 0L) { | ||
return reviewLikesRepository.existsByReviewIdAndUserId(userId, reviewId) | ||
private fun convertResponeDto(reviewsWithLikes: List<ReviewWithLikesDto>): List<ReviewResponseDto> { | ||
val reviewResponses = reviewsWithLikes.map { | ||
ReviewResponseDto.toDto( | ||
it.review, | ||
it.isLikedByUser | ||
) | ||
} | ||
return false | ||
return reviewResponses | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/com/restaurant/be/review/domain/service/LikeReviewService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.restaurant.be.review.domain.service | ||
|
||
import com.restaurant.be.common.exception.DuplicateLikeException | ||
import com.restaurant.be.common.exception.NotFoundLikeException | ||
import com.restaurant.be.common.exception.NotFoundReviewException | ||
import com.restaurant.be.common.exception.NotFoundUserEmailException | ||
import com.restaurant.be.common.exception.NotFoundUserIdException | ||
import com.restaurant.be.review.domain.entity.QReview.review | ||
import com.restaurant.be.review.presentation.dto.LikeReviewRequest | ||
import com.restaurant.be.review.presentation.dto.LikeReviewResponse | ||
import com.restaurant.be.review.presentation.dto.ReviewWithLikesDto | ||
import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto | ||
import com.restaurant.be.review.repository.ReviewLikesRepository | ||
import com.restaurant.be.review.repository.ReviewRepository | ||
import com.restaurant.be.user.domain.entity.User | ||
import com.restaurant.be.user.repository.UserRepository | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class LikeReviewService( | ||
val userRepository: UserRepository, | ||
val reviewLikesRepository: ReviewLikesRepository, | ||
val reviewRepository: ReviewRepository | ||
) { | ||
@Transactional | ||
fun likeReview(reviewId: Long, request: LikeReviewRequest, email: String): LikeReviewResponse { | ||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
val userId = user.id ?: throw NotFoundUserIdException() | ||
|
||
likeReviewWhetherAlreadyLikeOrNot(request, reviewId, user, userId) | ||
|
||
val reviewWithLikes: ReviewWithLikesDto? = reviewRepository.findReview(user, reviewId) | ||
?: throw NotFoundReviewException() | ||
|
||
val responseDto = ReviewResponseDto.toDto( | ||
reviewWithLikes!!.review, | ||
reviewWithLikes.isLikedByUser | ||
) | ||
|
||
return LikeReviewResponse(responseDto) | ||
} | ||
|
||
private fun likeReviewWhetherAlreadyLikeOrNot( | ||
request: LikeReviewRequest, | ||
reviewId: Long, | ||
user: User, | ||
userId: Long | ||
) { | ||
if (request.isLike) { | ||
if (isAlreadyLike(reviewId, user)) { | ||
throw DuplicateLikeException() | ||
} | ||
reviewLikesRepository.save(request.toEntity(userId, reviewId)) | ||
val review = reviewRepository.findById(reviewId) | ||
review.get().incrementLikeCount() | ||
} else { | ||
if (!isAlreadyLike(reviewId, user)) { | ||
throw NotFoundLikeException() | ||
} | ||
reviewLikesRepository.deleteByReviewIdAndUserId(reviewId, userId) | ||
val review = reviewRepository.findById(reviewId) | ||
review.get().decrementLikeCount() | ||
} | ||
} | ||
|
||
private fun isAlreadyLike(reviewId: Long, user: User) = | ||
reviewLikesRepository.existsByReviewIdAndUserId(reviewId, user.id) | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/com/restaurant/be/review/domain/service/UpdateReviewService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.restaurant.be.review.domain.service | ||
|
||
import com.restaurant.be.common.exception.NotFoundReviewException | ||
import com.restaurant.be.common.exception.NotFoundUserEmailException | ||
import com.restaurant.be.common.exception.UnAuthorizedUpdateException | ||
import com.restaurant.be.review.domain.entity.QReview.review | ||
import com.restaurant.be.review.presentation.dto.UpdateReviewRequest | ||
import com.restaurant.be.review.presentation.dto.UpdateReviewResponse | ||
import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto | ||
import com.restaurant.be.review.repository.ReviewRepository | ||
import com.restaurant.be.user.repository.UserRepository | ||
import org.springframework.stereotype.Service | ||
import javax.transaction.Transactional | ||
import kotlin.jvm.optionals.getOrNull | ||
|
||
@Service | ||
class UpdateReviewService( | ||
private val reviewRepository: ReviewRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
@Transactional | ||
fun updateReview(restaurantId: Long, reviewId: Long, reviewRequest: UpdateReviewRequest, email: String): UpdateReviewResponse { | ||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
val review = reviewRepository.findById(reviewId) | ||
.getOrNull() | ||
?: throw NotFoundReviewException() | ||
|
||
if (user.id != review.user.id) throw UnAuthorizedUpdateException() | ||
|
||
review.updateReview(reviewRequest) | ||
|
||
val reviewWithLikes = reviewRepository.findReview(user, reviewId) | ||
?: throw NotFoundReviewException() | ||
|
||
val responseDto = ReviewResponseDto.toDto( | ||
reviewWithLikes.review, | ||
reviewWithLikes.isLikedByUser | ||
) | ||
|
||
return UpdateReviewResponse(responseDto) | ||
} | ||
} |
Oops, something went wrong.