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

View File

@ -19,13 +19,13 @@
</div> </div>
</div> </div>
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-6 text-center"> <form class="col-6 text-center" action="/gallery">
<label for="imageSearch"></label> <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"> <div class="d-grid col-4 mx-auto">
<button type="button" class="btn btn-primary mt-1">Search</button> <button class="btn btn-primary mt-1" type="submit">Search</button>
</div> </div>
</div> </form>
</div> </div>
<div class="row mt-3"> <div class="row mt-3">
<div class="col"> <div class="col">