diff --git a/src/main/kotlin/gdsc/plantory/plant/domain/CompanionPlantRepository.kt b/src/main/kotlin/gdsc/plantory/plant/domain/CompanionPlantRepository.kt index b84790a..b73f697 100644 --- a/src/main/kotlin/gdsc/plantory/plant/domain/CompanionPlantRepository.kt +++ b/src/main/kotlin/gdsc/plantory/plant/domain/CompanionPlantRepository.kt @@ -17,6 +17,7 @@ interface CompanionPlantRepository : JpaRepository { fun findAllByMemberId(memberId: Long): List fun findByIdAndMemberId(id: Long, memberId: Long): CompanionPlant? + fun removeByIdAndMemberId(id: Long, memberId: Long) @Query( """ diff --git a/src/main/kotlin/gdsc/plantory/plant/presentation/PlantCommandApi.kt b/src/main/kotlin/gdsc/plantory/plant/presentation/PlantCommandApi.kt index 314d572..faee114 100644 --- a/src/main/kotlin/gdsc/plantory/plant/presentation/PlantCommandApi.kt +++ b/src/main/kotlin/gdsc/plantory/plant/presentation/PlantCommandApi.kt @@ -4,11 +4,13 @@ import BadRequestException import gdsc.plantory.common.support.AccessDeviceToken import gdsc.plantory.plant.domain.HistoryType import gdsc.plantory.plant.presentation.dto.CompanionPlantCreateRequest +import gdsc.plantory.plant.presentation.dto.CompanionPlantDeleteRequest import gdsc.plantory.plant.presentation.dto.PlantRecordCreateRequest import gdsc.plantory.plant.presentation.dto.PlantHistoryRequest import gdsc.plantory.plant.service.PlantService import org.springframework.http.MediaType import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping @@ -32,6 +34,15 @@ class PlantCommandApi( return ResponseEntity.ok().build() } + @DeleteMapping(consumes = [MediaType.APPLICATION_JSON_VALUE]) + fun remove( + @RequestBody request: CompanionPlantDeleteRequest, + @AccessDeviceToken deviceToken: String, + ): ResponseEntity { + plantService.remove(request, deviceToken) + return ResponseEntity.noContent().build() + } + @PostMapping("/histories", consumes = [MediaType.APPLICATION_JSON_VALUE]) fun createPlantHistory( @RequestBody request: PlantHistoryRequest, diff --git a/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantDeleteRequest.kt b/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantDeleteRequest.kt new file mode 100644 index 0000000..4f37761 --- /dev/null +++ b/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantDeleteRequest.kt @@ -0,0 +1,5 @@ +package gdsc.plantory.plant.presentation.dto + +data class CompanionPlantDeleteRequest( + val companionPlantId: Long +) diff --git a/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt b/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt index 2f2b9ae..dc77c4e 100644 --- a/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt +++ b/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt @@ -9,6 +9,7 @@ import gdsc.plantory.plant.domain.findByIdAndMemberIdOrThrow import gdsc.plantory.plant.domain.findRecordByDateOrThrow import gdsc.plantory.plant.presentation.dto.PlantHistoriesLookupRequest import gdsc.plantory.plant.presentation.dto.CompanionPlantCreateRequest +import gdsc.plantory.plant.presentation.dto.CompanionPlantDeleteRequest import gdsc.plantory.plant.presentation.dto.CompanionPlantsLookupResponse import gdsc.plantory.plant.presentation.dto.PlantRecordLookupRequest import gdsc.plantory.plant.presentation.dto.PlantRecordDto @@ -39,6 +40,11 @@ class PlantService( companionPlantRepository.save(companionPlant) } + fun remove(request: CompanionPlantDeleteRequest, deviceToken: String) { + val findMember = memberRepository.findByDeviceTokenOrThrow(deviceToken) + companionPlantRepository.removeByIdAndMemberId(request.companionPlantId, findMember.getId) + } + @Transactional(readOnly = true) fun lookupAllCompanionPlantsOfMember(deviceToken: String): CompanionPlantsLookupResponse { val findMember = memberRepository.findByDeviceTokenOrThrow(deviceToken) diff --git a/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantAcceptanceTest.kt b/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantAcceptanceTest.kt index 557ff7a..e47071a 100644 --- a/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantAcceptanceTest.kt +++ b/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantAcceptanceTest.kt @@ -6,6 +6,7 @@ import gdsc.plantory.acceptance.CompanionPlantStep.Companion.식물_조회_요 import gdsc.plantory.acceptance.CompanionPlantStep.Companion.데일리_기록_등록_요청 import gdsc.plantory.acceptance.CompanionPlantStep.Companion.데일리_기록_조회_요청 import gdsc.plantory.acceptance.CompanionPlantStep.Companion.데일리_기록_조회_응답_확인 +import gdsc.plantory.acceptance.CompanionPlantStep.Companion.반려_식물_삭제_요청 import gdsc.plantory.acceptance.CompanionPlantStep.Companion.식물_히스토리_생성_요청 import gdsc.plantory.acceptance.CompanionPlantStep.Companion.식물_조회_응답_확인 import gdsc.plantory.acceptance.CompanionPlantStep.Companion.히스토리_조회_요청 @@ -16,6 +17,7 @@ import gdsc.plantory.fixture.테스터_디바이스_토큰 import gdsc.plantory.fixture.테스트_식물정보_ID import gdsc.plantory.fixture.CompanionPlantFixture.generateCompanionPlantCreateRequest import gdsc.plantory.fixture.CompanionPlantFixture.generatePlantRecordCreateRequest +import gdsc.plantory.plant.presentation.dto.CompanionPlantDeleteRequest import gdsc.plantory.plant.presentation.dto.PlantHistoryRequest import gdsc.plantory.plant.presentation.dto.PlantHistoriesLookupRequest import gdsc.plantory.plant.presentation.dto.PlantRecordLookupRequest @@ -41,6 +43,18 @@ class CompanionPlantAcceptanceTest : AcceptanceTest() { 응답_확인(식물_등록_요청_응답, HttpStatus.OK) } + @Test + fun `반려식물 삭제`() { + // given + val 반려_식물_정보 = CompanionPlantDeleteRequest(기록있는_테스트식물_ID) + + // when + val 식물_삭제_요청_응답 = 반려_식물_삭제_요청(반려_식물_정보, 테스터_디바이스_토큰) + + // then + 응답_확인(식물_삭제_요청_응답, HttpStatus.NO_CONTENT) + } + @Test fun `반려식물 물주기 히스토리 등록`() { // given diff --git a/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantStep.kt b/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantStep.kt index 56df29f..ef0908a 100644 --- a/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantStep.kt +++ b/src/test/kotlin/gdsc/plantory/acceptance/CompanionPlantStep.kt @@ -1,6 +1,7 @@ package gdsc.plantory.acceptance import gdsc.plantory.plant.presentation.dto.CompanionPlantCreateRequest +import gdsc.plantory.plant.presentation.dto.CompanionPlantDeleteRequest import gdsc.plantory.plant.presentation.dto.PlantRecordCreateRequest import gdsc.plantory.plant.presentation.dto.PlantHistoryRequest import gdsc.plantory.plant.presentation.dto.PlantRecordLookupRequest @@ -39,6 +40,24 @@ class CompanionPlantStep { .extract() } + fun 반려_식물_삭제_요청( + request: CompanionPlantDeleteRequest, + deviceToken: String, + ): ExtractableResponse { + return RestAssured + .given() + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Device-Token", deviceToken) + .log().all() + .body(request) + .`when`() + .delete("/api/v1/plants") + .then() + .log().all() + .statusCode(HttpStatus.NO_CONTENT.value()) + .extract() + } + fun 식물_히스토리_생성_요청( request: PlantHistoryRequest, deviceToken: String,