diff --git a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt index 8b28b4e..5bb10b9 100644 --- a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt +++ b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt @@ -1,11 +1,14 @@ package org.meogo.domain.review.presentation import lombok.RequiredArgsConstructor -import org.meogo.domain.review.presentation.dto.ReviewRequest +import org.meogo.domain.review.presentation.dto.request.ReviewRequest import org.meogo.domain.review.service.CreateReviewService +import org.meogo.domain.review.service.QueryAllBySchoolIdService +import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import javax.validation.Valid @@ -13,7 +16,8 @@ import javax.validation.Valid @RequiredArgsConstructor @RequestMapping("/review") class ReviewController( - private val createReviewService: CreateReviewService + private val createReviewService: CreateReviewService, + private val queryAllBySchoolIdService: QueryAllBySchoolIdService ) { @PostMapping fun create( @@ -21,4 +25,8 @@ class ReviewController( request: ReviewRequest ) = createReviewService.execute(request) + + @GetMapping("/query") + fun queryAllBySchoolId(@RequestParam(name = "school_id") schoolId: Int) = + queryAllBySchoolIdService.queryAllBySchoolId(schoolId) } diff --git a/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt b/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt index 60dae40..4abdd91 100644 --- a/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt +++ b/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt @@ -5,4 +5,6 @@ import org.springframework.data.repository.Repository interface ReviewRepository : Repository { fun save(review: Review): Review + + fun findAllBySchoolId(id: Int): List } diff --git a/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt b/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt new file mode 100644 index 0000000..95e050a --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt @@ -0,0 +1,32 @@ +package org.meogo.domain.review.service + +import org.meogo.domain.review.presentation.dto.response.ReviewResponse +import org.meogo.domain.review.repository.ReviewRepository +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 +) { + @Transactional(readOnly = true) + fun queryAllBySchoolId(schoolId: Int): List { + val reviews = reviewRepository.findAllBySchoolId(schoolId) + + return reviews.map { review -> + ReviewResponse( + id = review.id, + 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() +}