-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
487 additions
and
9 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
1 change: 1 addition & 0 deletions
1
src/main/kotlin/kr/weit/roadyfoody/global/cachekey/PublicCacheKey.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 +1,2 @@ | ||
const val USER_ENTITY_LOCK_KEY = "USER-ENTITY-LOCK" | ||
const val REVIEW_LIKE_LOCK_KEY = "REVIEW-LIKE-LOCK" |
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
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/kr/weit/roadyfoody/review/application/service/ReviewLikeCommandService.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,53 @@ | ||
package kr.weit.roadyfoody.review.application.service | ||
|
||
import REVIEW_LIKE_LOCK_KEY | ||
import jakarta.persistence.EntityManager | ||
import kr.weit.roadyfoody.common.exception.ErrorCode | ||
import kr.weit.roadyfoody.common.exception.RoadyFoodyBadRequestException | ||
import kr.weit.roadyfoody.global.annotation.DistributedLock | ||
import kr.weit.roadyfoody.review.domain.ReviewLike | ||
import kr.weit.roadyfoody.review.domain.ReviewLikeId | ||
import kr.weit.roadyfoody.review.repository.FoodSpotsReviewRepository | ||
import kr.weit.roadyfoody.review.repository.ReviewLikeRepository | ||
import kr.weit.roadyfoody.review.repository.getReviewByReviewId | ||
import kr.weit.roadyfoody.user.domain.User | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class ReviewLikeCommandService( | ||
private val reviewRepository: FoodSpotsReviewRepository, | ||
private val reviewLikeRepository: ReviewLikeRepository, | ||
private val entityManager: EntityManager, | ||
) { | ||
@Transactional | ||
@DistributedLock(lockName = REVIEW_LIKE_LOCK_KEY, identifier = "reviewId") | ||
fun likeReview( | ||
reviewId: Long, | ||
user: User, | ||
) { | ||
val review = reviewRepository.getReviewByReviewId(reviewId) | ||
if (reviewLikeRepository.existsById(ReviewLikeId(review, user))) { | ||
throw RoadyFoodyBadRequestException(ErrorCode.ALREADY_LIKED) | ||
} | ||
review.increaseLike() | ||
reviewLikeRepository.save(ReviewLike(review, entityManager.merge(user))) | ||
} | ||
|
||
@Transactional | ||
@DistributedLock(lockName = REVIEW_LIKE_LOCK_KEY, identifier = "reviewId") | ||
fun unlikeReview( | ||
reviewId: Long, | ||
user: User, | ||
) { | ||
val review = reviewRepository.getReviewByReviewId(reviewId) | ||
if (!reviewLikeRepository.existsById(ReviewLikeId(review, user))) { | ||
throw RoadyFoodyBadRequestException(ErrorCode.NOT_LIKED) | ||
} | ||
if (review.likeTotal <= 0) { | ||
throw RoadyFoodyBadRequestException(ErrorCode.NEGATIVE_NUMBER_OF_LIKED) | ||
} | ||
review.decreaseLike() | ||
reviewLikeRepository.deleteById(ReviewLikeId(review, user)) | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/kr/weit/roadyfoody/review/presentation/api/ReviewLikeController.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,43 @@ | ||
package kr.weit.roadyfoody.review.presentation.api | ||
|
||
import jakarta.validation.constraints.Positive | ||
import kr.weit.roadyfoody.auth.security.LoginUser | ||
import kr.weit.roadyfoody.review.application.service.ReviewLikeCommandService | ||
import kr.weit.roadyfoody.review.presentation.spec.ReviewLikeControllerSpec | ||
import kr.weit.roadyfoody.user.domain.User | ||
import org.springframework.http.HttpStatus.CREATED | ||
import org.springframework.http.HttpStatus.NO_CONTENT | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/reviews/{reviewId}/likes") | ||
class ReviewLikeController( | ||
private val reviewLikeCommandService: ReviewLikeCommandService, | ||
) : ReviewLikeControllerSpec { | ||
@PostMapping | ||
@ResponseStatus(CREATED) | ||
override fun likeReview( | ||
@LoginUser | ||
user: User, | ||
@Positive(message = "리뷰 ID는 양수여야 합니다.") | ||
@PathVariable("reviewId") reviewId: Long, | ||
) { | ||
reviewLikeCommandService.likeReview(reviewId, user) | ||
} | ||
|
||
@DeleteMapping | ||
@ResponseStatus(NO_CONTENT) | ||
override fun unlikeReview( | ||
@LoginUser | ||
user: User, | ||
@Positive(message = "리뷰 ID는 양수여야 합니다.") | ||
@PathVariable("reviewId") reviewId: Long, | ||
) { | ||
reviewLikeCommandService.unlikeReview(reviewId, user) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/kr/weit/roadyfoody/review/presentation/spec/ReviewLikeControllerSpec.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,64 @@ | ||
package kr.weit.roadyfoody.review.presentation.spec | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import jakarta.validation.constraints.Positive | ||
import kr.weit.roadyfoody.auth.security.LoginUser | ||
import kr.weit.roadyfoody.common.exception.ErrorCode | ||
import kr.weit.roadyfoody.global.swagger.ApiErrorCodeExamples | ||
import kr.weit.roadyfoody.global.swagger.v1.SwaggerTag | ||
import kr.weit.roadyfoody.user.domain.User | ||
import org.springframework.web.bind.annotation.PathVariable | ||
|
||
@Tag(name = SwaggerTag.LIKE) | ||
interface ReviewLikeControllerSpec { | ||
@Operation( | ||
description = "리뷰 좋아요 생성 API", | ||
responses = [ | ||
ApiResponse( | ||
responseCode = "201", | ||
description = "리뷰 좋아요 성공", | ||
), | ||
], | ||
) | ||
@ApiErrorCodeExamples( | ||
[ | ||
ErrorCode.REVIEW_ID_NON_POSITIVE, | ||
ErrorCode.NOT_FOUND_FOOD_SPOTS_REVIEW, | ||
ErrorCode.ALREADY_LIKED, | ||
], | ||
) | ||
fun likeReview( | ||
@LoginUser | ||
user: User, | ||
@Positive(message = "리뷰 ID는 양수여야 합니다.") | ||
@PathVariable("reviewId") | ||
reviewId: Long, | ||
) | ||
|
||
@Operation( | ||
description = "리뷰 좋아요 삭제 API", | ||
responses = [ | ||
ApiResponse( | ||
responseCode = "204", | ||
description = "리뷰 좋아요 삭제 성공", | ||
), | ||
], | ||
) | ||
@ApiErrorCodeExamples( | ||
[ | ||
ErrorCode.NOT_LIKED, | ||
ErrorCode.NEGATIVE_NUMBER_OF_LIKED, | ||
ErrorCode.REVIEW_ID_NON_POSITIVE, | ||
ErrorCode.NOT_FOUND_FOOD_SPOTS_REVIEW, | ||
], | ||
) | ||
fun unlikeReview( | ||
@LoginUser | ||
user: User, | ||
@Positive(message = "리뷰 ID는 양수여야 합니다.") | ||
@PathVariable("reviewId") | ||
reviewId: Long, | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/kr/weit/roadyfoody/review/repository/ReviewLikeRepository.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,9 @@ | ||
package kr.weit.roadyfoody.review.repository | ||
|
||
import kr.weit.roadyfoody.review.domain.ReviewLike | ||
import kr.weit.roadyfoody.review.domain.ReviewLikeId | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface ReviewLikeRepository : JpaRepository<ReviewLike, ReviewLikeId> |
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
Oops, something went wrong.