Added more proper tag searching

This commit is contained in:
neviyn 2021-04-29 22:26:15 +01:00
parent 7ab9409ef0
commit 0e959d60d4
2 changed files with 18 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package uk.co.neviyn.booru
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.crypto.codec.Hex
@ -39,18 +40,23 @@ class ImageController
@GetMapping
fun getGalleryPage(
@RequestParam(defaultValue = "1") pageNumber: Int,
@RequestParam tags: List<String>?,
@RequestParam tags: String?,
model: Model
): String {
val page = PageRequest.of(pageNumber - 1, 20)
if (tags != null) {
val tagData = tags.mapNotNull { tagRepository.findByTagIs(it) }
val images = imageRepository.findByTags(tagData, page)
model.addAttribute("images", images)
} else { // If no tag data supplied, just return all images
val images = imageRepository.findAll(page)
model.addAttribute("images", images)
val images : Page<Image>? = when (tags) {
null -> imageRepository.findAll(page) // No tags were given to search for
else -> {
val tagData = tags.split(" ") // Tags arrive separated by spaces, tags themselves cannot contain spaces
.distinct() // Eliminate duplicates
.mapNotNull { tagRepository.findByTagIs(it) } // Try to get actual tag objects
when {
tagData.isEmpty() -> null // No tags existed with the specified search terms
else -> imageRepository.findByTags(tagData, page)
}
}
}
model.addAttribute("images", images)
return "gallery"
}

View File

@ -19,13 +19,13 @@
</div>
</div>
<div class="row justify-content-center">
<div class="col-6 text-center">
<form class="col-6 text-center" action="/gallery">
<label for="imageSearch"></label>
<input type="search" class="form-control" id="imageSearch" placeholder="Ex: blue_eyes smile" pattern="[a-zA-Z0-9\s]*">
<input class="form-control" id="imageSearch" pattern="[a-zA-Z0-9\s]*" placeholder="Ex: blue_eyes smile" type="search" name="tags">
<div class="d-grid col-4 mx-auto">
<button type="button" class="btn btn-primary mt-1">Search</button>
</div>
<button class="btn btn-primary mt-1" type="submit">Search</button>
</div>
</form>
</div>
<div class="row mt-3">
<div class="col">