From 463c5d9b4330e889ac617c6814678616266c9154 Mon Sep 17 00:00:00 2001 From: woowabrie Date: Mon, 9 Oct 2023 13:30:18 +0900 Subject: [PATCH] refactor: delete factory method from MailHistory --- .../apply/application/mail/MailHistoryService.kt | 4 ++-- .../kotlin/apply/config/DatabaseInitializer.kt | 10 ++++++---- src/main/kotlin/apply/domain/mail/MailHistory.kt | 14 ++------------ src/test/kotlin/apply/MailHistoryFixtures.kt.kt | 4 ++-- .../apply/application/MailHistoryServiceTest.kt | 6 +++--- .../apply/domain/mail/MailHistoryRepositoryTest.kt | 4 ++-- 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/apply/application/mail/MailHistoryService.kt b/src/main/kotlin/apply/application/mail/MailHistoryService.kt index 94e5df492..fa73669ee 100644 --- a/src/main/kotlin/apply/application/mail/MailHistoryService.kt +++ b/src/main/kotlin/apply/application/mail/MailHistoryService.kt @@ -23,10 +23,10 @@ class MailHistoryService( val mailHistories = mutableListOf() if (event.succeedRecipients.isNotEmpty()) { - mailHistories.add(MailHistory.ofSuccess(mailMessage, event.succeedRecipients)) + mailHistories.add(MailHistory(mailMessage, event.succeedRecipients, true)) } if (event.failedRecipients.isNotEmpty()) { - mailHistories.add(MailHistory.ofFailure(mailMessage, event.failedRecipients)) + mailHistories.add(MailHistory(mailMessage, event.failedRecipients, false)) } mailHistoryRepository.saveAll(mailHistories) diff --git a/src/main/kotlin/apply/config/DatabaseInitializer.kt b/src/main/kotlin/apply/config/DatabaseInitializer.kt index a1432e446..eb123af38 100644 --- a/src/main/kotlin/apply/config/DatabaseInitializer.kt +++ b/src/main/kotlin/apply/config/DatabaseInitializer.kt @@ -442,13 +442,15 @@ class DatabaseInitializer( mailMessageRepository.save(mailMessage) val mailHistories = listOf( - MailHistory.ofSuccess( + MailHistory( mailMessage = mailMessage, - recipients = mailMessage.recipients.subList(0, 2) + recipients = mailMessage.recipients.subList(0, 2), + true ), - MailHistory.ofSuccess( + MailHistory( mailMessage = mailMessage, - recipients = mailMessage.recipients.subList(3, 4) + recipients = mailMessage.recipients.subList(3, 4), + true ) ) mailHistoryRepository.saveAll(mailHistories) diff --git a/src/main/kotlin/apply/domain/mail/MailHistory.kt b/src/main/kotlin/apply/domain/mail/MailHistory.kt index 7178006ff..7caf2a9c5 100644 --- a/src/main/kotlin/apply/domain/mail/MailHistory.kt +++ b/src/main/kotlin/apply/domain/mail/MailHistory.kt @@ -12,7 +12,7 @@ import javax.persistence.Lob import javax.persistence.ManyToOne @Entity -class MailHistory private constructor( +class MailHistory( @ManyToOne @JoinColumn(nullable = false, foreignKey = ForeignKey(name = "fk_mail_history_to_mail_message")) val mailMessage: MailMessage, @@ -28,14 +28,4 @@ class MailHistory private constructor( @Column(nullable = false) val sentTime: LocalDateTime = LocalDateTime.now(), id: Long = 0L -) : BaseEntity(id) { - companion object { - fun ofSuccess(mailMessage: MailMessage, recipients: List): MailHistory { - return MailHistory(mailMessage, recipients, true) - } - - fun ofFailure(mailMessage: MailMessage, recipients: List): MailHistory { - return MailHistory(mailMessage, recipients, false) - } - } -} +) : BaseEntity(id) diff --git a/src/test/kotlin/apply/MailHistoryFixtures.kt.kt b/src/test/kotlin/apply/MailHistoryFixtures.kt.kt index dec3c4212..a9c2bfa04 100644 --- a/src/test/kotlin/apply/MailHistoryFixtures.kt.kt +++ b/src/test/kotlin/apply/MailHistoryFixtures.kt.kt @@ -62,11 +62,11 @@ fun createMailReservation( ) } -fun createSuccessMailHistory2( +fun createSuccessMailHistory( subject: String = SUBJECT, body: String = BODY, sender: String = SENDER, recipients: List = RECIPIENTS ): MailHistory { - return MailHistory.ofSuccess(createMailMessage(subject, body, sender, recipients), recipients) + return MailHistory(createMailMessage(subject, body, sender, recipients), recipients, true) } diff --git a/src/test/kotlin/apply/application/MailHistoryServiceTest.kt b/src/test/kotlin/apply/application/MailHistoryServiceTest.kt index bddf7e607..ee3f818bd 100644 --- a/src/test/kotlin/apply/application/MailHistoryServiceTest.kt +++ b/src/test/kotlin/apply/application/MailHistoryServiceTest.kt @@ -1,7 +1,7 @@ package apply.application import apply.application.mail.MailHistoryService -import apply.createSuccessMailHistory2 +import apply.createSuccessMailHistory import apply.domain.mail.MailHistoryRepository import apply.domain.mail.MailMessageRepository import io.kotest.core.spec.style.BehaviorSpec @@ -19,8 +19,8 @@ class MailHistoryServiceTest : BehaviorSpec({ Given("메일 이력이 있는 경우") { every { mailHistoryRepository.findAll() } returns listOf( - createSuccessMailHistory2(subject = "제목1"), - createSuccessMailHistory2(subject = "제목2") + createSuccessMailHistory(subject = "제목1"), + createSuccessMailHistory(subject = "제목2") ) When("모든 메일 이력을 조회하면") { diff --git a/src/test/kotlin/apply/domain/mail/MailHistoryRepositoryTest.kt b/src/test/kotlin/apply/domain/mail/MailHistoryRepositoryTest.kt index a576f8661..896f0079a 100644 --- a/src/test/kotlin/apply/domain/mail/MailHistoryRepositoryTest.kt +++ b/src/test/kotlin/apply/domain/mail/MailHistoryRepositoryTest.kt @@ -21,14 +21,14 @@ class MailHistoryRepositoryTest( val mailMessage = mailMessageRepository.save(createMailMessage()) expect("메일 발송 성공에 대한 히스토리를 저장한다") { - val actual = mailHistoryRepository.save(MailHistory.ofSuccess(mailMessage, mailMessage.recipients)) + val actual = mailHistoryRepository.save(MailHistory(mailMessage, mailMessage.recipients, true)) actual.id.shouldNotBeZero() } } context("메일 히스토리 조회") { val mailMessage = mailMessageRepository.save(createMailMessage()) - mailHistoryRepository.save(MailHistory.ofSuccess(mailMessage, mailMessage.recipients)) + mailHistoryRepository.save(MailHistory(mailMessage, mailMessage.recipients, true)) expect("메일 발송 성공에 대한 히스토리를 저장한다") { val actual = mailHistoryRepository.findAll()