Skip to content

Commit

Permalink
chore: test embedding full userbook
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Apr 24, 2024
1 parent cb049ea commit e171117
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 13 deletions.
30 changes: 27 additions & 3 deletions src/jelu-ui/src/components/TagBooks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dataService from "../services/DataService";
import { ObjectUtils } from '../utils/ObjectUtils';
import BookCard from "./BookCard.vue";
import SortFilterBarVue from "./SortFilterBar.vue";
import { ReadingEventType } from '../model/ReadingEvent';
const { t } = useI18n({
inheritLocale: true,
Expand All @@ -31,13 +32,15 @@ const { sortQuery, sortOrder, sortBy, sortOrderUpdated } = useSort('title,asc')
const libraryFilter: Ref<LibraryFilter> = useRouteQuery('libraryFilter', 'ANY' as LibraryFilter)
const eventTypes: Ref<Array<ReadingEventType>> = useRouteQuery('lastEventTypes', [])
const { showSelect, selectAll, checkedCards, cardChecked, toggleEdit } = useBulkEdition(modalClosed)
const open = ref(false)
const getBooksIsLoading: Ref<boolean> = ref(false)
watch([() => route.params.tagId, page, sortQuery, libraryFilter], (newVal, oldVal) => {
watch([() => route.params.tagId, page, sortQuery, libraryFilter, eventTypes], (newVal, oldVal) => {
console.log(newVal + " " + oldVal)
if (newVal !== oldVal && route.params.tagId !== undefined) {
throttledGetBooks()
Expand All @@ -64,7 +67,7 @@ const getBooks = () => {
getBooksIsLoading.value = true
dataService.getTagBooksById(route.params.tagId as string,
pageAsNumber.value - 1, perPage.value, sortQuery.value,
libraryFilter.value)
libraryFilter.value, eventTypes.value)
.then(res => {
console.log(res)
total.value = res.totalElements
Expand All @@ -89,7 +92,7 @@ const throttledGetBooks = useThrottleFn(() => {
getBooks()
}, 100, false)
const convertedBooks = computed(() => tagBooks.value?.map(b => ObjectUtils.toUserBook(b)))
const convertedBooks = computed(() => tagBooks.value?.map(b => ObjectUtils.unwrapUserBook(b)))
function modalClosed() {
console.log("modal closed")
Expand Down Expand Up @@ -178,6 +181,27 @@ getBooks()
{{ t('filtering.only_not_in_my_list') }}
</o-radio>
</div>
<div class="field capitalize flex flex-col gap-1">
<label class="label">{{ t('reading_events.last_event_type') }} : </label>
<o-checkbox
v-model="eventTypes"
native-value="FINISHED"
>
{{ t('reading_events.finished') }}
</o-checkbox>
<o-checkbox
v-model="eventTypes"
native-value="CURRENTLY_READING"
>
{{ t('reading_events.currently_reading') }}
</o-checkbox>
<o-checkbox
v-model="eventTypes"
native-value="DROPPED"
>
{{ t('reading_events.dropped') }}
</o-checkbox>
</div>
</template>
</sort-filter-bar-vue>
<div class="flex flex-row justify-between">
Expand Down
5 changes: 3 additions & 2 deletions src/jelu-ui/src/model/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export interface Book {
amazonId?: string,
goodreadsId?: string,
librarythingId?: string,
language?: string
userBookId?: string
language?: string,
userBookId?: string,
userbook?: UserBook,
}
export interface UserBook {
id?: string,
Expand Down
11 changes: 8 additions & 3 deletions src/jelu-ui/src/services/DataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,20 @@ class DataService {
}

getTagBooksById = async (tagId: string,
page?: number, size?: number, sort?: string, libraryFilter?: LibraryFilter) => {
page?: number, size?: number, sort?: string, libraryFilter?: LibraryFilter, lastEventTypes?: Array<ReadingEventType> | null) => {
try {
const response = await this.apiClient.get<Page<Book>>(`${this.API_TAG}/${tagId}${this.API_BOOK}`, {
params: {
page: page,
size: size,
sort: sort,
libraryFilter: libraryFilter
}
libraryFilter: libraryFilter,
lastEventTypes: lastEventTypes,
},
paramsSerializer: {
serialize : (params) => {
return qs.stringify(params, { arrayFormat: 'comma' })
}},
});
console.log("called tag books by id")
console.log(response)
Expand Down
18 changes: 18 additions & 0 deletions src/jelu-ui/src/utils/ObjectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ export class ObjectUtils {
return converted
}

public static unwrapUserBook = (book: Book): UserBook => {
const userbook = book.userbook
if (userbook != undefined) {
userbook.book = {
...book,
userbook: undefined
}
console.log('ub')
console.log(userbook)
return userbook
} else {
const converted = {
id: undefined,
book: book
} as UserBook
return converted
}
}
public static swalMixin = Swal.mixin({
background: '#404040',
color: '#ffffff',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ class BooksController(
fun tagBooksById(
@PathVariable("id") tagId: UUID,
@RequestParam(name = "libraryFilter", required = false) libraryFilter: LibraryFilter?,
@RequestParam(name = "lastEventTypes", required = false) eventTypes: List<ReadingEventType>?,
@PageableDefault(page = 0, size = 20, direction = Sort.Direction.ASC, sort = ["title"]) @ParameterObject pageable: Pageable,
principal: Authentication,
): Page<BookDto> {
assertIsJeluUser(principal.principal)
return repository.findTagBooksById(tagId, (principal.principal as JeluUser).user, pageable, libraryFilter ?: LibraryFilter.ANY)
return repository.findTagBooksById(tagId, (principal.principal as JeluUser).user, pageable, libraryFilter ?: LibraryFilter.ANY, eventTypes)
}

@GetMapping(path = ["/tags/orphans"])
Expand Down
14 changes: 12 additions & 2 deletions src/main/kotlin/io/github/bayang/jelu/dao/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ class BookRepository(

fun findSeriesById(seriesId: UUID): Series = Series[seriesId]

fun findTagBooksById(tagId: UUID, user: User, pageable: Pageable, filter: LibraryFilter = LibraryFilter.ANY): Page<Book> {
fun findTagBooksById(
tagId: UUID,
user: User,
pageable: Pageable,
filter: LibraryFilter = LibraryFilter.ANY,
eventTypes: List<ReadingEventType>?,
): Page<Book> {
val booksWithSameIdAndUserHasUserbook = BookTable.join(UserBookTable, JoinType.LEFT)
.slice(BookTable.id)
.select { UserBookTable.book eq BookTable.id and (UserBookTable.user eq user.id) }
Expand All @@ -374,10 +380,13 @@ class BookRepository(
if (filter == LibraryFilter.ONLY_USER_BOOKS) {
// only books where user has an userbook
query.andWhere { UserBookTable.user eq user.id }
} else if (filter == LibraryFilter.ONLY_NON_USER_BOOKS) {
} else if (filter == LibraryFilter.ONLY_NON_USER_BOOKS && eventTypes.isNullOrEmpty()) {
// only books where there are no userbooks or only other users have userbooks
query.andWhere { BookTable.id notInSubQuery booksWithSameIdAndUserHasUserbook }
}
if (!eventTypes.isNullOrEmpty()) {
query.andWhere { UserBookTable.user eq user.id and(UserBookTable.lastReadingEvent inList eventTypes) }
}
query.withDistinct(true)
val total = query.count()
query.limit(pageable.pageSize, pageable.offset)
Expand Down Expand Up @@ -539,6 +548,7 @@ class BookRepository(
val userbook = e.userBooks.firstOrNull { u -> u.user.id.value == userId }
if (userbook != null) {
e.userBookId = userbook.id.value
e.userBook = userbook
}
return e
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/dao/BookTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Book(id: EntityID<UUID>) : UUIDEntity(id) {
var librarythingId by BookTable.librarythingId
val userBooks by UserBook referrersOn UserBookTable.book
var userBookId: UUID? = null
var userBook: UserBook? = null

fun toBookDto(): BookDto =
BookDto(
Expand All @@ -80,6 +81,7 @@ class Book(id: EntityID<UUID>) : UUIDEntity(id) {
tags = this.tags.map { it.toTagDto() },
userBookId = this.userBookId,
series = this.seriesAndOrder.map { it.toSeriesOrderDto() },
userbook = this.userBook?.toUserBookLightWithoutBookDto(),
)

fun toBookUpdateDto(): BookUpdateDto =
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/dao/UserBookTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.bayang.jelu.dao

import io.github.bayang.jelu.dto.UserBookDto
import io.github.bayang.jelu.dto.UserBookLightDto
import io.github.bayang.jelu.dto.UserBookLightWithoutBookDto
import io.github.bayang.jelu.dto.UserBookWithoutEventsAndUserDto
import io.github.bayang.jelu.dto.UserBookWithoutEventsDto
import org.jetbrains.exposed.dao.UUIDEntity
Expand Down Expand Up @@ -80,6 +81,21 @@ class UserBook(id: EntityID<UUID>) : UUIDEntity(id) {
borrowed = this.borrowed,
readingEvents = this.readingEvents.map { it.toReadingEventWithoutUserBookDto() },
)
fun toUserBookLightWithoutBookDto(): UserBookLightWithoutBookDto =
UserBookLightWithoutBookDto(
id = this.id.value,
creationDate = this.creationDate,
modificationDate = this.modificationDate,
owned = this.owned,
toRead = this.toRead,
personalNotes = this.personalNotes,
lastReadingEvent = this.lastReadingEvent,
lastReadingEventDate = this.lastReadingEventDate,
percentRead = this.percentRead,
currentPageNumber = this.currentPageNumber,
borrowed = this.borrowed,
readingEvents = this.readingEvents.map { it.toReadingEventWithoutUserBookDto() },
)
fun toUserBookWthoutEventsAndUserDto(): UserBookWithoutEventsAndUserDto =
UserBookWithoutEventsAndUserDto(
id = this.id.value,
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/io/github/bayang/jelu/dto/BookDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data class BookDto(
val librarythingId: String?,
val language: String?,
val userBookId: UUID?,
val userbook: UserBookLightWithoutBookDto?,
)

data class BookCreateDto(
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/dto/UserBookDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ data class UserBookLightDto(
val currentPageNumber: Int?,
val borrowed: Boolean?,
)
data class UserBookLightWithoutBookDto(
val id: UUID?,
val creationDate: Instant?,
val modificationDate: Instant?,
val readingEvents: List<ReadingEventWithoutUserBookDto>?,
val lastReadingEventDate: Instant?,
val lastReadingEvent: ReadingEventType?,
val personalNotes: String?,
val owned: Boolean?,
val toRead: Boolean?,
val percentRead: Int?,
val currentPageNumber: Int?,
val borrowed: Boolean?,
)
data class UserBookWithoutEventsAndUserDto(
val id: UUID?,
val creationDate: Instant?,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/bayang/jelu/service/BookService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ class BookService(
}

@Transactional
fun findTagBooksById(tagId: UUID, user: User, pageable: Pageable, libaryFilter: LibraryFilter): Page<BookDto> {
return bookRepository.findTagBooksById(tagId, user, pageable, libaryFilter).map { book -> book.toBookDto() }
fun findTagBooksById(tagId: UUID, user: User, pageable: Pageable, libaryFilter: LibraryFilter, eventTypes: List<ReadingEventType>?): Page<BookDto> {
return bookRepository.findTagBooksById(tagId, user, pageable, libaryFilter, eventTypes).map { book -> book.toBookDto() }
}

@Transactional
Expand Down

0 comments on commit e171117

Please sign in to comment.