Skip to content

Commit

Permalink
Merge pull request #233 from zucchero-sintattico/sap/docs
Browse files Browse the repository at this point in the history
Improve docs
  • Loading branch information
manuandru authored Jul 5, 2024
2 parents a1e2e42 + 65e4523 commit 3b72726
Show file tree
Hide file tree
Showing 67 changed files with 3,722 additions and 532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ open class FriendshipService(
val friendship =
friendshipRepository.findByMembers(request.requestFrom, request.friend)
?: return Result.failure(FriendshipServiceException.FriendshipNotFoundException())
return Result.success(FriendshipQuery.GetMessages.Response(friendship.directMessages))
return Result.success(FriendshipQuery.GetMessages.Response(friendship.messages))
}

override fun getFriendshipRequests(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package piperkt.services.friendships.application.api.query

import piperkt.services.friendships.application.api.ServiceRequest
import piperkt.services.friendships.domain.DirectMessage
import piperkt.services.friendships.domain.Message

sealed interface FriendshipQuery {
sealed interface GetMessages : FriendshipQuery {
Expand All @@ -12,7 +12,7 @@ sealed interface FriendshipQuery {
override val requestFrom: String
) : GetMessages, ServiceRequest

data class Response(val directMessages: List<DirectMessage>) : GetMessages
data class Response(val messages: List<Message>) : GetMessages
}

sealed interface GetFriendshipRequests : FriendshipQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import piperkt.common.ddd.AggregateRoot
class Friendship(
id: FriendshipId = FriendshipId(),
val users: Set<String> = mutableSetOf(),
val directMessages: MutableList<DirectMessage> = mutableListOf(),
val messages: MutableList<Message> = mutableListOf(),
) : AggregateRoot<FriendshipId>(id) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Friendship) return false
if (!super.equals(other)) return false

if (users != other.users) return false
if (directMessages != other.directMessages) return false
if (messages != other.messages) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + users.hashCode()
result = 31 * result + directMessages.hashCode()
result = 31 * result + messages.hashCode()
return result
}

fun addMessage(directMessage: DirectMessage) {
directMessages.add(directMessage)
fun addMessage(message: Message) {
messages.add(message)
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package piperkt.services.friendships.domain
import java.time.Instant
import piperkt.common.ddd.Entity

class DirectMessage(
override val id: DirectMessageId = DirectMessageId(),
class Message(
override val id: MessageId = MessageId(),
val sender: String,
val content: String,
val timestamp: Instant,
) : Entity<DirectMessageId>(id) {
) : Entity<MessageId>(id) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is DirectMessage) return false
if (other !is Message) return false

if (id != other.id) return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package piperkt.services.friendships.domain

import piperkt.common.ddd.UUIDEntityId

class DirectMessageId(value: String = newId()) : UUIDEntityId(value)
class MessageId(value: String = newId()) : UUIDEntityId(value)
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package piperkt.services.friendships.domain.factory

import piperkt.common.ddd.Factory
import piperkt.services.friendships.domain.DirectMessage
import piperkt.services.friendships.domain.Friendship
import piperkt.services.friendships.domain.Message

object FriendshipFactory : Factory<Friendship> {

fun createFriendship(
firstUser: String,
secondUser: String,
directMessages: List<DirectMessage> = emptyList()
) =
Friendship(
users = setOf(firstUser, secondUser),
directMessages = directMessages.toMutableList()
)
messages: List<Message> = emptyList()
) = Friendship(users = setOf(firstUser, secondUser), messages = messages.toMutableList())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package piperkt.services.friendships.domain.factory

import java.time.Instant
import piperkt.common.ddd.Factory
import piperkt.services.friendships.domain.DirectMessage
import piperkt.services.friendships.domain.DirectMessageId
import piperkt.services.friendships.domain.Message
import piperkt.services.friendships.domain.MessageId

object MessageFactory : Factory<DirectMessage> {
object MessageFactory : Factory<Message> {

fun createMessage(
content: String,
sender: String,
timeStamp: String = Instant.now().toString(),
id: String = DirectMessageId().value,
id: String = MessageId().value,
) =
DirectMessage(
Message(
sender = sender,
content = content,
timestamp = Instant.parse(timeStamp),
id = DirectMessageId(id)
id = MessageId(id)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class FriendshipEntity(
FriendshipEntity(
id = friendship.id.value,
users = friendship.users,
messages = friendship.directMessages.map { MessageEntity.fromDomain(it) }
messages = friendship.messages.map { MessageEntity.fromDomain(it) }
)
}
}
Expand All @@ -27,7 +27,7 @@ fun FriendshipEntity.toDomain() =
Friendship(
id = FriendshipId(id),
users = users,
directMessages = messages.map { it.toDomain() }.toMutableList()
messages = messages.map { it.toDomain() }.toMutableList()
)

@MongoRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package piperkt.services.friendships.infrastructure.persistence.model
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import java.time.Instant
import piperkt.services.friendships.domain.DirectMessage
import piperkt.services.friendships.domain.Message
import piperkt.services.friendships.domain.factory.MessageFactory

@MappedEntity
Expand All @@ -14,12 +14,12 @@ data class MessageEntity(
val timestamp: String = Instant.now().toString()
) {
companion object {
fun fromDomain(directMessage: DirectMessage) =
fun fromDomain(message: Message) =
MessageEntity(
id = directMessage.id.value,
content = directMessage.content,
sender = directMessage.sender,
timestamp = directMessage.timestamp.toString()
id = message.id.value,
content = message.content,
sender = message.sender,
timestamp = message.timestamp.toString()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class FriendshipHttpController(private val friendshipService: FriendshipService)
)
.getOrThrow()
return FriendshipApi.GetFriendshipMessages.Response(
messages = response.directMessages.map { DirectMessageDTO.fromDomain(it) }
messages = response.messages.map { DirectMessageDTO.fromDomain(it) }
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package piperkt.services.friendships.presentation

import io.micronaut.serde.annotation.Serdeable
import piperkt.services.friendships.domain.DirectMessage
import piperkt.services.friendships.domain.DirectMessageId
import piperkt.services.friendships.domain.Message
import piperkt.services.friendships.domain.MessageId

@Serdeable
data class DirectMessageDTO(
Expand All @@ -11,22 +11,22 @@ data class DirectMessageDTO(
val content: String,
val timestamp: String = java.time.Instant.now().toString(),
) {
fun toDomain(): DirectMessage {
return DirectMessage(
id = DirectMessageId(id),
fun toDomain(): Message {
return Message(
id = MessageId(id),
sender = sender,
content = content,
timestamp = java.time.Instant.parse(timestamp)
)
}

companion object {
fun fromDomain(directMessage: DirectMessage): DirectMessageDTO {
fun fromDomain(message: Message): DirectMessageDTO {
return DirectMessageDTO(
id = directMessage.id.value,
sender = directMessage.sender,
content = directMessage.content,
timestamp = directMessage.timestamp.toString()
id = message.id.value,
sender = message.sender,
content = message.content,
timestamp = message.timestamp.toString()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FriendshipServiceFeatureTest : BasicFriendshipServiceTest() {
MessageFactory.createMessage(request.from, "Hello"),
MessageFactory.createMessage(request.to, "Hi")
)
friendship.directMessages.addAll(fakeMessages)
friendship.messages.addAll(fakeMessages)
whenever(mockedFriendshipRepository.findByMembers(any(), any())).thenReturn(friendship)
service.getMessages(
FriendshipQuery.GetMessages.Request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class FriendshipRepositoryImplTest(private val friendshipRepository: FriendshipR
val friendship = FriendshipFactory.createFriendship("alice", "bob")
friendshipRepository.save(friendship)
friendship.addMessage(
directMessage = MessageFactory.createMessage(sender = "alice", content = "Hello, Bob!")
message = MessageFactory.createMessage(sender = "alice", content = "Hello, Bob!")
)
friendshipRepository.update(friendship)
friendshipRepository.findByMembers("alice", "bob")?.let {
it.directMessages.size shouldBe 1
it.directMessages[0].content shouldBe "Hello, Bob!"
it.messages.size shouldBe 1
it.messages[0].content shouldBe "Hello, Bob!"
}
}
}
Loading

0 comments on commit 3b72726

Please sign in to comment.