david dereba
01/17/2024, 10:16 AM"message": {
"data": [
{
"clientId": 5,
"clientName": "Saka",
"createdAt": "2024-01-11T12:13:12",
"phoneNumber": "+1234567890"
},
{
"clientId": 6,
"clientName": "Partey",
"createdAt": "2024-01-11T12:14:18",
"phoneNumber": "+11234567890"
},
{
"clientId": 7,
"clientName": "Martinelli",
"createdAt": "2024-01-15T12:10:56",
"phoneNumber": "+254715176167"
},
{
"clientId": 8,
"clientName": "Master",
"createdAt": "2024-01-16T11:32:37",
"phoneNumber": "+12234567890",
"accountId": 5
}
]
},
"responseCode": 302,
"requestStatus": "successful",
"timestamp": "2024-01-17T12:55:45.621865757"
but it is not in the format that I would like. that is I would have wanted to ensure that if the value is not found in the db, then the object is returned as null. For instance, I would expect the object with clientId like 7 to have returned accountId = null but as you can see in the above object, because accountId was null it was not totally included in the return object. now, i want it to be always be included as null.
I will share my implementation, and sugest ways i can achieve that.
model
@Serializable
data class Client (
val clientId: Int,
val clientName: String?=null,
@Contextual
val createdAt: LocalDateTime?=null,
@Contextual
val lastTimeOnline: LocalDateTime?= null,
val phoneNumber: String?=null,
val accountId: Int?=null
)
schema
object Clients : Table("client") {
val clientId = integer("client_id").autoIncrement()
val clientName = varchar("client_name", length = 100).nullable()
val createdAt = datetime("created_at")
val lastTimeOnline = datetime("last_time_online")
val phoneNumber = varchar("phone_number", length = 100).nullable()
val accountId = integer("account_id").nullable()
override val primaryKey = PrimaryKey(clientId, name = "client_id")
}
repositories
object ClientRepository {
private val gson = GsonBuilder().serializeNulls().create() // Create Gson with serializeNulls enabled
suspend fun registerClient(dispatcher: CoroutineDispatcher = Dispatchers.Default, client: Client): Int =
withContext(dispatcher){
suspendedTransactionAsync {
val insertStatement = Clients.insert { row ->
client.clientName?.let { value -> row[clientName] = value }
client.phoneNumber?.let { value -> row[phoneNumber] = value }
client.accountId?.let { value -> row[accountId] = value }
}
val generatedId = insertStatement[Clients.clientId]
generatedId
}.await()
}
suspend fun updateClientDetails(dispatcher: CoroutineDispatcher = Dispatchers.Default, client: Client): Boolean =
withContext(dispatcher){
transaction {
val updateStatement = Clients.update({Clients.clientId eq client.clientId}){
client.clientName?.let { value -> it[clientName] = value }
client.phoneNumber?.let { value -> it[phoneNumber] =value }
client.accountId?.let { value -> it[accountId] = value }
}
updateStatement >0
}
}
suspend fun getClient(clientId: Int): Client? {
return transaction {
return@transaction Clients.select { Clients.clientId eq clientId }
.singleOrNull()
?.toClient()
}
}
suspend fun getAllClients(): List<Client> {
return transaction {
return@transaction Clients.selectAll().map { it.toClient() }
}
}
suspend fun checkIfClientExists(clientId: Int): Boolean = transaction {
return@transaction Clients.select { Clients.clientId eq clientId }.singleOrNull() != null
}
suspend fun checkIfClientExistsByMsisdn(msisdn: String): Boolean = transaction {
return@transaction Clients.select { Clients.phoneNumber eq msisdn }.singleOrNull() != null
}
suspend fun deleteClient(clientId: Int): Boolean = transaction {
return@transaction Clients.deleteWhere { Clients.clientId eq clientId } ==1
}
private fun ResultRow.toClient(): Client {
return Client(
this[Clients.clientId],
this[Clients.clientName],
this[Clients.createdAt],
this[Clients.lastTimeOnline],
this[Clients.phoneNumber],
this[Clients.accountId]
)
}
private fun Client.toClientDataString(): String{
return Gson().toJson(this)
}
}
how can i be able to implement the same? like if null
return accountId : nullAdamW
01/17/2024, 12:50 PMAdamW
01/17/2024, 1:04 PMserializeNulls
, one without. Likely you use toClientDataString()
somewhere which probably should use the existing GSON instance instead of creating a new one.david dereba
01/21/2024, 12:43 AMserializeNulls
david dereba
01/21/2024, 12:44 AM