Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): manage book read progress per user
ability to mark a book as read, unread, or in progress by storing the last page read related to #25
- Loading branch information
Showing
15 changed files
with
568 additions
and
28 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
komga/src/flyway/resources/db/migration/V20200601173217__read_progress.sql
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,15 @@ | ||
create table read_progress | ||
( | ||
book_id bigint not null, | ||
user_id bigint not null, | ||
created_date timestamp not null default now(), | ||
last_modified_date timestamp not null default now(), | ||
page integer not null, | ||
completed boolean not null | ||
); | ||
|
||
alter table read_progress | ||
add constraint fk_read_progress_book_book_id foreign key (book_id) references book (id); | ||
|
||
alter table read_progress | ||
add constraint fk_read_progress_user_user_id foreign key (user_id) references user (id); |
13 changes: 13 additions & 0 deletions
13
komga/src/main/kotlin/org/gotson/komga/domain/model/ReadProgress.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,13 @@ | ||
package org.gotson.komga.domain.model | ||
|
||
import java.time.LocalDateTime | ||
|
||
data class ReadProgress( | ||
val bookId: Long, | ||
val userId: Long, | ||
val page: Int, | ||
val completed: Boolean, | ||
|
||
override val createdDate: LocalDateTime = LocalDateTime.now(), | ||
override val lastModifiedDate: LocalDateTime = LocalDateTime.now() | ||
) : Auditable() |
18 changes: 18 additions & 0 deletions
18
komga/src/main/kotlin/org/gotson/komga/domain/persistence/ReadProgressRepository.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,18 @@ | ||
package org.gotson.komga.domain.persistence | ||
|
||
import org.gotson.komga.domain.model.ReadProgress | ||
|
||
|
||
interface ReadProgressRepository { | ||
fun findAll(): Collection<ReadProgress> | ||
fun findByBookIdAndUserId(bookId: Long, userId: Long): ReadProgress? | ||
fun findByUserId(userId: Long): Collection<ReadProgress> | ||
|
||
fun save(readProgress: ReadProgress) | ||
|
||
fun delete(bookId: Long, userId: Long) | ||
fun deleteByUserId(userId: Long) | ||
fun deleteByBookId(bookId: Long) | ||
fun deleteByBookIds(bookIds: Collection<Long>) | ||
fun deleteAll() | ||
} |
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
91 changes: 91 additions & 0 deletions
91
komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/ReadProgressDao.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,91 @@ | ||
package org.gotson.komga.infrastructure.jooq | ||
|
||
import org.gotson.komga.domain.model.ReadProgress | ||
import org.gotson.komga.domain.persistence.ReadProgressRepository | ||
import org.gotson.komga.jooq.Tables | ||
import org.gotson.komga.jooq.tables.records.ReadProgressRecord | ||
import org.jooq.DSLContext | ||
import org.springframework.stereotype.Component | ||
import java.time.LocalDateTime | ||
|
||
@Component | ||
class ReadProgressDao( | ||
private val dsl: DSLContext | ||
) : ReadProgressRepository { | ||
|
||
private val r = Tables.READ_PROGRESS | ||
|
||
override fun findAll(): Collection<ReadProgress> = | ||
dsl.selectFrom(r) | ||
.fetchInto(r) | ||
.map { it.toDomain() } | ||
|
||
override fun findByBookIdAndUserId(bookId: Long, userId: Long): ReadProgress? = | ||
dsl.selectFrom(r) | ||
.where(r.BOOK_ID.eq(bookId).and(r.USER_ID.eq(userId))) | ||
.fetchOneInto(r) | ||
?.toDomain() | ||
|
||
override fun findByUserId(userId: Long): Collection<ReadProgress> = | ||
dsl.selectFrom(r) | ||
.where(r.USER_ID.eq(userId)) | ||
.fetchInto(r) | ||
.map { it.toDomain() } | ||
|
||
|
||
override fun save(readProgress: ReadProgress) { | ||
dsl.mergeInto(r) | ||
.using(dsl.selectOne()) | ||
.on(r.BOOK_ID.eq(readProgress.bookId).and(r.USER_ID.eq(readProgress.userId))) | ||
.whenMatchedThenUpdate() | ||
.set(r.PAGE, readProgress.page) | ||
.set(r.COMPLETED, readProgress.completed) | ||
.set(r.LAST_MODIFIED_DATE, LocalDateTime.now()) | ||
.whenNotMatchedThenInsert() | ||
.set(r.BOOK_ID, readProgress.bookId) | ||
.set(r.USER_ID, readProgress.userId) | ||
.set(r.PAGE, readProgress.page) | ||
.set(r.COMPLETED, readProgress.completed) | ||
.execute() | ||
} | ||
|
||
|
||
override fun delete(bookId: Long, userId: Long) { | ||
dsl.deleteFrom(r) | ||
.where(r.BOOK_ID.eq(bookId).and(r.USER_ID.eq(userId))) | ||
.execute() | ||
} | ||
|
||
override fun deleteByUserId(userId: Long) { | ||
dsl.deleteFrom(r) | ||
.where(r.USER_ID.eq(userId)) | ||
.execute() | ||
} | ||
|
||
override fun deleteByBookId(bookId: Long) { | ||
dsl.deleteFrom(r) | ||
.where(r.BOOK_ID.eq(bookId)) | ||
.execute() | ||
} | ||
|
||
override fun deleteByBookIds(bookIds: Collection<Long>) { | ||
dsl.deleteFrom(r) | ||
.where(r.BOOK_ID.`in`(bookIds)) | ||
.execute() | ||
} | ||
|
||
override fun deleteAll() { | ||
dsl.deleteFrom(r).execute() | ||
} | ||
|
||
|
||
private fun ReadProgressRecord.toDomain() = | ||
ReadProgress( | ||
bookId = bookId, | ||
userId = userId, | ||
page = page, | ||
completed = completed, | ||
createdDate = createdDate, | ||
lastModifiedDate = lastModifiedDate | ||
) | ||
} |
Oops, something went wrong.