-
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-39] Review 작성 API & 테스트코드 초안 작성 (#33)
* Restaurant Review Entity 추가 * feat: Review Creat API fix: final BaseEntity to non-final * test: 리뷰 작성 간단한 테스트 코드 추가 * chore: lint apply * chore: lint apply again * test: MethodArguementNotValidException Handling * chore: lint apply * refactor: 1. restaurantId nullable 수정 2. nullable=false 생략 3. 별점 Integer To Double 수정 4. 리뷰 작성 Response에 Entity가 아닌 DTO로 수정+swagger * chore: lint apply * fix: Entity Column nullable option * chore: Review, ReviewDto 자잘한 수정 * feat: ReviewLike Entity추가 및 로직 반영 * chore: lint apply * feat: review list 조회 기능 * chore: lint applies * fix: queryDSL 반영 * Revert "chore: lint applies" This reverts commit 6264f0b. * fix: queryDSL 쿼리 반영 * refactor: remove PrincipalUtils * refactor: naming again ReviewDto field * refactor: 자잘한 수정들 * refactor: queryDSL 계층 구조 분리 * [KAN-39] 리뷰 생성 API - 일부 수정 (#43) * [KAN-39] 리뷰 생성 API - 일부 수정 * [KAN-39] 리뷰 생성 API - test code * [KAN-39] 리뷰 생성 API - test code --------- Co-authored-by: Bob Sin <tkagmd1@naver.com>
- Loading branch information
1 parent
b360342
commit 770cec2
Showing
18 changed files
with
498 additions
and
28 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
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
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/com/restaurant/be/review/domain/entity/Review.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,62 @@ | ||
package com.restaurant.be.review.domain.entity | ||
|
||
import com.restaurant.be.common.entity.BaseEntity | ||
import com.restaurant.be.review.presentation.dto.common.ReviewResponseDto | ||
import com.restaurant.be.user.domain.entity.User | ||
import kotlinx.serialization.json.JsonNull.content | ||
import javax.persistence.CascadeType | ||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.FetchType | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.GenerationType | ||
import javax.persistence.Id | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.ManyToOne | ||
import javax.persistence.OneToMany | ||
import javax.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "restaurant_reviews") | ||
class Review( | ||
@Id | ||
@Column(name = "review_id") | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
val id: Long? = null, | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
val user: User, | ||
|
||
@Column(nullable = false) | ||
val restaurantId: Long, | ||
|
||
@Column(nullable = false) | ||
val content: String, | ||
|
||
@Column(nullable = false) | ||
val rating: Double, | ||
|
||
// 부모 (Review Entity)가 주인이되어 Image참조 가능. 반대는 불가능 | ||
@OneToMany(cascade = [CascadeType.ALL], orphanRemoval = true) | ||
@JoinColumn(name = "review_id") | ||
val images: MutableList<ReviewImage> = mutableListOf() | ||
|
||
) : BaseEntity() { | ||
fun addImage(reviewImage: ReviewImage) { | ||
images.add(reviewImage) | ||
} | ||
|
||
fun toResponseDTO(doesUserLike: Boolean): ReviewResponseDto { | ||
return ReviewResponseDto( | ||
userId = user.id ?: 0, | ||
username = user.nickname, | ||
profileImageUrl = user.profileImageUrl, | ||
restaurantId = restaurantId, | ||
rating = rating, | ||
content = content, | ||
imageUrls = images.map { it.imageUrl }, | ||
isLike = doesUserLike | ||
) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/restaurant/be/review/domain/entity/ReviewImage.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,19 @@ | ||
package com.restaurant.be.review.domain.entity | ||
|
||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.GenerationType | ||
import javax.persistence.Id | ||
import javax.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "review_images") | ||
class ReviewImage( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long? = null, | ||
|
||
@Column(nullable = false, length = 300) | ||
val imageUrl: String | ||
) |
22 changes: 22 additions & 0 deletions
22
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.restaurant.be.review.domain.entity | ||
|
||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.GenerationType | ||
import javax.persistence.Id | ||
import javax.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "review_likes") | ||
class ReviewLikes( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
val id: Long? = null, | ||
|
||
@Column(name = "user_id", nullable = false) | ||
val userId: Long, | ||
|
||
@Column(name = "review_id", nullable = false) | ||
val reviewId: Long | ||
) |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/restaurant/be/review/domain/service/CreateReviewService.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,50 @@ | ||
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.domain.entity.ReviewImage | ||
import com.restaurant.be.review.presentation.dto.CreateReviewResponse | ||
import com.restaurant.be.review.presentation.dto.common.ReviewRequestDto | ||
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 | ||
|
||
@Service | ||
class CreateReviewService( | ||
private val reviewRepository: ReviewRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
@Transactional | ||
fun createReview( | ||
restaurantId: Long, | ||
reviewRequest: ReviewRequestDto, | ||
email: String | ||
): CreateReviewResponse { | ||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
val review = reviewRequest.toEntity(user, restaurantId) | ||
|
||
reviewRequest.imageUrls.forEach { | ||
review.addImage( | ||
ReviewImage( | ||
imageUrl = it | ||
) | ||
) | ||
} | ||
|
||
reviewRepository.save(review) | ||
|
||
val reviewWithLikes = reviewRepository.findReview(user, review.id ?: 0) | ||
?: throw NotFoundReviewException() | ||
|
||
val responseDto = ReviewResponseDto.toDto( | ||
reviewWithLikes.review, | ||
reviewWithLikes.isLikedByUser | ||
) | ||
|
||
return CreateReviewResponse(responseDto) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.restaurant.be.review.domain.service | ||
|
||
import com.restaurant.be.common.exception.NotFoundUserEmailException | ||
import com.restaurant.be.review.presentation.dto.GetReviewResponse | ||
import com.restaurant.be.review.repository.ReviewLikesRepository | ||
import com.restaurant.be.review.repository.ReviewRepository | ||
import com.restaurant.be.user.repository.UserRepository | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GetReviewService( | ||
private val userRepository: UserRepository, | ||
private val reviewRepository: ReviewRepository, | ||
private val reviewLikesRepository: ReviewLikesRepository | ||
) { | ||
fun getReviewListOf(page: Int, size: Int, email: String): GetReviewResponse { | ||
val pageable = PageRequest.of(page, size) | ||
val reviews = reviewRepository.findAll(pageable).content | ||
|
||
val user = userRepository.findByEmail(email) | ||
?: throw NotFoundUserEmailException() | ||
|
||
return GetReviewResponse( | ||
reviews.map { | ||
it | ||
.toResponseDTO(doesUserLike = isReviewLikedByUser(user.id, it.id)) | ||
} | ||
) | ||
} | ||
|
||
fun isReviewLikedByUser(userId: Long?, reviewId: Long?): Boolean { | ||
if (userId != 0L) { | ||
return reviewLikesRepository.existsByReviewIdAndUserId(userId, reviewId) | ||
} | ||
return false | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/restaurant/be/review/presentation/dto/ReviewWithLikesDto.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,8 @@ | ||
package com.restaurant.be.review.presentation.dto | ||
|
||
import com.restaurant.be.review.domain.entity.Review | ||
|
||
data class ReviewWithLikesDto( | ||
val review: Review, | ||
val isLikedByUser: Boolean | ||
) |
Oops, something went wrong.