From 5ad6d43c23f93675e19321dfe28667e4b5854ae5 Mon Sep 17 00:00:00 2001 From: luigi-borriello00 Date: Wed, 22 May 2024 00:05:56 +0200 Subject: [PATCH] fix(channel-service): fix addMessage into channel --- .../services/servers/domain/Channel.kt | 14 +------------ .../repository/ServerRepositoryImpl.kt | 6 ++++-- .../repository/ServerRepositoryImplTest.kt | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/servers-service/src/main/kotlin/piperkt/services/servers/domain/Channel.kt b/servers-service/src/main/kotlin/piperkt/services/servers/domain/Channel.kt index 3a0b2f6ba..5d59d0195 100644 --- a/servers-service/src/main/kotlin/piperkt/services/servers/domain/Channel.kt +++ b/servers-service/src/main/kotlin/piperkt/services/servers/domain/Channel.kt @@ -7,25 +7,13 @@ enum class ChannelType { MULTIMEDIA } -open class Channel( +data class Channel( override val id: ChannelId = ChannelId(), var name: String, val type: ChannelType, var description: String, val messages: MutableList = mutableListOf(), ) : Entity(id) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is Channel) return false - - if (id != other.id) return false - - return true - } - - override fun hashCode(): Int { - return id.hashCode() - } fun addMessage(message: Message) { messages.add(message) diff --git a/servers-service/src/main/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImpl.kt b/servers-service/src/main/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImpl.kt index b44b653f7..fffa6cdb0 100644 --- a/servers-service/src/main/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImpl.kt +++ b/servers-service/src/main/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImpl.kt @@ -8,6 +8,7 @@ import piperkt.services.servers.application.ServerRepository import piperkt.services.servers.domain.Server import piperkt.services.servers.domain.ServerId import piperkt.services.servers.infrastructure.persistence.model.ChannelEntity +import piperkt.services.servers.infrastructure.persistence.model.MessageEntity import piperkt.services.servers.infrastructure.persistence.model.ServerEntity import piperkt.services.servers.infrastructure.persistence.model.ServerModelRepository @@ -44,7 +45,8 @@ class ServerRepositoryImpl(private val serverModelRepository: ServerModelReposit id = it.id.value, name = it.name, description = it.description, - channelType = it.type.toString() + channelType = it.type.toString(), + messages = it.messages.map(MessageEntity::fromDomain) ) } serverModelRepository.update( @@ -54,7 +56,7 @@ class ServerRepositoryImpl(private val serverModelRepository: ServerModelReposit description = entity.description, owner = entity.owner, users = entity.users, - channels = channelEntities + channels = channelEntities, ) ) } diff --git a/servers-service/src/test/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImplTest.kt b/servers-service/src/test/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImplTest.kt index 1c916eac1..7f3d643bf 100644 --- a/servers-service/src/test/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImplTest.kt +++ b/servers-service/src/test/kotlin/piperkt/services/servers/infrastructure/persistence/repository/ServerRepositoryImplTest.kt @@ -5,6 +5,7 @@ import io.kotest.matchers.shouldBe import piperkt.services.servers.application.ServerRepository import piperkt.services.servers.domain.ChannelType import piperkt.services.servers.domain.factory.ChannelFactory +import piperkt.services.servers.domain.factory.MessageFactory import piperkt.services.servers.domain.factory.ServerFactory class ServerRepositoryImplTest(private val serverRepository: ServerRepository) : IntegrationTest() { @@ -94,6 +95,7 @@ class ServerRepositoryImplTest(private val serverRepository: ServerRepository) : val server = ServerFactory.createServer("serverName", "serverDescription", "owner") val channel = ChannelFactory.createFromType("channelName", "channelDescription", "TEXT") server.addChannel(channel) + serverRepository.save(server) channel.name = "newChannelName" channel.description = "newChannelDescription" serverRepository.update(server) @@ -103,4 +105,22 @@ class ServerRepositoryImplTest(private val serverRepository: ServerRepository) : it.channels[0].description shouldBe "newChannelDescription" } } + + @Test + fun `should add a message to a channel`() { + val server = ServerFactory.createServer("serverName", "serverDescription", "owner") + val channel = ChannelFactory.createFromType("channelName", "channelDescription", "TEXT") + server.addChannel(channel) + serverRepository.save(server) + val message = MessageFactory.createMessage(sender = "sender", content = "content") + channel.addMessage(message) + serverRepository.update(server) + serverRepository.findById(server.id)?.let { + it.channels.size shouldBe 1 + it.channels[0].messages.size shouldBe 1 + it.channels[0].messages[0].id shouldBe message.id + it.channels[0].messages[0].sender shouldBe "sender" + it.channels[0].messages[0].content shouldBe "content" + } + } }