Improve update/upsert methods of repos using flatmap

This commit is contained in:
Paul Schaub 2019-12-02 02:25:48 +01:00
parent d4de49e25b
commit 39da217460
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 18 additions and 21 deletions

View File

@ -109,13 +109,12 @@ public class XmppAccountRepository
return data().select(AccountModel.class) return data().select(AccountModel.class)
.where(AccountModel.ID.eq(account.getId())) .where(AccountModel.ID.eq(account.getId()))
.get().maybe().toSingle() // to single .get().maybe().toSingle() // to single
.map(model -> { .flatMap(model -> {
// copy changes from the entity to the model // copy changes from the entity to the model
model = accountMapping.entityToModel(account, model); model = accountMapping.entityToModel(account, model);
// write the updated model back // write the updated model back
model = data().update(model).blockingGet(); return data().update(model);
return accountMapping.modelToEntity(model, account); }).map(model -> accountMapping.modelToEntity(model, account))
})
.subscribeOn(subscriberScheduler()) .subscribeOn(subscriberScheduler())
.observeOn(observerScheduler()); .observeOn(observerScheduler());
} }
@ -128,14 +127,13 @@ public class XmppAccountRepository
.get().maybe() .get().maybe()
// If it does not exist, create a new model from the entity // If it does not exist, create a new model from the entity
.switchIfEmpty(data().insert(accountMapping.entityToModel(account, new AccountModel()))) .switchIfEmpty(data().insert(accountMapping.entityToModel(account, new AccountModel())))
// finally .flatMap(model -> {
.map(model -> {
// update the model // update the model
model = accountMapping.entityToModel(account, model); model = accountMapping.entityToModel(account, model);
// write the updated model back // write the updated model back
model = data().update(model).blockingGet(); return data().update(model);
return accountMapping.modelToEntity(model, account);
}) })
.map(model -> accountMapping.modelToEntity(model, account))
.subscribeOn(subscriberScheduler()) .subscribeOn(subscriberScheduler())
.observeOn(observerScheduler()); .observeOn(observerScheduler());
} }

View File

@ -120,11 +120,11 @@ public class XmppDirectChatRepository
return data().select(DirectChatModel.class) return data().select(DirectChatModel.class)
.where(DirectChatModel.ID.eq(chat.getId())) .where(DirectChatModel.ID.eq(chat.getId()))
.get().maybe().toSingle() .get().maybe().toSingle()
.map(model -> { .flatMap(model -> {
model = directChatMapping.entityToModel(chat, model); model = directChatMapping.entityToModel(chat, model);
model = data().update(model).blockingGet(); return data().update(model);
return directChatMapping.modelToEntity(model, chat);
}) })
.map(model -> directChatMapping.modelToEntity(model, chat))
.subscribeOn(subscriberScheduler()) .subscribeOn(subscriberScheduler())
.observeOn(observerScheduler()); .observeOn(observerScheduler());
} }
@ -135,11 +135,11 @@ public class XmppDirectChatRepository
.where(DirectChatModel.ID.eq(chat.getId())) .where(DirectChatModel.ID.eq(chat.getId()))
.get().maybe() .get().maybe()
.switchIfEmpty(data().insert(directChatMapping.entityToModel(chat, new DirectChatModel()))) .switchIfEmpty(data().insert(directChatMapping.entityToModel(chat, new DirectChatModel())))
.map(model -> { .flatMap(model -> {
model = directChatMapping.entityToModel(chat, model); model = directChatMapping.entityToModel(chat, model);
model = data().update(model).blockingGet(); return data().update(model);
return directChatMapping.modelToEntity(model, chat);
}) })
.map(model -> directChatMapping.modelToEntity(model, chat))
.subscribeOn(subscriberScheduler()) .subscribeOn(subscriberScheduler())
.observeOn(observerScheduler()); .observeOn(observerScheduler());
} }

View File

@ -109,13 +109,13 @@ public class XmppGroupChatRepository
return data().select(GroupChatModel.class) return data().select(GroupChatModel.class)
.where(GroupChatModel.ID.eq(chat.getId())) .where(GroupChatModel.ID.eq(chat.getId()))
.get().maybe().toSingle() // to single .get().maybe().toSingle() // to single
.map(model -> { .flatMap(model -> {
// copy changes from entity to the model // copy changes from entity to the model
model = groupChatMapping.entityToModel(chat, model); model = groupChatMapping.entityToModel(chat, model);
// write the updated model back // write the updated model back
model = data().update(model).blockingGet(); return data().update(model);
return groupChatMapping.modelToEntity(model, chat);
}) })
.map(model -> groupChatMapping.modelToEntity(model, chat))
.subscribeOn(subscriberScheduler()) .subscribeOn(subscriberScheduler())
.observeOn(observerScheduler()); .observeOn(observerScheduler());
} }
@ -128,14 +128,13 @@ public class XmppGroupChatRepository
.get().maybe() .get().maybe()
// If it does not exist, create a new model from the entity // If it does not exist, create a new model from the entity
.switchIfEmpty(data().insert(groupChatMapping.entityToModel(chat, new GroupChatModel()))) .switchIfEmpty(data().insert(groupChatMapping.entityToModel(chat, new GroupChatModel())))
// finally .flatMap(model -> {
.map(model -> {
// update the model // update the model
model = groupChatMapping.entityToModel(chat, model); model = groupChatMapping.entityToModel(chat, model);
// write the updated model back // write the updated model back
model = data().update(model).blockingGet(); return data().update(model);
return groupChatMapping.modelToEntity(model, chat);
}) })
.map(model -> groupChatMapping.modelToEntity(model, chat))
.subscribeOn(subscriberScheduler()) .subscribeOn(subscriberScheduler())
.observeOn(observerScheduler()); .observeOn(observerScheduler());
} }