Skip to content

Commit

Permalink
fix(channel-service): fix addMessage into channel
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-borriello00 authored and Ro0t-set committed May 21, 2024
1 parent 663ad41 commit 5ad6d43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> = mutableListOf(),
) : Entity<ChannelId>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -54,7 +56,7 @@ class ServerRepositoryImpl(private val serverModelRepository: ServerModelReposit
description = entity.description,
owner = entity.owner,
users = entity.users,
channels = channelEntities
channels = channelEntities,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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"
}
}
}

0 comments on commit 5ad6d43

Please sign in to comment.