Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

[Feat] 식물 정보 조회 API #44

Merged
merged 6 commits into from
Jan 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@Tag(name = "Plant Query", description = "반려식물 정보 조회")
@Tag(name = "Plant Command", description = "반려식물 정보 수정")
@RestController
@RequestMapping("/api/v1/plants")
class PlantCommandApi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController
import java.time.LocalDate
import java.time.YearMonth

@Tag(name = "Plant Command", description = "반려식물 정보 수정")
@Tag(name = "Plant Query", description = "반려식물 정보 조회")
@RestController
@RequestMapping("/api/v1/plants")
class PlantQueryApi(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package gdsc.plantory.plantInformation.domain

import NotFoundException
import gdsc.plantory.plantInformation.presentation.dto.PlantInformationDto
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import kotlin.jvm.optionals.getOrNull

fun PlantInformationRepository.findByIdOrThrow(id: Long): PlantInformation {
return findById(id).getOrNull() ?: throw NotFoundException("식물 정보가 없어요")
}

interface PlantInformationRepository : JpaRepository<PlantInformation, Long> {
@Query(
"""
SELECT new gdsc.plantory.plantInformation.presentation.dto.PlantInformationDto(
pi.id,
pi.species.name,
pi.species.familyName
)
FROM PlantInformation pi
"""
)
fun findAllSpeciesInformations(): List<PlantInformationDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gdsc.plantory.plantInformation.presentation

import gdsc.plantory.plantInformation.presentation.dto.PlantInformationsLookupResponse
import gdsc.plantory.plantInformation.service.PlantInformationService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Tag(name = "Plant Information Query", description = "식물 정보 조회")
@RestController
@RequestMapping("/api/v1/plantInformations")
class PlantInformationQueryApi(
private val plantInformationService: PlantInformationService
) {
@Operation(summary = "식물 정보 조회", description = "등록된 모든 식물 정보를 조회합니다.")
@ApiResponse(responseCode = "200", description = "조회 성공")
@GetMapping
fun lookupAllPlantInformations(): ResponseEntity<PlantInformationsLookupResponse> {
val plantInformations = plantInformationService.lookupAllPlantInformations()
return ResponseEntity.ok().body(plantInformations)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gdsc.plantory.plantInformation.presentation.dto

data class PlantInformationDto(
val id: Long,
val species: String,
val familyName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gdsc.plantory.plantInformation.presentation.dto

data class PlantInformationsLookupResponse(
val plantInformations: List<PlantInformationDto>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gdsc.plantory.plantInformation.service

import gdsc.plantory.plantInformation.domain.PlantInformationRepository
import gdsc.plantory.plantInformation.presentation.dto.PlantInformationsLookupResponse
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
class PlantInformationService(
private val plantInformationRepository: PlantInformationRepository,
) {
@Transactional(readOnly = true)
fun lookupAllPlantInformations(): PlantInformationsLookupResponse {
val findPlantInformations = plantInformationRepository.findAllSpeciesInformations()

return PlantInformationsLookupResponse(findPlantInformations)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gdsc.plantory.acceptance

import gdsc.plantory.acceptance.PlantInformationStep.Companion.식물_정보_조회_요청
import gdsc.plantory.acceptance.PlantInformationStep.Companion.식물_정보_조회_응답_확인
import gdsc.plantory.util.AcceptanceTest
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test

@DisplayName("인수 : PlantInformation")
class PlantInformationAcceptanceTest : AcceptanceTest() {

@Test
fun `식물 정보 조회`() {
// when
val 식물_정보_조회_요청_응답 = 식물_정보_조회_요청()

// then
식물_정보_조회_응답_확인(식물_정보_조회_요청_응답)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gdsc.plantory.acceptance

import io.restassured.RestAssured
import io.restassured.response.ExtractableResponse
import io.restassured.response.Response
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.assertAll
import org.springframework.http.HttpStatus

class PlantInformationStep {
companion object {
fun 식물_정보_조회_요청(): ExtractableResponse<Response> {
return RestAssured
.given()
.log().all()
.`when`()
.get("/api/v1/plantInformations")
.then()
.log().all()
.extract()
}

fun 식물_정보_조회_응답_확인(response: ExtractableResponse<Response>) {
assertAll(
{ assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()) },
{ assertThat(response.jsonPath().getString("plantInformations[].id")).isNotBlank() },
{ assertThat(response.jsonPath().getString("plantInformations[].species")).isNotBlank() },
{ assertThat(response.jsonPath().getString("plantInformations[].familyName")).isNotBlank() },
)
}
}
}
Loading