Skip to content

Commit

Permalink
feat: handle multiple story arcs in ComicInfoProvider
Browse files Browse the repository at this point in the history
StoryArc tag will be split on commas (',')

closes #282
  • Loading branch information
gotson committed Aug 27, 2020
1 parent 8a079ce commit f4451bf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ data class BookMetadataPatch(
val releaseDate: LocalDate?,
val authors: List<Author>?,

val readList: String?,
val readListNumber: Int?
)
val readLists: List<ReadListEntry> = emptyList()
) {
data class ReadListEntry(
val name: String,
val number: Int? = null
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,32 @@ class MetadataLifecycle(

// handle read lists
if (provider is ComicInfoProvider && library.importComicInfoReadList) {
patch?.let { bPatch ->
val readList = bPatch.readList
val readListNumber = bPatch.readListNumber


if (readList != null) {
readListRepository.findByNameOrNull(readList).let { existing ->
if (existing != null) {
if (existing.bookIds.containsValue(book.id))
logger.debug { "Book is already in existing readlist '${existing.name}'" }
else {
val map = existing.bookIds.toSortedMap()
val key = if (readListNumber != null && existing.bookIds.containsKey(readListNumber)) {
logger.debug { "Existing readlist '${existing.name}' already contains a book at position $readListNumber, adding book '${book.name}' at the end" }
existing.bookIds.lastKey() + 1
} else {
logger.debug { "Adding book '${book.name}' to existing readlist '${existing.name}'" }
readListNumber ?: existing.bookIds.lastKey() + 1
}
map[key] = book.id
readListLifecycle.updateReadList(
existing.copy(bookIds = map)
)
patch?.readLists?.forEach { readList ->

readListRepository.findByNameOrNull(readList.name).let { existing ->
if (existing != null) {
if (existing.bookIds.containsValue(book.id))
logger.debug { "Book is already in existing readlist '${existing.name}'" }
else {
val map = existing.bookIds.toSortedMap()
val key = if (readList.number != null && existing.bookIds.containsKey(readList.number)) {
logger.debug { "Existing readlist '${existing.name}' already contains a book at position ${readList.number}, adding book '${book.name}' at the end" }
existing.bookIds.lastKey() + 1
} else {
logger.debug { "Adding book '${book.name}' to existing readlist '${existing.name}'" }
readList.number ?: existing.bookIds.lastKey() + 1
}
} else {
logger.debug { "Adding book '${book.name}' to new readlist '$readList'" }
readListLifecycle.addReadList(ReadList(
name = readList,
bookIds = mapOf((readListNumber ?: 0) to book.id).toSortedMap()
))
map[key] = book.id
readListLifecycle.updateReadList(
existing.copy(bookIds = map)
)
}
} else {
logger.debug { "Adding book '${book.name}' to new readlist '$readList'" }
readListLifecycle.addReadList(ReadList(
name = readList.name,
bookIds = mapOf((readList.number ?: 0) to book.id).toSortedMap()
))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,23 @@ class ComicInfoProvider(
comicInfo.coverArtist?.let { authors += it.splitWithRole("cover") }
comicInfo.editor?.let { authors += it.splitWithRole("editor") }

val readLists = mutableListOf<BookMetadataPatch.ReadListEntry>()
comicInfo.alternateSeries?.let { readLists.add(BookMetadataPatch.ReadListEntry(it, comicInfo.alternateNumber?.toIntOrNull())) }

comicInfo.storyArc?.let { value ->
val arcs = value.split(",").map { it.trim() }
readLists.addAll(arcs.map { BookMetadataPatch.ReadListEntry(it) })
}


return BookMetadataPatch(
title = comicInfo.title,
summary = comicInfo.summary,
number = comicInfo.number,
numberSort = comicInfo.number?.toFloatOrNull(),
releaseDate = releaseDate,
authors = authors.ifEmpty { null },
readList = comicInfo.alternateSeries ?: comicInfo.storyArc,
readListNumber = comicInfo.alternateNumber?.toIntOrNull() ?: comicInfo.storyArcNumber?.toIntOrNull()
readLists = readLists
)
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ class ComicInfo {
@JsonProperty(value = "StoryArc")
var storyArc: String? = null

@JsonProperty(value = "StoryArcNumber")
var storyArcNumber: String? = null

@JsonProperty(value = "SeriesGroup")
var seriesGroup: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class EpubMetadataProvider(
number = null,
numberSort = null,
releaseDate = date,
authors = authors,
readList = null,
readListNumber = null
authors = authors
)
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ComicInfoProviderTest {
month = 2
alternateSeries = "story arc"
alternateNumber = "5"
storyArc = "one, two, three"
}

every { mockMapper.readValue(any<ByteArray>(), ComicInfo::class.java) } returns comicInfo
Expand All @@ -55,9 +56,15 @@ class ComicInfoProviderTest {
assertThat(summary).isEqualTo("summary")
assertThat(number).isEqualTo("010")
assertThat(numberSort).isEqualTo(10F)
assertThat(readList).isEqualTo("story arc")
assertThat(readListNumber).isEqualTo(5)
assertThat(releaseDate).isEqualTo(LocalDate.of(2020, 2, 1))
with(readLists) {
assertThat(this).hasSize(4)
assertThat(this.map { it.name }).containsExactly("story arc", "one", "two", "three")
this.first { it.number != null }.let {
assertThat(it.name).isEqualTo("story arc")
assertThat(it.number).isEqualTo(5)
}
}
}
}

Expand Down

0 comments on commit f4451bf

Please sign in to comment.