-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from TeamMiso/feat/email-setting
๐ :: Email ์ธํ
- Loading branch information
Showing
9 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...andreas311/miso/domain/email/adapter/output/persistence/CommandEmailPersistenceAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package andreas311.miso.domain.email.adapter.output.persistence | ||
|
||
import andreas311.miso.domain.email.adapter.output.persistence.mapper.EmailMapper | ||
import andreas311.miso.domain.email.adapter.output.persistence.repository.EmailRepository | ||
import andreas311.miso.domain.email.application.port.output.CommandEmailPort | ||
import andreas311.miso.domain.email.domain.Email | ||
|
||
class CommandEmailPersistenceAdapter( | ||
private val emailRepository: EmailRepository, | ||
private val emailMapper: EmailMapper | ||
): CommandEmailPort { | ||
override fun saveEmail(email: Email): Email { | ||
val email = emailRepository.save(emailMapper.toEntity(email)) | ||
return emailMapper.toDomain(email)!! | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...main/kotlin/andreas311/miso/domain/email/adapter/output/persistence/entity/EmailEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package andreas311.miso.domain.email.adapter.output.persistence.entity | ||
|
||
import andreas311.miso.common.entitiy.BaseIdxEntity | ||
import javax.persistence.* | ||
|
||
@Entity | ||
@Table(name = "email") | ||
class EmailEntity( | ||
@Column(name = "email_id", nullable = false) | ||
val id: Long, | ||
|
||
@Column(name = "email", nullable = false) | ||
val email: String, | ||
|
||
@Column(name = "random_key", nullable = false) | ||
var randomKey: String, | ||
|
||
@Column(name = "authentication", nullable = false) | ||
var authentication: Boolean | ||
): BaseIdxEntity(id) |
26 changes: 26 additions & 0 deletions
26
...main/kotlin/andreas311/miso/domain/email/adapter/output/persistence/mapper/EmailMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package andreas311.miso.domain.email.adapter.output.persistence.mapper | ||
|
||
import andreas311.miso.domain.email.adapter.output.persistence.entity.EmailEntity | ||
import andreas311.miso.domain.email.domain.Email | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class EmailMapper { | ||
fun toEntity(domain: Email): EmailEntity = | ||
EmailEntity( | ||
id = domain.id, | ||
email = domain.email, | ||
randomKey = domain.randomKey, | ||
authentication = domain.authentication | ||
) | ||
|
||
fun toDomain(entity: EmailEntity): Email? = | ||
entity?.let { | ||
Email( | ||
id = entity.id, | ||
email = entity.email, | ||
randomKey = entity.randomKey, | ||
authentication = entity.authentication | ||
) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...lin/andreas311/miso/domain/email/adapter/output/persistence/repository/EmailRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package andreas311.miso.domain.email.adapter.output.persistence.repository | ||
|
||
import andreas311.miso.domain.email.adapter.output.persistence.entity.EmailEntity | ||
import org.springframework.data.repository.CrudRepository | ||
|
||
interface EmailRepository: CrudRepository<EmailEntity, Long> |
6 changes: 6 additions & 0 deletions
6
...ain/kotlin/andreas311/miso/domain/email/application/exception/EmailSendFailedException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package andreas311.miso.domain.email.application.exception | ||
|
||
import andreas311.miso.global.error.ErrorCode | ||
import andreas311.miso.global.error.exception.MisoException | ||
|
||
class EmailSendFailedException: MisoException(ErrorCode.EMAIL_SEND_FAIL) |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/andreas311/miso/domain/email/application/port/output/CommandEmailPort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package andreas311.miso.domain.email.application.port.output | ||
|
||
import andreas311.miso.domain.email.domain.Email | ||
|
||
interface CommandEmailPort { | ||
fun saveEmail(email: Email): Email | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/andreas311/miso/domain/email/application/port/output/EmailSendPort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package andreas311.miso.domain.email.application.port.output | ||
|
||
interface EmailSendPort { | ||
fun sendEmailAuthKey(email: String) | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/andreas311/miso/domain/email/domain/Email.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package andreas311.miso.domain.email.domain | ||
|
||
data class Email( | ||
val id: Long, | ||
val email: String, | ||
var randomKey: String, | ||
var authentication: Boolean | ||
) { | ||
fun updateAuthentication(authentication: Boolean) { | ||
this.authentication = authentication | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/kotlin/andreas311/miso/thirdparty/email/EmailSendAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package andreas311.miso.thirdparty.email | ||
|
||
import andreas311.miso.domain.email.application.exception.EmailSendFailedException | ||
import andreas311.miso.domain.email.application.port.output.CommandEmailPort | ||
import andreas311.miso.domain.email.application.port.output.EmailSendPort | ||
import andreas311.miso.domain.email.domain.Email | ||
import org.springframework.mail.javamail.JavaMailSender | ||
import org.springframework.mail.javamail.MimeMessageHelper | ||
import java.util.* | ||
import javax.mail.MessagingException | ||
|
||
class EmailSendAdapter( | ||
private val javaMailSender: JavaMailSender, | ||
private val commandEmailPort: CommandEmailPort | ||
): EmailSendPort { | ||
override fun sendEmailAuthKey(email: String) { | ||
val randomKey = createRandomKey() | ||
sendAuthEmail(email, randomKey) | ||
} | ||
|
||
private fun sendAuthEmail(email: String, randomKey: String) { | ||
val subject = "MISO ์ธ์ฆ๋ฒํธ๊ฐ ๋์ฐฉํ์ต๋๋ค!" | ||
val content = buildEmailContent(randomKey) | ||
|
||
try { | ||
sendEmail(email, subject, content) | ||
} catch (e: MessagingException) { | ||
throw EmailSendFailedException() | ||
} | ||
saveEmailToRepository(email, randomKey) | ||
} | ||
|
||
private fun buildEmailContent(randomKey: String): String { | ||
return """ | ||
<div style='margin:100px;'> | ||
<h1> ์๋ ํ์ธ์ MISO ์ ๋๋ค! </h1> | ||
<br> | ||
<h2><p>์๋ ์ธ์ฆ๋ฒํธ๋ฅผ ์ธ์ฆ ํ์ด์ง๋ก ๋์๊ฐ ์ ๋ ฅํด ์ฃผ์ธ์. ์ด์ฉํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!<p></h2> | ||
<br> | ||
<div align='center' style='border:1px solid black; font-family:verdana';> | ||
<h3 style='color:blue;'>์ธ์ฆ๋ฒํธ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค!</h3> | ||
<div style='font-size:130%'> | ||
์ธ์ฆ๋ฒํธ : <strong>$randomKey</strong><div><br/> | ||
</div> | ||
""".trimIndent() | ||
} | ||
|
||
private fun saveEmailToRepository(email: String, randomKey: String) { | ||
commandEmailPort.saveEmail( | ||
Email( | ||
id = 0L, | ||
email = email, | ||
randomKey = randomKey, | ||
authentication = false | ||
) | ||
) | ||
} | ||
|
||
private fun sendEmail(email: String, subject: String, content: String) { | ||
val mimeMessage = javaMailSender.createMimeMessage() | ||
val helper = MimeMessageHelper(mimeMessage, true, "utf-8") | ||
helper.setTo(email) | ||
helper.setSubject(subject) | ||
helper.setText(content, true) | ||
javaMailSender.send(mimeMessage) | ||
} | ||
|
||
private fun createRandomKey(): String { | ||
val random = Random() | ||
|
||
val randomKey = random.nextInt(8888) + 1111 | ||
|
||
return randomKey.toString() | ||
} | ||
} |