Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ„ :: (Meogo-13) modif review #15

Merged
merged 8 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/main/kotlin/org/meogo/domain/review/domain/Review.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ class Review(
@Id
val id: Long = 0,

@Column(nullable = false)
val date: LocalDateTime,

@Column(columnDefinition = "BINARY(16)")
@Column(name = "user_id", columnDefinition = "BINARY(16)")
val userId: UUID,

val userName: String,

@Column(name = "school_id", nullable = false)
val schoolId: Int,

val star: Long,

val content: String,

val picture: String?

)
@Column(nullable = false)
var star: Float,

@Column(nullable = false)
var content: String,

var picture: String?

) {
fun updateReview(
content: String,
star: Float,
picture: String?
): Review {
this.content = content
this.star = star
this.picture = picture
return this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.meogo.domain.review.exception

import org.meogo.global.error.exception.ErrorCode
import org.meogo.global.error.exception.MeogoException

object ReviewNotFoundException : MeogoException(
ErrorCode.REVIEW_NOT_FOUND
)
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.meogo.domain.review.exception

import org.meogo.global.error.exception.ErrorCode
import org.meogo.global.error.exception.MeogoException

object UserMisMatchException : MeogoException(
ErrorCode.USER_MISMATCH
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.meogo.domain.review.presentation

import lombok.RequiredArgsConstructor
import org.meogo.domain.review.presentation.dto.request.ModifyReviewRequest
import org.meogo.domain.review.presentation.dto.request.ReviewRequest
import org.meogo.domain.review.service.CreateReviewService
import org.meogo.domain.review.service.ModifyReviewService
import org.meogo.domain.review.service.QueryAllBySchoolIdService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
Expand All @@ -17,7 +20,8 @@ import javax.validation.Valid
@RequestMapping("/review")
class ReviewController(
private val createReviewService: CreateReviewService,
private val queryAllBySchoolIdService: QueryAllBySchoolIdService
private val queryAllBySchoolIdService: QueryAllBySchoolIdService,
private val modifyReviewService: ModifyReviewService
) {
@PostMapping
fun create(
Expand All @@ -29,4 +33,8 @@ class ReviewController(
@GetMapping("/query")
fun queryAllBySchoolId(@RequestParam(name = "school_id") schoolId: Int) =
queryAllBySchoolIdService.queryAllBySchoolId(schoolId)

@PatchMapping("/modify")
fun modify(@RequestParam(name = "review_id") reviewId: Long, @RequestBody request: ModifyReviewRequest) =
modifyReviewService.modifyReview(reviewId, request)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.meogo.domain.review.presentation.dto.request

import javax.validation.constraints.Size

data class ModifyReviewRequest(
@field:Size(min = 1, max = 300)
val content: String,
val star: Float,
val image: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ data class ReviewRequest(
@field:Size(min = 1, max = 300)
val content: String,
val schoolId: Int,
val star: Long,
val star: Float,
val image: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ data class ReviewResponse(
val content: String,
val date: String,
val userName: String,
val star: Long,
val star: Float,
val picture: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ import org.springframework.data.repository.Repository
interface ReviewRepository : Repository<Review, Long> {
fun save(review: Review): Review

fun findAllBySchoolId(id: Int): List<Review>
fun findAllBySchoolId(id: Int): List<Review>?

fun findById(id: Long): Review?
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class CreateReviewService(
Review(
date = LocalDateTime.now(),
userId = user.id!!,
userName = user.name,
schoolId = request.schoolId,
star = request.star,
content = request.content,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.meogo.domain.review.service

import org.meogo.domain.review.exception.ReviewNotFoundException
import org.meogo.domain.review.exception.UserMisMatchException
import org.meogo.domain.review.presentation.dto.request.ModifyReviewRequest
import org.meogo.domain.review.repository.ReviewRepository
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.facade.UserFacade
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class ModifyReviewService(
private val reviewRepository: ReviewRepository,
private val userFacade: UserFacade
) {

@Transactional
fun modifyReview(reviewId: Long, request: ModifyReviewRequest) {
val user = userFacade.currentUser() ?: throw UserNotFoundException
val review = reviewRepository.findById(reviewId) ?: throw ReviewNotFoundException

if (user.id != review.userId) throw UserMisMatchException

reviewRepository.save(
review.updateReview(request.content, request.star, review.picture)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@ package org.meogo.domain.review.service

import org.meogo.domain.review.presentation.dto.response.ReviewResponse
import org.meogo.domain.review.repository.ReviewRepository
import org.meogo.domain.user.facade.UserFacade
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Service
class QueryAllBySchoolIdService(
private val reviewRepository: ReviewRepository
private val reviewRepository: ReviewRepository,
private val userFacade: UserFacade
) {

@Transactional(readOnly = true)
fun queryAllBySchoolId(schoolId: Int): List<ReviewResponse> {
val reviews = reviewRepository.findAllBySchoolId(schoolId)
val reviews = reviewRepository.findAllBySchoolId(schoolId) ?: return emptyList()

return reviews.map { review ->
val user = userFacade.getUserById(review.userId)
ReviewResponse(
id = review.id,
userName = user.name,
content = review.content,
date = format(review.date),
userName = review.userName,
star = review.star,
picture = review.picture ?: ""
)
}.sortedBy { it.id }
}

private fun format(date: LocalDateTime) =
date.format(DateTimeFormatter.ofPattern("MM.dd HH:mm")).toString()
date.format(DateTimeFormatter.ofPattern("MM.dd HH:mm"))
}
6 changes: 5 additions & 1 deletion src/main/kotlin/org/meogo/domain/user/facade/UserFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.repository.UserRepository
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Component
import java.util.UUID

@Component
class UserFacade(
Expand All @@ -16,6 +17,9 @@ class UserFacade(
return accountId?.let { getUserByAccountId(it) }
}

fun getUserByAccountId(accountId: String) =
fun getUserByAccountId(accountId: String): User =
userRepository.findByAccountId(accountId) ?: throw UserNotFoundException

fun getUserById(id: UUID): User =
userRepository.findById(id).orElseThrow { UserNotFoundException }
}
2 changes: 2 additions & 0 deletions src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ enum class ErrorCode(
INVALID_TOKEN(401, "Invalid Token"),
EXPIRED_TOKEN(401, "Expired Token"),
PASSWORD_MISMATCH(401, "Password Mismatch"),
USER_MISMATCH(401, "User Mismatch"),

USER_NOT_FOUND(404, "User not found"),
REVIEW_NOT_FOUND(404, "Review not found"),

// Internal Server Error
INTERNAL_SERVER_ERROR(500, "Internal Server Error")
Expand Down
Loading