Creating value objects

Similar to what we did when creating the messenger backend, we need to create value objects to model common types of data we will be handling across the application. Create a vo package in the data package. The value objects we are creating are already familiar to you. In fact, they are exactly the same as those we created during the development of the API. We are going to create ConversationListVO, ConversationVO, UserListVO, UserVO, and MessageVO. Create Kotlin files to hold each of these value objects in the vo package. Before creating any list value object data models, we have to create the basic models. These models are UserVO, MessageVO, and ConversationVO.

Create a UserVO data class, as follows:

package com.example.messenger.data.vo

data class UserVO(
val id: Long,
val username: String,
val phoneNumber: String,
val status: String,
val createdAt: String
)

As we have created value objects in the past, the previous code doesn't need much explaining. Add MessageVO to your MessageVO.kt file, as follows:

package com.example.messenger.data.vo

data class MessageVO(
val id: Long,
val senderId: Long,
val recipientId: Long,
val conversationId: Long,
val body: String,
val createdAt: String
)

Now create a ConversationVo data class in ConversationVO.kt, as follows:

package com.example.messenger.data.vo

data class ConversationVO(
val conversationId: Long,
val secondPartyUsername: String,
val messages: ArrayList<MessageVO>
)

Having created the basic value objects, let's create ConversationListVO and UserListVO, shall we? ConversationListVO is as follows:

package com.example.messenger.data.vo

data class ConversationListVO(
val conversations: List<ConversationVO>
)

The ConversationListVO data class has a single conversations property of the List type that can only contain elements of the ConversationVO type. The UserListVO data class is similar to ConversationListVO, with the exception that it has a user's property, which can only contain elements of the UserVO type instead of a conversations property. The following is the UserListVO data class:

package com.example.messenger.data.vo

data class UserListVO(
val users: List<UserVO>
)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57