Skip to content

Commit

Permalink
Correct order of CRUD operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakubk15 committed Dec 31, 2024
1 parent e3c74f1 commit 1137be0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public LockerRepositoryOrmLite(DatabaseManager databaseManager, Scheduler schedu

@Override
public CompletableFuture<Void> save(Locker locker) {
this.addToCache(locker);
return this.save(LockerWrapper.class, LockerWrapper.from(locker)).thenApply(dao -> null);
return this.save(LockerWrapper.class, LockerWrapper.from(locker)).thenApply(dao -> {
this.addToCache(locker);
return null;
});
}

@Override
Expand All @@ -64,12 +66,21 @@ public CompletableFuture<Optional<Locker>> findByPosition(Position position) {

@Override
public CompletableFuture<Integer> remove(UUID uuid) {
return this.deleteById(LockerWrapper.class, uuid);
return this.deleteById(LockerWrapper.class, uuid)
.thenApply(result -> {
if (result > 0) {
Locker locker = this.cache.remove(uuid);
if (locker != null) {
this.positionCache.remove(locker.position());
}
}
return result;
});
}

@Override
public CompletableFuture<Integer> remove(Locker locker) {
return this.action(LockerWrapper.class, dao -> dao.delete(LockerWrapper.from(locker)));
return this.remove(locker.uuid());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public ParcelRepositoryOrmLite(DatabaseManager databaseManager, Scheduler schedu

@Override
public CompletableFuture<Void> save(Parcel parcel) {
this.addToCache(parcel);
return this.save(ParcelWrapper.class, ParcelWrapper.from(parcel)).thenApply(dao -> null);
return this.save(ParcelWrapper.class, ParcelWrapper.from(parcel)).thenApply(dao -> {
this.addToCache(parcel);
return null;
});
}

@Override
Expand Down Expand Up @@ -112,6 +114,6 @@ private void removeFromCache(UUID uuid) {

@Override
public Optional<Parcel> findParcel(UUID uuid) {
return Optional.of(this.cache.get(uuid));
return Optional.ofNullable(this.cache.get(uuid));
}
}

0 comments on commit 1137be0

Please sign in to comment.