From 0e959d60d45844eae03a224ba0eb75d6778ce84f Mon Sep 17 00:00:00 2001 From: neviyn Date: Thu, 29 Apr 2021 22:26:15 +0100 Subject: [PATCH] Added more proper tag searching --- .../kotlin/uk/co/neviyn/booru/Controller.kt | 22 ++++++++++++------- src/main/resources/templates/landing.html | 8 +++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 933b6fd..28fc4c8 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -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?, + @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? = 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" } diff --git a/src/main/resources/templates/landing.html b/src/main/resources/templates/landing.html index 1f43093..1404de3 100644 --- a/src/main/resources/templates/landing.html +++ b/src/main/resources/templates/landing.html @@ -19,13 +19,13 @@
-
+
- +
- +
-
+