forked from gotson/komga
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): search books by read status
related to gotson#25
- Loading branch information
Showing
7 changed files
with
271 additions
and
14 deletions.
There are no files selected for viewing
14 changes: 13 additions & 1 deletion
14
komga/src/main/kotlin/org/gotson/komga/domain/model/BookSearch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package org.gotson.komga.domain.model | ||
|
||
data class BookSearch( | ||
open class BookSearch( | ||
val libraryIds: Collection<Long> = emptyList(), | ||
val seriesIds: Collection<Long> = emptyList(), | ||
val searchTerm: String? = null, | ||
val mediaStatus: Collection<Media.Status> = emptyList() | ||
) | ||
|
||
class BookSearchWithReadProgress( | ||
libraryIds: Collection<Long> = emptyList(), | ||
seriesIds: Collection<Long> = emptyList(), | ||
searchTerm: String? = null, | ||
mediaStatus: Collection<Media.Status> = emptyList(), | ||
val readStatus: Collection<ReadStatus> = emptyList() | ||
) : BookSearch(libraryIds, seriesIds, searchTerm, mediaStatus) | ||
|
||
enum class ReadStatus { | ||
UNREAD, READ, IN_PROGRESS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
komga/src/main/kotlin/org/gotson/komga/interfaces/rest/persistence/BookDtoRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
komga/src/test/kotlin/org/gotson/komga/infrastructure/jooq/BookDtoDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package org.gotson.komga.infrastructure.jooq | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.gotson.komga.domain.model.BookSearchWithReadProgress | ||
import org.gotson.komga.domain.model.KomgaUser | ||
import org.gotson.komga.domain.model.ReadProgress | ||
import org.gotson.komga.domain.model.ReadStatus | ||
import org.gotson.komga.domain.model.makeBook | ||
import org.gotson.komga.domain.model.makeLibrary | ||
import org.gotson.komga.domain.model.makeSeries | ||
import org.gotson.komga.domain.persistence.BookRepository | ||
import org.gotson.komga.domain.persistence.KomgaUserRepository | ||
import org.gotson.komga.domain.persistence.LibraryRepository | ||
import org.gotson.komga.domain.persistence.ReadProgressRepository | ||
import org.gotson.komga.domain.persistence.SeriesRepository | ||
import org.gotson.komga.domain.service.BookLifecycle | ||
import org.gotson.komga.domain.service.KomgaUserLifecycle | ||
import org.gotson.komga.domain.service.LibraryLifecycle | ||
import org.gotson.komga.domain.service.SeriesLifecycle | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.test.context.junit.jupiter.SpringExtension | ||
|
||
@ExtendWith(SpringExtension::class) | ||
@SpringBootTest | ||
@AutoConfigureTestDatabase | ||
class BookDtoDaoTest( | ||
@Autowired private val bookDtoDao: BookDtoDao, | ||
@Autowired private val bookRepository: BookRepository, | ||
@Autowired private val bookLifecycle: BookLifecycle, | ||
@Autowired private val seriesRepository: SeriesRepository, | ||
@Autowired private val seriesLifecycle: SeriesLifecycle, | ||
@Autowired private val libraryRepository: LibraryRepository, | ||
@Autowired private val libraryLifecycle: LibraryLifecycle, | ||
@Autowired private val readProgressRepository: ReadProgressRepository, | ||
@Autowired private val userRepository: KomgaUserRepository, | ||
@Autowired private val userLifecycle: KomgaUserLifecycle | ||
) { | ||
|
||
private var library = makeLibrary() | ||
private var series = makeSeries("Series") | ||
private var user = KomgaUser("user@example.org", "", false) | ||
|
||
@BeforeAll | ||
fun setup() { | ||
library = libraryRepository.insert(library) | ||
series = seriesLifecycle.createSeries(series.copy(libraryId = library.id)) | ||
user = userRepository.save(user) | ||
} | ||
|
||
@AfterEach | ||
fun deleteBooks() { | ||
bookRepository.findAll().forEach { | ||
bookLifecycle.delete(it.id) | ||
} | ||
} | ||
|
||
@AfterAll | ||
fun tearDown() { | ||
userRepository.findAll().forEach { | ||
userLifecycle.deleteUser(it) | ||
} | ||
libraryRepository.findAll().forEach { | ||
libraryLifecycle.deleteLibrary(it) | ||
} | ||
} | ||
|
||
private fun setupBooks() { | ||
seriesLifecycle.addBooks(series, | ||
(1..3).map { | ||
makeBook("$it", seriesId = series.id, libraryId = library.id) | ||
}) | ||
|
||
val books = bookRepository.findAll().sortedBy { it.name } | ||
books.elementAt(0).let { readProgressRepository.save(ReadProgress(it.id, user.id, 5, false)) } | ||
books.elementAt(1).let { readProgressRepository.save(ReadProgress(it.id, user.id, 5, true)) } | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for read books then only read books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.READ)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(1) | ||
assertThat(found.first().readProgress?.completed).isTrue() | ||
assertThat(found.first().name).isEqualTo("2") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for unread books then only unread books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.UNREAD)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(1) | ||
assertThat(found.first().readProgress).isNull() | ||
assertThat(found.first().name).isEqualTo("3") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for in progress books then only in progress books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.IN_PROGRESS)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(1) | ||
assertThat(found.first().readProgress?.completed).isFalse() | ||
assertThat(found.first().name).isEqualTo("1") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for read and unread books then only matching books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.READ, ReadStatus.UNREAD)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(2) | ||
assertThat(found.map { it.name }).containsExactlyInAnyOrder("2", "3") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for read and in progress books then only matching books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.READ, ReadStatus.IN_PROGRESS)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(2) | ||
assertThat(found.map { it.name }).containsExactlyInAnyOrder("2", "1") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for unread and in progress books then only matching books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.UNREAD, ReadStatus.IN_PROGRESS)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(2) | ||
assertThat(found.map { it.name }).containsExactlyInAnyOrder("3", "1") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching for read and unread and in progress books then only matching books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(readStatus = listOf(ReadStatus.UNREAD, ReadStatus.IN_PROGRESS, ReadStatus.READ)), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(3) | ||
assertThat(found.map { it.name }).containsExactlyInAnyOrder("3", "1", "2") | ||
} | ||
|
||
@Test | ||
fun `given books in various read status when searching without read progress then all books are returned`() { | ||
// given | ||
setupBooks() | ||
|
||
// when | ||
val found = bookDtoDao.findAll( | ||
BookSearchWithReadProgress(), | ||
user.id, | ||
PageRequest.of(0, 20) | ||
) | ||
|
||
// then | ||
assertThat(found).hasSize(3) | ||
assertThat(found.map { it.name }).containsExactlyInAnyOrder("3", "1", "2") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters