Added more proper tag searching
This commit is contained in:
parent
7ab9409ef0
commit
0e959d60d4
@ -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"
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
<button class="btn btn-primary mt-1" type="submit">Search</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="row mt-3">
|
||||
<div class="col">
|
||||
|
Loading…
Reference in New Issue
Block a user