Fix/kobo library snapshot (#2051)
* Fix Kobo snapshot ownership and clean existing rows * Add Kobo snapshot ownership and clean existing rows test.
This commit is contained in:
+18
-1
@@ -1,6 +1,8 @@
|
||||
package com.adityachandel.booklore.service.kobo;
|
||||
|
||||
import com.adityachandel.booklore.mapper.BookEntityToKoboSnapshotBookMapper;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||
import com.adityachandel.booklore.model.entity.KoboDeletedBookProgressEntity;
|
||||
import com.adityachandel.booklore.model.entity.KoboSnapshotBookEntity;
|
||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||
@@ -117,7 +119,10 @@ public class KoboLibrarySnapshotService {
|
||||
}
|
||||
|
||||
private List<KoboSnapshotBookEntity> mapBooksToKoboSnapshotBook(ShelfEntity shelf, KoboLibrarySnapshotEntity snapshot) {
|
||||
Long userId = snapshot.getUserId();
|
||||
|
||||
return shelf.getBookEntities().stream()
|
||||
.filter(book -> isBookOwnedByUser(book, userId))
|
||||
.filter(koboCompatibilityService::isBookSupportedForKobo)
|
||||
.map(book -> {
|
||||
KoboSnapshotBookEntity snapshotBook = mapper.toKoboSnapshotBook(book);
|
||||
@@ -127,8 +132,20 @@ public class KoboLibrarySnapshotService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private boolean isBookOwnedByUser(BookEntity book, Long userId) {
|
||||
if (book == null || book.getLibrary() == null || book.getLibrary().getUsers() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return book.getLibrary()
|
||||
.getUsers()
|
||||
.stream()
|
||||
.map(BookLoreUserEntity::getId)
|
||||
.anyMatch(id -> Objects.equals(id, userId));
|
||||
}
|
||||
|
||||
public void deleteById(String id) {
|
||||
koboLibrarySnapshotRepository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
DELETE ksb
|
||||
FROM kobo_library_snapshot_book ksb
|
||||
JOIN kobo_library_snapshot ks ON ks.id = ksb.snapshot_id
|
||||
LEFT JOIN book b ON b.id = ksb.book_id
|
||||
LEFT JOIN user_library_mapping ulm
|
||||
ON ulm.library_id = b.library_id
|
||||
AND ulm.user_id = ks.user_id
|
||||
WHERE ulm.user_id IS NULL;
|
||||
Reference in New Issue
Block a user