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

[Feat] RECORDING hsitory 직접 추가 방지 #42

Merged
merged 3 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
21 changes: 18 additions & 3 deletions src/main/kotlin/gdsc/plantory/plant/domain/CompanionPlant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ class CompanionPlant(
}

this.records.add(PlantRecord(imageUrl, comment, this))
this.saveRecordHistory(date)
}

fun saveHistory(historyType: HistoryType, date: LocalDate = LocalDate.now()) {
if (isNotCurrentDay(date)) {
throw IllegalArgumentException("물을 줄 날짜는 오늘 날짜여야 합니다.")
}
validateInput(historyType, date)

if (historyType == HistoryType.WATER_CHANGE) {
this.lastWaterDate = date
Expand All @@ -125,6 +124,22 @@ class CompanionPlant(
return ChronoUnit.DAYS.between(birthDate, currentDate).toInt() + 1
}

private fun validateInput(historyType: HistoryType, date: LocalDate) {
if (isRecordType(historyType)) {
throw IllegalArgumentException("데일리 기록은 히스토리 타입을 직접 추가할 수 없습니다.")
}

if (isNotCurrentDay(date)) {
throw IllegalArgumentException("물을 줄 날짜는 오늘 날짜여야 합니다.")
}
}

private fun isRecordType(historyType: HistoryType) = historyType == HistoryType.RECORDING

private fun saveRecordHistory(date: LocalDate) {
this.histories.add(PlantHistory(HistoryType.RECORDING, date, this))
}

private fun isNotCurrentDay(date: LocalDate) = !date.isEqual(LocalDate.now())

override fun equals(other: Any?): Boolean {
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class PlantService(
fun createPlantHistory(plantId: Long, deviceToken: String, historyType: HistoryType) {
val findMember = memberRepository.findByDeviceTokenOrThrow(deviceToken)
val findCompanionPlant = companionPlantRepository.findByIdAndMemberIdOrThrow(plantId, findMember.getId)

findCompanionPlant.saveHistory(historyType)
}

Expand Down Expand Up @@ -94,7 +93,6 @@ class PlantService(
// TODO : Cloud 환경으로 이전 후 제거, 로컬 사진 저장 테스트 용도
val baseUrl = "https://nongsaro.go.kr/"
findCompanionPlant.saveRecord(request.comment, baseUrl + imagePath)
findCompanionPlant.saveHistory(HistoryType.RECORDING)
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ class CompanionPlantTest {
.doesNotThrowAnyException()
}

@Test
fun `반려식물에게 레코드 타입의 히스토리를 직접 저장하려는 경우 예외 발생`() {
val waterCycle = 7L
val lastWaterDate = LocalDate.now()
val nextWaterDate = lastWaterDate.plusDays(waterCycle)
val companionPlant = CompanionPlant(
"https://nongsaro.go.kr/cms_contents/301/14687_MF_ATTACH_01.jpg",
"나의 아기 선인장", "shine", nextWaterDate, lastWaterDate, waterCycle.toInt()
)

assertThatThrownBy {
companionPlant.saveHistory(HistoryType.RECORDING, LocalDate.now())
}
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessageContaining("데일리 기록은 히스토리 타입을 직접 추가할 수 없습니다.")
}

@Test
fun `물 준 주기가 맞지 않으면 예외 발생`() {
val waterCycle = 7L
Expand Down
15 changes: 13 additions & 2 deletions src/test/kotlin/gdsc/plantory/plant/service/PlantServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gdsc.plantory.plant.service

import gdsc.plantory.fixture.기록없는_테스트식물_ID
import gdsc.plantory.fixture.테스터_디바이스_토큰
import gdsc.plantory.plant.domain.CompanionPlant
import gdsc.plantory.plant.domain.CompanionPlantRepository
import gdsc.plantory.plant.domain.HistoryType
Expand All @@ -8,6 +10,7 @@ import gdsc.plantory.plantInformation.domain.PlantInformationRepository
import gdsc.plantory.util.AcceptanceTest
import jakarta.persistence.EntityManager
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -24,6 +27,16 @@ class PlantServiceTest(
@Autowired val plantInformationRepository: PlantInformationRepository,
) : AcceptanceTest() {

@Test
fun `데일리 기록 히스토리는 직접 추가할 수 없다`() {
// when, then
assertThatThrownBy {
plantService.createPlantHistory(기록없는_테스트식물_ID, 테스터_디바이스_토큰, HistoryType.RECORDING)
}
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessageContaining("데일리 기록은 히스토리 타입을 직접 추가할 수 없습니다.")
}

@Test
fun `사용자는 식물의 데일리 기록을 조회하여 사진, 본문, 물준유무를 확인할 수 있다`() {
// given
Expand Down Expand Up @@ -60,7 +73,6 @@ class PlantServiceTest(
memberId = 1L,
)
companionPlant.saveRecord("test-record", "https://test.com", today)
companionPlant.saveHistory(HistoryType.RECORDING, today)
companionPlant.saveHistory(HistoryType.WATER_CHANGE, today)
val savedPlant = companionPlantRepository.save(companionPlant)

Expand Down Expand Up @@ -116,7 +128,6 @@ class PlantServiceTest(
)
val today = LocalDate.now()
companionPlant.saveRecord("test-record", "https://test.com", today)
companionPlant.saveHistory(HistoryType.RECORDING, today)
companionPlant.saveHistory(HistoryType.POT_CHANGE, today)
val savedPlant = companionPlantRepository.save(companionPlant)

Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/gdsc/plantory/util/DatabaseLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class DatabaseLoader(
val testCompanionPlantHasNoHistories = CompanionPlantFixture.generateTestCompanionPlantHasNoHistories(2L)

testCompanionPlantWillHaveHistories.saveRecord("test-record", "https://test.com")
testCompanionPlantWillHaveHistories.saveHistory(HistoryType.RECORDING)
testCompanionPlantWillHaveHistories.saveHistory(HistoryType.POT_CHANGE)
testCompanionPlantWillHaveHistories.saveHistory(HistoryType.WATER_CHANGE)

Expand Down
Loading