Skip to content

Commit

Permalink
feat: search by authors and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Feb 25, 2022
1 parent ea8ab7c commit 7be74ff
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
22 changes: 18 additions & 4 deletions src/jelu-ui/src/components/SearchResultsDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const search = () => {
}
dataService.findBooks(query.value.get('title'),
query.value.get('isbn10'), query.value.get('isbn13'),
query.value.get('series'),
query.value.get('series'), arrayParam(query.value.get('authors')),
arrayParam(query.value.get('tags')),
pageAsNumber.value - 1, perPage.value, sortQuery.value, libraryFilter.value)
.then(res => {
console.log(res)
Expand All @@ -59,6 +60,13 @@ const search = () => {
)
}
const arrayParam = (input: string|undefined) => {
if (input != null) {
return input.split(",")
}
return input
}
watch([page, sortQuery, libraryFilter], (newVal, oldVal) => {
console.log(page.value)
console.log(newVal + " " + oldVal)
Expand Down Expand Up @@ -133,7 +141,7 @@ if (props.query != null && StringUtils.isNotBlank(props.query)) {
</script>

<template>
<sort-filter-bar-vue
<sort-filter-bar-vue
:open="open"
:order="sortOrder"
class="sort-filter-bar"
Expand Down Expand Up @@ -251,6 +259,12 @@ if (props.query != null && StringUtils.isNotBlank(props.query)) {
<option value="title">
Title
</option>
<option value="authors">
Authors
</option>
<option value="tags">
Tags
</option>
<option value="isbn10">
Isbn10
</option>
Expand All @@ -266,14 +280,14 @@ if (props.query != null && StringUtils.isNotBlank(props.query)) {
type="text"
/>
<o-button
v-tooltip="'Add to query params'"
v-tooltip="'Add to query'"
variant="warning"
icon-pack="mdi"
icon-right="magnify-plus-outline"
@click="addToQuery"
/>
<o-button
v-tooltip="'Search selected query params'"
v-tooltip="'Search selected query'"
variant="success"
icon-pack="mdi"
icon-right="magnify"
Expand Down
9 changes: 7 additions & 2 deletions src/jelu-ui/src/services/DataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class DataService {
}

findBooks = async (title?:string, isbn10?: string, isbn13?: string,
series?: string, page?: number, size?: number, sort?: string,
series?: string, authors?: Array<string>, tags?: Array<string>,page?: number, size?: number, sort?: string,
libraryFilter?: LibraryFilter) => {
try {
const response = await this.apiClient.get<Page<Book>>(`${this.API_BOOK}`, {
Expand All @@ -507,11 +507,16 @@ class DataService {
title: title,
isbn13: isbn13,
series: series,
authors: authors,
tags: tags,
page: page,
size: size,
sort: sort,
libraryFilter: libraryFilter
}
},
paramsSerializer: function(params) {
return qs.stringify(params, {arrayFormat: 'comma'})
},
});
console.log("called find books")
console.log(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class BooksController(
@RequestParam(name = "isbn10", required = false) isbn10: String?,
@RequestParam(name = "isbn13", required = false) isbn13: String?,
@RequestParam(name = "series", required = false) series: String?,
@RequestParam(name = "authors", required = false) authors: List<String>?,
@RequestParam(name = "tags", required = false) tags: List<String>?,
@RequestParam(name = "libraryFilter", required = false) libraryFilter: LibraryFilter?,
@PageableDefault(page = 0, size = 20, direction = Sort.Direction.ASC, sort = ["title"]) pageable: Pageable,
principal: Authentication
): Page<BookDto> {
return repository.findAll(title, isbn10, isbn13, series, pageable, (principal.principal as JeluUser).user, libraryFilter ?: LibraryFilter.ANY)
return repository.findAll(title, isbn10, isbn13, series, authors, tags, pageable, (principal.principal as JeluUser).user, libraryFilter ?: LibraryFilter.ANY)
}

@GetMapping(path = ["/books/{id}"])
Expand Down
33 changes: 32 additions & 1 deletion src/main/kotlin/io/github/bayang/jelu/dao/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.orWhere
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.springframework.data.domain.Page
Expand Down Expand Up @@ -58,17 +59,25 @@ fun parseSorts(sort: Sort, defaultSort: Pair<Expression<*>, SortOrder>, vararg t
return orders.toTypedArray()
}

fun formatLike(input: String): String {
return "%$input%"
}

@Repository
class BookRepository(
val readingEventRepository: ReadingEventRepository
) {

fun findAll(title: String?, isbn10: String?, isbn13: String?, series: String?, pageable: Pageable, user: User, filter: LibraryFilter = LibraryFilter.ANY): Page<Book> {
fun findAll(title: String?, isbn10: String?, isbn13: String?, series: String?, authors: List<String>?, tags: List<String>?, pageable: Pageable, user: User, filter: LibraryFilter = LibraryFilter.ANY): Page<Book> {
val booksWithSameIdAndUserHasUserbook = BookTable.join(UserBookTable, JoinType.LEFT)
.slice(BookTable.id)
.select { UserBookTable.book eq BookTable.id and (UserBookTable.user eq user.id) }
.withDistinct()
val query = BookTable.join(UserBookTable, JoinType.LEFT, onColumn = UserBookTable.book, otherColumn = BookTable.id)
.join(BookAuthors, JoinType.LEFT, onColumn = BookTable.id, otherColumn = BookAuthors.book)
.join(AuthorTable, JoinType.LEFT, onColumn = AuthorTable.id, otherColumn = BookAuthors.author)
.join(BookTags, JoinType.LEFT, onColumn = BookTable.id, otherColumn = BookTags.book)
.join(TagTable, JoinType.LEFT, onColumn = TagTable.id, otherColumn = BookTags.tag)
.slice(BookTable.columns)
.selectAll()
.withDistinct()
Expand All @@ -85,6 +94,28 @@ class BookRepository(
series?.let {
query.andWhere { BookTable.series like "%$series%" }
}
if (authors != null && authors.isNotEmpty()) {
var first = true
authors.forEach { author: String ->
if (first) {
first = false
query.andWhere { AuthorTable.name like(formatLike(author)) }
} else {
query.orWhere { AuthorTable.name like(formatLike(author)) }
}
}
}
if (tags != null && tags.isNotEmpty()) {
var first = true
tags.forEach { tag: String ->
if (first) {
first = false
query.andWhere { TagTable.name like (formatLike(tag)) }
} else {
query.orWhere { TagTable.name like (formatLike(tag)) }
}
}
}
if (filter == LibraryFilter.ONLY_USER_BOOKS) {
// only books where user has an userbook
query.andWhere { UserBookTable.user eq user.id }
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/io/github/bayang/jelu/service/BookService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ class BookService(
isbn10: String?,
isbn13: String?,
series: String?,
authors: List<String>?,
tags: List<String>?,
pageable: Pageable,
user: User,
libraryFilter: LibraryFilter
): Page<BookDto> =
bookRepository.findAll(title, isbn10, isbn13, series, pageable, user, libraryFilter).map { it.toBookDto() }
bookRepository.findAll(title, isbn10, isbn13, series, authors, tags, pageable, user, libraryFilter).map { it.toBookDto() }

@Transactional
fun findAllAuthors(name: String?, pageable: Pageable): Page<AuthorDto> = bookRepository.findAllAuthors(name, pageable).map { it.toAuthorDto() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ class CsvImportService(
}
val readStatusEnum: ReadingEventType? = readingStatus(readStatusFromShelves)
book.tags = tags
// val booksPage: Page<BookWithUserBookDto> = bookService.findAll(null, importEntity.isbn10, importEntity.isbn13, null, 0, 20, userEntity)
val booksPage: Page<BookDto> = bookService.findAll(null, importEntity.isbn10, importEntity.isbn13, null, Pageable.ofSize(20), userEntity, LibraryFilter.ANY)
val booksPage: Page<BookDto> = bookService.findAll(null, importEntity.isbn10, importEntity.isbn13, null, null, null, Pageable.ofSize(20), userEntity, LibraryFilter.ANY)
// first case : the book we try to import from csv already exists in DB,
// try to see if user already has it attached to his account (and only update userbook), or create new userbook if not
val savedUserBook: UserBookLightDto = if (! booksPage.isEmpty) {
Expand Down

0 comments on commit 7be74ff

Please sign in to comment.