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)
.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());
}

View File

@ -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());
}

View File

@ -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());
}