Skip to content

Commit

Permalink
Create a dedicated MailTypeId class
Browse files Browse the repository at this point in the history
  • Loading branch information
awelless committed Feb 17, 2024
1 parent d059acb commit f955094
Show file tree
Hide file tree
Showing 36 changed files with 128 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.mailit.core.admin.api.type.MailMessageTypeService
import io.mailit.core.admin.api.type.UpdateMailMessageTypeCommand
import io.mailit.core.model.MailMessageTemplate
import io.mailit.core.model.Slice
import io.mailit.value.MailTypeId
import jakarta.annotation.security.RolesAllowed
import jakarta.ws.rs.DELETE
import jakarta.ws.rs.GET
Expand All @@ -32,7 +33,7 @@ class MailMessageTypeResource(
@GET
@Path("/{id}")
suspend fun getById(@PathParam("id") id: Long): SingleMailMessageTypeResponseDto {
val mailType = mailMessageTypeService.getById(id)
val mailType = mailMessageTypeService.getById(MailTypeId(id))
return mailMessageTypeDtoMapper.toSingleDto(mailType)
}

Expand Down Expand Up @@ -66,7 +67,7 @@ class MailMessageTypeResource(
@Path("/{id}")
suspend fun update(@PathParam("id") id: Long, updateDto: MailMessageTypeUpdateDto): SingleMailMessageTypeResponseDto {
val command = UpdateMailMessageTypeCommand(
id = id,
id = MailTypeId(id),
description = updateDto.description,
maxRetriesCount = updateDto.maxRetriesCount,
templateEngine = updateDto.templateEngine,
Expand All @@ -82,5 +83,5 @@ class MailMessageTypeResource(
@DELETE
@Path("/{id}")
suspend fun delete(@PathParam("id") id: Long, @QueryParam("force") force: Boolean?) =
mailMessageTypeService.deleteMailType(id, force ?: false)
mailMessageTypeService.deleteMailType(MailTypeId(id), force ?: false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ResponseMailMessageTypeDtoMapper {

fun toSingleDto(mailType: MailMessageType) =
SingleMailMessageTypeResponseDto(
id = mailType.id.toString(),
id = mailType.id.value.toString(),
name = mailType.name,
description = mailType.description,
maxRetriesCount = mailType.maxRetriesCount,
Expand All @@ -26,7 +26,7 @@ class ResponseMailMessageTypeDtoMapper {

fun toPagedDto(mailType: MailMessageType) =
PagedMailMessageTypeResponseDto(
id = mailType.id.toString(),
id = mailType.id.value.toString(),
name = mailType.name,
description = mailType.description,
maxRetriesCount = mailType.maxRetriesCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.mailit.core.model.PlainTextMailMessageType
import io.mailit.template.api.TemplateEngine
import io.mailit.value.EmailAddress.Companion.toEmailAddress
import io.mailit.value.MailId
import io.mailit.value.MailTypeId
import java.util.concurrent.atomic.AtomicLong

private val counter = AtomicLong()
Expand All @@ -17,7 +18,7 @@ fun createPlainMailMessageType(): PlainTextMailMessageType {
val id = counter.incrementAndGet()

return PlainTextMailMessageType(
id = id,
id = MailTypeId(id),
name = "plain-type-$id",
description = "Some description",
maxRetriesCount = 111,
Expand All @@ -30,7 +31,7 @@ fun createHtmlMailMessageType(): HtmlMailMessageType {
val id = counter.incrementAndGet()

return HtmlMailMessageType(
id = id,
id = MailTypeId(id),
name = "html-type-$id",
description = "Some description",
createdAt = nowWithoutNanos(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import io.mailit.core.model.MailMessageTemplate
import io.mailit.core.model.MailMessageType
import io.mailit.core.model.Slice
import io.mailit.template.api.TemplateEngine
import io.mailit.value.MailTypeId

interface MailMessageTypeService {

suspend fun getById(id: Long): MailMessageType
suspend fun getById(id: MailTypeId): MailMessageType

suspend fun getAllSliced(page: Int, size: Int): Slice<MailMessageType>

suspend fun createNewMailType(command: CreateMailMessageTypeCommand): MailMessageType

suspend fun updateMailType(command: UpdateMailMessageTypeCommand): MailMessageType

suspend fun deleteMailType(id: Long, force: Boolean)
suspend fun deleteMailType(id: MailTypeId, force: Boolean)
}

data class CreateMailMessageTypeCommand(
Expand All @@ -28,7 +29,7 @@ data class CreateMailMessageTypeCommand(
)

data class UpdateMailMessageTypeCommand(
val id: Long,
val id: MailTypeId,
val description: String?,
val maxRetriesCount: Int?,
val templateEngine: TemplateEngine? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.mailit.core.model

import io.mailit.template.api.TemplateEngine
import io.mailit.value.MailTypeId
import java.time.Instant

/**
* Type of [MailMessage]. Contains general info for all mails of one type
*/
sealed class MailMessageType(

open val id: Long,
open val id: MailTypeId,

/**
* Name of actual mail type. Used in external API
Expand Down Expand Up @@ -43,8 +44,7 @@ enum class MailMessageTypeState {
* [MailMessageType] for plain "text" messages
*/
data class PlainTextMailMessageType(

override val id: Long,
override val id: MailTypeId,
override val name: String,
override var description: String? = null,
override var maxRetriesCount: Int? = null,
Expand All @@ -57,8 +57,7 @@ data class PlainTextMailMessageType(
* [MailMessageType] for "html" messages
*/
data class HtmlMailMessageType(

override val id: Long,
override val id: MailTypeId,
override val name: String,
override var description: String? = null,
override var maxRetriesCount: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.mailit.core.model.HtmlMailMessageType
import io.mailit.core.model.MailMessageType
import io.mailit.core.model.PlainTextMailMessageType
import io.mailit.idgenerator.api.IdGenerator
import io.mailit.value.MailTypeId
import java.time.Instant

interface MailMessageTypeFactory<T : MailMessageType> {
Expand Down Expand Up @@ -35,7 +36,7 @@ class PlainTextMailMessageTypeFactory(
val now = Instant.now()

return PlainTextMailMessageType(
id = idGenerator.generateId(),
id = MailTypeId(idGenerator.generateId()),
name = command.name,
description = command.description,
maxRetriesCount = command.maxRetriesCount,
Expand Down Expand Up @@ -64,7 +65,7 @@ class HtmlMailMessageTypeFactory(
val now = Instant.now()

return HtmlMailMessageType(
id = idGenerator.generateId(),
id = MailTypeId(idGenerator.generateId()),
name = command.name,
description = command.description,
maxRetriesCount = command.maxRetriesCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.mailit.core.model.MailMessageTypeState.DELETED
import io.mailit.core.model.MailMessageTypeState.FORCE_DELETED
import io.mailit.core.model.Slice
import io.mailit.core.spi.MailMessageTypeRepository
import io.mailit.value.MailTypeId
import java.time.Instant
import mu.KLogging

Expand All @@ -21,7 +22,7 @@ class MailMessageTypeServiceImpl(
) : MailMessageTypeService {
companion object : KLogging()

override suspend fun getById(id: Long): MailMessageType =
override suspend fun getById(id: MailTypeId): MailMessageType =
mailMessageTypeRepository.findById(id)
?: throw NotFoundException("MailMessageType with id: $id is not found")

Expand Down Expand Up @@ -56,7 +57,7 @@ class MailMessageTypeServiceImpl(
return updatedMailType
}

override suspend fun deleteMailType(id: Long, force: Boolean) {
override suspend fun deleteMailType(id: MailTypeId, force: Boolean) {
val newState = if (force) {
FORCE_DELETED
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import io.mailit.core.model.MailMessageTemplate
import io.mailit.core.model.PlainTextMailMessageType
import io.mailit.idgenerator.test.ConstantIdGenerator
import io.mailit.template.api.TemplateEngine.FREEMARKER
import io.mailit.value.MailTypeId
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class MailMessageTypeFactoryTest {

private val plainTextMailTypeId = 1L
private val htmlMailTypeId = 2L
private val plainTextMailTypeId = MailTypeId(1)
private val htmlMailTypeId = MailTypeId(2)

val mailMessageTypeFactory = MailMessageTypeFactoryManager(
PlainTextMailMessageTypeFactory(ConstantIdGenerator(plainTextMailTypeId)),
HtmlMailMessageTypeFactory(ConstantIdGenerator(htmlMailTypeId)),
PlainTextMailMessageTypeFactory(ConstantIdGenerator(plainTextMailTypeId.value)),
HtmlMailMessageTypeFactory(ConstantIdGenerator(htmlMailTypeId.value)),
)

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package io.mailit.core.spi
import io.mailit.core.model.MailMessageType
import io.mailit.core.model.MailMessageTypeState
import io.mailit.core.model.Slice
import io.mailit.value.MailTypeId
import java.time.Instant

interface MailMessageTypeRepository {

suspend fun findById(id: Long): MailMessageType?
suspend fun findById(id: MailTypeId): MailMessageType?

suspend fun findByName(name: String): MailMessageType?

Expand All @@ -17,5 +18,5 @@ interface MailMessageTypeRepository {

suspend fun update(mailMessageType: MailMessageType): MailMessageType

suspend fun updateState(id: Long, state: MailMessageTypeState, updatedAt: Instant): Int
suspend fun updateState(id: MailTypeId, state: MailMessageTypeState, updatedAt: Instant): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class MailMessageTypeResourceTest {
@Test
fun getById() {
When {
get(MAIL_TYPE_URL, mailType.id)
get(MAIL_TYPE_URL, mailType.id.value)
} Then {
statusCode(OK)

body(
ID equalTo mailType.id.toString(),
ID equalTo mailType.id.value.toString(),
NAME equalTo mailType.name,
DESCRIPTION equalTo mailType.description,
MAX_RETRIES_COUNT equalTo mailType.maxRetriesCount,
Expand All @@ -72,7 +72,7 @@ class MailMessageTypeResourceTest {
body(
"content.size()" equalTo 1,

"content[0].$ID" equalTo mailType.id.toString(),
"content[0].$ID" equalTo mailType.id.value.toString(),
"content[0].$NAME" equalTo mailType.name,
"content[0].$DESCRIPTION" equalTo mailType.description,
"content[0].$MAX_RETRIES_COUNT" equalTo mailType.maxRetriesCount,
Expand All @@ -94,7 +94,7 @@ class MailMessageTypeResourceTest {
body(
"content.size()" equalTo 1,

"content[0].$ID" equalTo mailType.id.toString(),
"content[0].$ID" equalTo mailType.id.value.toString(),
"content[0].$NAME" equalTo mailType.name,
"content[0].$DESCRIPTION" equalTo mailType.description,
"content[0].$MAX_RETRIES_COUNT" equalTo mailType.maxRetriesCount,
Expand Down Expand Up @@ -143,7 +143,7 @@ class MailMessageTypeResourceTest {
contentType(JSON)
body(updateDto)
} When {
put(MAIL_TYPE_URL, mailType.id)
put(MAIL_TYPE_URL, mailType.id.value)
} Then {
statusCode(OK)

Expand All @@ -159,7 +159,7 @@ class MailMessageTypeResourceTest {
Given {
param(FORCE_PARAM, "true")
} When {
delete(MAIL_TYPE_URL, mailType.id)
delete(MAIL_TYPE_URL, mailType.id.value)
} Then {
statusCode(ACCEPTED)
}
Expand All @@ -174,7 +174,7 @@ class MailMessageTypeResourceTest {
Given {
param(FORCE_PARAM, "true")
} When {
delete(MAIL_TYPE_URL, mailType.id)
delete(MAIL_TYPE_URL, mailType.id.value)
} Then {
statusCode(ACCEPTED)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class H2MailMessageRepository(
mailMessage.subject,
mailMessage.emailFrom?.email,
mailMessage.emailTo.email,
mailMessage.type.id,
mailMessage.type.id.value,
mailMessage.createdAt,
mailMessage.sendingStartedAt,
mailMessage.sentAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.mailit.persistence.h2.MailMessageContent.HTML
import io.mailit.persistence.h2.MailMessageContent.PLAIN_TEXT
import io.mailit.persistence.h2.Tables.MAIL_MESSAGE_TEMPLATE
import io.mailit.persistence.h2.Tables.MAIL_MESSAGE_TYPE
import io.mailit.value.MailTypeId
import java.sql.SQLException
import java.time.Instant
import javax.sql.DataSource
Expand Down Expand Up @@ -115,13 +116,13 @@ class H2MailMessageTypeRepository(
private val singleMapper = SingleResultSetMapper { it.getMailMessageTypeFromRow() }
private val multipleMapper = MultipleResultSetMapper { it.getMailMessageTypeFromRow() }

override suspend fun findById(id: Long): MailMessageType? =
override suspend fun findById(id: MailTypeId): MailMessageType? =
dataSource.connection.use {
queryRunner.query(
it,
FIND_BY_ID_SQL,
singleMapper,
id,
id.value,
)
}

Expand Down Expand Up @@ -157,7 +158,7 @@ class H2MailMessageTypeRepository(
it.withTransaction {
queryRunner.update(
it, INSERT_MAIL_TYPE_SQL,
mailMessageType.id,
mailMessageType.id.value,
mailMessageType.name,
mailMessageType.description,
mailMessageType.maxRetriesCount,
Expand All @@ -169,7 +170,7 @@ class H2MailMessageTypeRepository(
)

if (mailMessageType is HtmlMailMessageType) {
queryRunner.update(it, INSERT_MAIL_TEMPLATE_SQL, mailMessageType.id, mailMessageType.template.compressedValue)
queryRunner.update(it, INSERT_MAIL_TEMPLATE_SQL, mailMessageType.id.value, mailMessageType.template.compressedValue)
}
}
}
Expand All @@ -192,7 +193,7 @@ class H2MailMessageTypeRepository(
it,
UPDATE_TEMPLATE_SQL,
(mailMessageType as? HtmlMailMessageType)?.template?.compressedValue,
mailMessageType.id,
mailMessageType.id.value,
)

queryRunner.update(
Expand All @@ -202,7 +203,7 @@ class H2MailMessageTypeRepository(
mailMessageType.maxRetriesCount,
mailMessageType.updatedAt,
(mailMessageType as? HtmlMailMessageType)?.templateEngine?.name,
mailMessageType.id,
mailMessageType.id.value,
)
}
}
Expand All @@ -214,14 +215,14 @@ class H2MailMessageTypeRepository(
return mailMessageType
}

override suspend fun updateState(id: Long, state: MailMessageTypeState, updatedAt: Instant): Int =
override suspend fun updateState(id: MailTypeId, state: MailMessageTypeState, updatedAt: Instant): Int =
dataSource.connection.use {
queryRunner.update(
it,
UPDATE_STATE_SQL,
state.name,
updatedAt,
id,
id.value,
)
}

Expand Down
Loading

0 comments on commit f955094

Please sign in to comment.