Improve update/upsert methods of repos using flatmap
This commit is contained in:
parent
d4de49e25b
commit
39da217460
|
@ -109,13 +109,12 @@ public class XmppAccountRepository
|
|||
return data().select(AccountModel.class)
|
||||
.where(AccountModel.ID.eq(account.getId()))
|
||||
.get().maybe().toSingle() // to single
|
||||
.map(model -> {
|
||||
.flatMap(model -> {
|
||||
// copy changes from the entity to the model
|
||||
model = accountMapping.entityToModel(account, model);
|
||||
// write the updated model back
|
||||
model = data().update(model).blockingGet();
|
||||
return accountMapping.modelToEntity(model, account);
|
||||
})
|
||||
return data().update(model);
|
||||
}).map(model -> accountMapping.modelToEntity(model, account))
|
||||
.subscribeOn(subscriberScheduler())
|
||||
.observeOn(observerScheduler());
|
||||
}
|
||||
|
@ -128,14 +127,13 @@ public class XmppAccountRepository
|
|||
.get().maybe()
|
||||
// If it does not exist, create a new model from the entity
|
||||
.switchIfEmpty(data().insert(accountMapping.entityToModel(account, new AccountModel())))
|
||||
// finally
|
||||
.map(model -> {
|
||||
.flatMap(model -> {
|
||||
// update the model
|
||||
model = accountMapping.entityToModel(account, model);
|
||||
// write the updated model back
|
||||
model = data().update(model).blockingGet();
|
||||
return accountMapping.modelToEntity(model, account);
|
||||
return data().update(model);
|
||||
})
|
||||
.map(model -> accountMapping.modelToEntity(model, account))
|
||||
.subscribeOn(subscriberScheduler())
|
||||
.observeOn(observerScheduler());
|
||||
}
|
||||
|
|
|
@ -120,11 +120,11 @@ public class XmppDirectChatRepository
|
|||
return data().select(DirectChatModel.class)
|
||||
.where(DirectChatModel.ID.eq(chat.getId()))
|
||||
.get().maybe().toSingle()
|
||||
.map(model -> {
|
||||
.flatMap(model -> {
|
||||
model = directChatMapping.entityToModel(chat, model);
|
||||
model = data().update(model).blockingGet();
|
||||
return directChatMapping.modelToEntity(model, chat);
|
||||
return data().update(model);
|
||||
})
|
||||
.map(model -> directChatMapping.modelToEntity(model, chat))
|
||||
.subscribeOn(subscriberScheduler())
|
||||
.observeOn(observerScheduler());
|
||||
}
|
||||
|
@ -135,11 +135,11 @@ public class XmppDirectChatRepository
|
|||
.where(DirectChatModel.ID.eq(chat.getId()))
|
||||
.get().maybe()
|
||||
.switchIfEmpty(data().insert(directChatMapping.entityToModel(chat, new DirectChatModel())))
|
||||
.map(model -> {
|
||||
.flatMap(model -> {
|
||||
model = directChatMapping.entityToModel(chat, model);
|
||||
model = data().update(model).blockingGet();
|
||||
return directChatMapping.modelToEntity(model, chat);
|
||||
return data().update(model);
|
||||
})
|
||||
.map(model -> directChatMapping.modelToEntity(model, chat))
|
||||
.subscribeOn(subscriberScheduler())
|
||||
.observeOn(observerScheduler());
|
||||
}
|
||||
|
|
|
@ -109,13 +109,13 @@ public class XmppGroupChatRepository
|
|||
return data().select(GroupChatModel.class)
|
||||
.where(GroupChatModel.ID.eq(chat.getId()))
|
||||
.get().maybe().toSingle() // to single
|
||||
.map(model -> {
|
||||
.flatMap(model -> {
|
||||
// copy changes from entity to the model
|
||||
model = groupChatMapping.entityToModel(chat, model);
|
||||
// write the updated model back
|
||||
model = data().update(model).blockingGet();
|
||||
return groupChatMapping.modelToEntity(model, chat);
|
||||
return data().update(model);
|
||||
})
|
||||
.map(model -> groupChatMapping.modelToEntity(model, chat))
|
||||
.subscribeOn(subscriberScheduler())
|
||||
.observeOn(observerScheduler());
|
||||
}
|
||||
|
@ -128,14 +128,13 @@ public class XmppGroupChatRepository
|
|||
.get().maybe()
|
||||
// If it does not exist, create a new model from the entity
|
||||
.switchIfEmpty(data().insert(groupChatMapping.entityToModel(chat, new GroupChatModel())))
|
||||
// finally
|
||||
.map(model -> {
|
||||
.flatMap(model -> {
|
||||
// update the model
|
||||
model = groupChatMapping.entityToModel(chat, model);
|
||||
// write the updated model back
|
||||
model = data().update(model).blockingGet();
|
||||
return groupChatMapping.modelToEntity(model, chat);
|
||||
return data().update(model);
|
||||
})
|
||||
.map(model -> groupChatMapping.modelToEntity(model, chat))
|
||||
.subscribeOn(subscriberScheduler())
|
||||
.observeOn(observerScheduler());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue