Skip to content

Commit

Permalink
add :: queryAllBySchoolId api
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Sep 4, 2024
1 parent 8d4e850 commit a3c246d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
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

@RestController
@RequiredArgsConstructor
@RequestMapping("/review")
class ReviewController(
private val createReviewService: CreateReviewService
private val createReviewService: CreateReviewService,
private val queryAllBySchoolIdService: QueryAllBySchoolIdService
) {
@PostMapping
fun create(
@Valid @RequestBody
request: ReviewRequest
) =
createReviewService.execute(request)

@GetMapping("/query")
fun queryAllBySchoolId(@RequestParam(name = "school_id") schoolId: Int) =
queryAllBySchoolIdService.queryAllBySchoolId(schoolId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import org.springframework.data.repository.Repository

interface ReviewRepository : Repository<Review, Long> {
fun save(review: Review): Review

fun findAllBySchoolId(id: Int): List<Review>
}
Original file line number Diff line number Diff line change
@@ -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<ReviewResponse> {
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()
}

0 comments on commit a3c246d

Please sign in to comment.