From b528b3d56d02eb837adbe8bd19b21128f0eaf884 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Fri, 20 Aug 2021 15:28:21 +0800 Subject: [PATCH] fix: skip duplicate books during reading list import closes #622 --- ERRORCODES.md | 1 + komga-webui/src/locales/en.json | 3 +- .../komga/domain/service/ReadListMatcher.kt | 1 + .../domain/service/ReadListMatcherTest.kt | 48 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/ERRORCODES.md b/ERRORCODES.md index 6b599b829..18a1abdbe 100644 --- a/ERRORCODES.md +++ b/ERRORCODES.md @@ -26,3 +26,4 @@ ERR_1019 | Cannot import file that is part of an existing library ERR_1020 | Book to upgrade does not belong to provided series ERR_1021 | Destination file already exists ERR_1022 | Newly imported book could not be scanned +ERR_1023 | Book already present in ReadingList diff --git a/komga-webui/src/locales/en.json b/komga-webui/src/locales/en.json index 035bbccb1..0690f8320 100644 --- a/komga-webui/src/locales/en.json +++ b/komga-webui/src/locales/en.json @@ -535,7 +535,8 @@ "ERR_1019": "Cannot import file that is part of an existing library", "ERR_1020": "Book to upgrade does not belong to provided series", "ERR_1021": "Destination file already exists", - "ERR_1022": "Newly imported book could not be scanned" + "ERR_1022": "Newly imported book could not be scanned", + "ERR_1023": "Book already present in ReadingList" }, "filter": { "age_rating": "age rating", diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/service/ReadListMatcher.kt b/komga/src/main/kotlin/org/gotson/komga/domain/service/ReadListMatcher.kt index 8febcfe8e..69a4f2361 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/service/ReadListMatcher.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/service/ReadListMatcher.kt @@ -45,6 +45,7 @@ class ReadListMatcher( when { bookMatches.size > 1 -> unmatchedBooks += ReadListRequestResultBook(book, "ERR_1013") bookMatches.isEmpty() -> unmatchedBooks += ReadListRequestResultBook(book, "ERR_1014") + bookIds.contains(bookMatches.first()) -> unmatchedBooks += ReadListRequestResultBook(book, "ERR_1023") else -> bookIds.add(bookMatches.first()) } } diff --git a/komga/src/test/kotlin/org/gotson/komga/domain/service/ReadListMatcherTest.kt b/komga/src/test/kotlin/org/gotson/komga/domain/service/ReadListMatcherTest.kt index 106f2052c..f096f5790 100644 --- a/komga/src/test/kotlin/org/gotson/komga/domain/service/ReadListMatcherTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/domain/service/ReadListMatcherTest.kt @@ -220,4 +220,52 @@ class ReadListMatcherTest( assertThat(unmatchedBooks[3].errorCode).isEqualTo("ERR_1013") } } + + @Test + fun `given request with duplicate books when matching then returns result with appropriate error codes`() { + // given + val booksSeries1 = listOf( + makeBook("book1", libraryId = library.id), + makeBook("book2", libraryId = library.id), + ) + makeSeries(name = "batman", libraryId = library.id).let { s -> + seriesLifecycle.createSeries(s) + seriesLifecycle.addBooks(s, booksSeries1) + seriesLifecycle.sortBooks(s) + } + + val request = ReadListRequest( + name = "readlist", + books = listOf( + ReadListRequestBook(series = "batman", number = "1"), + ReadListRequestBook(series = "batman", number = "2"), + ReadListRequestBook(series = "batman", number = "2"), + ) + ) + + // when + val result = readListMatcher.matchReadListRequest(request) + + // then + with(result) { + assertThat(readList).isNotNull + with(readList!!) { + assertThat(name).isEqualTo(request.name) + assertThat(bookIds).hasSize(2) + assertThat(bookIds).containsExactlyEntriesOf( + mapOf( + 0 to booksSeries1[0].id, + 1 to booksSeries1[1].id, + ) + ) + } + + assertThat(errorCode).isBlank + + assertThat(unmatchedBooks).hasSize(1) + + assertThat(unmatchedBooks[0].book).isEqualTo(request.books[2]) + assertThat(unmatchedBooks[0].errorCode).isEqualTo("ERR_1023") + } + } }