Skip to content

Commit

Permalink
Implement goal deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
zionhann committed Aug 6, 2024
1 parent 0341d56 commit 2123255
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/kotlin/com/likelionhgu/stepper/goal/Goal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.likelionhgu.stepper.goal

import com.likelionhgu.stepper.common.BaseTime
import com.likelionhgu.stepper.goal.enums.GoalStatus
import com.likelionhgu.stepper.journal.Journal
import com.likelionhgu.stepper.member.Member
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
Expand All @@ -11,6 +13,7 @@ import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import java.time.LocalDate

@Entity
Expand Down Expand Up @@ -45,6 +48,9 @@ class Goal(
@Column
var lastEntryDate: LocalDate? = null

@OneToMany(mappedBy = "goal", cascade = [CascadeType.REMOVE])
val journals: MutableList<Journal> = mutableListOf()

fun update(targetGoal: Goal) {
title = targetGoal.title
startDate = targetGoal.startDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class JournalService(
member = member,
goal = goal
).let(journalRepository::save)

goal.updateStreak(journal.createdDate.toLocalDate())
goal.journals.add(journal)

return journal.journalId
}
Expand Down Expand Up @@ -64,6 +66,9 @@ class JournalService(
}

fun deleteJournal(journalId: Long) {
val journal = journalRepository.findById(journalId).getOrNull()
?: throw JournalNotFoundException("Journal not found with ID: $journalId")
journal.goal?.journals?.remove(journal)
journalRepository.deleteById(journalId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class JournalServiceTest : BehaviorSpec({
goal = mockk()
)
every { goal.updateStreak(any<LocalDate>()) } returns Unit
every { goal.journals } returns mutableListOf()
val request = JournalRequest("title", "content")

`when`("the member writes the journal entry") {
Expand Down

0 comments on commit 2123255

Please sign in to comment.