Skip to content

Commit

Permalink
Merge pull request #3185 from mikiher/genre-search
Browse files Browse the repository at this point in the history
Adds genres to gloabl search
  • Loading branch information
advplyr authored Jul 21, 2024
2 parents f746e24 + a191dab commit 604ae08
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 78 deletions.
8 changes: 6 additions & 2 deletions client/components/cards/AuthorSearchCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</div>
<div class="flex-grow px-2 authorSearchCardContent h-full">
<p class="truncate text-sm">{{ name }}</p>
<p class="text-xs text-gray-400">{{ $getString('LabelXBooks', [numBooks]) }}</p>
</div>
</div>
</template>
Expand All @@ -23,6 +24,9 @@ export default {
computed: {
name() {
return this.author.name
},
numBooks() {
return this.author.numBooks
}
},
methods: {},
Expand All @@ -33,9 +37,9 @@ export default {
<style>
.authorSearchCardContent {
width: calc(100% - 80px);
height: 40px;
height: 44px;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
</style>
36 changes: 36 additions & 0 deletions client/components/cards/GenreSearchCard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<div class="flex h-full px-1 overflow-hidden">
<div class="w-10 h-10 flex items-center justify-center">
<span class="material-symbols text-2xl text-gray-200">category</span>
</div>
<div class="flex-grow px-2 tagSearchCardContent h-full">
<p class="truncate text-sm">{{ genre }}</p>
<p class="text-xs text-gray-400">{{ $getString('LabelXItems', [numItems]) }}</p>
</div>
</div>
</template>

<script>
export default {
props: {
genre: String,
numItems: Number
},
data() {
return {}
},
computed: {},
methods: {},
mounted() {}
}
</script>

<style>
.tagSearchCardContent {
width: calc(100% - 40px);
height: 44px;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
8 changes: 5 additions & 3 deletions client/components/cards/NarratorSearchCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
</div>
<div class="flex-grow px-2 narratorSearchCardContent h-full">
<p class="truncate text-sm">{{ narrator }}</p>
<p class="text-xs text-gray-400">{{ $getString('LabelXBooks', [numBooks]) }}</p>
</div>
</div>
</template>

<script>
export default {
props: {
narrator: String
narrator: String,
numBooks: Number
},
data() {
return {}
Expand All @@ -26,9 +28,9 @@ export default {
<style scoped>
.narratorSearchCardContent {
width: calc(100% - 40px);
height: 40px;
height: 44px;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
</style>
8 changes: 5 additions & 3 deletions client/components/cards/TagSearchCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
</div>
<div class="flex-grow px-2 tagSearchCardContent h-full">
<p class="truncate text-sm">{{ tag }}</p>
<p class="text-xs text-gray-400">{{ $getString('LabelXItems', [numItems]) }}</p>
</div>
</div>
</template>

<script>
export default {
props: {
tag: String
tag: String,
numItems: Number
},
data() {
return {}
Expand All @@ -26,9 +28,9 @@ export default {
<style>
.tagSearchCardContent {
width: calc(100% - 40px);
height: 40px;
height: 44px;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
</style>
26 changes: 19 additions & 7 deletions client/components/controls/GlobalSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@

<p v-if="tagResults.length" class="uppercase text-xs text-gray-400 mb-1 mt-3 px-1 font-semibold">{{ $strings.LabelTags }}</p>
<template v-for="item in tagResults">
<li :key="item.name" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
<li :key="`tag.${item.name}`" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
<nuxt-link :to="`/library/${currentLibraryId}/bookshelf?filter=tags.${$encode(item.name)}`">
<cards-tag-search-card :tag="item.name" />
<cards-tag-search-card :tag="item.name" :num-items="item.numItems" />
</nuxt-link>
</li>
</template>

<p v-if="genreResults.length" class="uppercase text-xs text-gray-400 mb-1 mt-3 px-1 font-semibold">{{ $strings.LabelGenres }}</p>
<template v-for="item in genreResults">
<li :key="`genre.${item.name}`" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
<nuxt-link :to="`/library/${currentLibraryId}/bookshelf?filter=genres.${$encode(item.name)}`">
<cards-genre-search-card :genre="item.name" :num-items="item.numItems" />
</nuxt-link>
</li>
</template>
Expand All @@ -70,7 +79,7 @@
<template v-for="narrator in narratorResults">
<li :key="narrator.name" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
<nuxt-link :to="`/library/${currentLibraryId}/bookshelf?filter=narrators.${$encode(narrator.name)}`">
<cards-narrator-search-card :narrator="narrator.name" />
<cards-narrator-search-card :narrator="narrator.name" :num-books="narrator.numBooks" />
</nuxt-link>
</li>
</template>
Expand All @@ -95,6 +104,7 @@ export default {
authorResults: [],
seriesResults: [],
tagResults: [],
genreResults: [],
narratorResults: [],
searchTimeout: null,
lastSearch: null
Expand All @@ -105,7 +115,7 @@ export default {
return this.$store.state.libraries.currentLibraryId
},
totalResults() {
return this.bookResults.length + this.seriesResults.length + this.authorResults.length + this.tagResults.length + this.podcastResults.length + this.narratorResults.length
return this.bookResults.length + this.seriesResults.length + this.authorResults.length + this.tagResults.length + this.genreResults.length + this.podcastResults.length + this.narratorResults.length
}
},
methods: {
Expand All @@ -116,7 +126,7 @@ export default {
if (!this.search) return
var search = this.search
this.clearResults()
this.$router.push(`/library/${this.currentLibraryId}/search?q=${search}`)
this.$router.push(`/library/${this.currentLibraryId}/search?q=${encodeURIComponent(search)}`)
},
clearResults() {
this.search = null
Expand All @@ -126,6 +136,7 @@ export default {
this.authorResults = []
this.seriesResults = []
this.tagResults = []
this.genreResults = []
this.narratorResults = []
this.showMenu = false
this.isFetching = false
Expand Down Expand Up @@ -155,7 +166,7 @@ export default {
}
this.isFetching = true
const searchResults = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/search?q=${value}&limit=3`).catch((error) => {
const searchResults = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/search?q=${encodeURIComponent(value)}&limit=3`).catch((error) => {
console.error('Search error', error)
return []
})
Expand All @@ -168,6 +179,7 @@ export default {
this.authorResults = searchResults.authors || []
this.seriesResults = searchResults.series || []
this.tagResults = searchResults.tags || []
this.genreResults = searchResults.genres || []
this.narratorResults = searchResults.narrators || []
this.isFetching = false
Expand Down Expand Up @@ -203,4 +215,4 @@ export default {
.globalSearchMenu {
max-height: calc(100vh - 75px);
}
</style>
</style>
7 changes: 4 additions & 3 deletions client/components/ui/LibrariesDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ export default {
if (this.$route.name.startsWith('config')) {
// No need to refresh
} else if (this.$route.name.startsWith('library') && this.$route.name !== 'library-library-series-id') {
const newRoute = this.$route.path.replace(currLibraryId, library.id)
this.$router.push(newRoute)
} else if (this.$route.name === 'library-library-series-id' && library.mediaType === 'book') {
// For series item page redirect to root series page
this.$router.push(`/library/${library.id}/bookshelf/series`)
} else if (this.$route.name === 'library-library-search') {
this.$router.push(this.$route.fullPath.replace(currLibraryId, library.id))
} else if (this.$route.name.startsWith('library')) {
this.$router.push(this.$route.path.replace(currLibraryId, library.id))
} else {
this.$router.push(`/library/${library.id}`)
}
Expand Down
4 changes: 2 additions & 2 deletions client/pages/library/_library/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
if (!library) {
return redirect('/oops?message=Library not found')
}
let results = await app.$axios.$get(`/api/libraries/${libraryId}/search?q=${query.q}`).catch((error) => {
let results = await app.$axios.$get(`/api/libraries/${libraryId}/search?q=${encodeURIComponent(query.q)}`).catch((error) => {
console.error('Failed to search library', error)
return null
})
Expand Down Expand Up @@ -55,7 +55,7 @@ export default {
},
methods: {
async search() {
const results = await this.$axios.$get(`/api/libraries/${this.libraryId}/search?q=${this.query}`).catch((error) => {
const results = await this.$axios.$get(`/api/libraries/${this.libraryId}/search?q=${encodeURIComponent(this.query)}`).catch((error) => {
console.error('Failed to search library', error)
return null
})
Expand Down
2 changes: 2 additions & 0 deletions client/strings/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@
"LabelViewQueue": "View player queue",
"LabelVolume": "Volume",
"LabelWeekdaysToRun": "Weekdays to run",
"LabelXBooks": "{0} books",
"LabelXItems": "{0} items",
"LabelYearReviewHide": "Hide Year in Review",
"LabelYearReviewShow": "See Year in Review",
"LabelYourAudiobookDuration": "Your audiobook duration",
Expand Down
21 changes: 20 additions & 1 deletion server/utils/queries/libraryItemsBookFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ module.exports = {

// Search tags
const tagMatches = []
const [tagResults] = await Database.sequelize.query(`SELECT value, count(*) AS numItems FROM books b, libraryItems li, json_each(b.tags) WHERE json_valid(b.tags) AND json_each.value LIKE :query AND b.id = li.mediaId AND li.libraryId = :libraryId GROUP BY value LIMIT :limit OFFSET :offset;`, {
const [tagResults] = await Database.sequelize.query(`SELECT value, count(*) AS numItems FROM books b, libraryItems li, json_each(b.tags) WHERE json_valid(b.tags) AND json_each.value LIKE :query AND b.id = li.mediaId AND li.libraryId = :libraryId GROUP BY value ORDER BY numItems DESC LIMIT :limit OFFSET :offset;`, {
replacements: {
query: `%${query}%`,
libraryId: oldLibrary.id,
Expand All @@ -1095,6 +1095,24 @@ module.exports = {
})
}

// Search genres
const genreMatches = []
const [genreResults] = await Database.sequelize.query(`SELECT value, count(*) AS numItems FROM books b, libraryItems li, json_each(b.genres) WHERE json_valid(b.genres) AND json_each.value LIKE :query AND b.id = li.mediaId AND li.libraryId = :libraryId GROUP BY value ORDER BY numItems DESC LIMIT :limit OFFSET :offset;`, {
replacements: {
query: `%${query}%`,
libraryId: oldLibrary.id,
limit,
offset
},
raw: true
})
for (const row of genreResults) {
genreMatches.push({
name: row.value,
numItems: row.numItems
})
}

// Search series
const allSeries = await Database.seriesModel.findAll({
where: {
Expand Down Expand Up @@ -1140,6 +1158,7 @@ module.exports = {
book: itemMatches,
narrators: narratorMatches,
tags: tagMatches,
genres: genreMatches,
series: seriesMatches,
authors: authorMatches
}
Expand Down
Loading

0 comments on commit 604ae08

Please sign in to comment.