From c4c44ad4da91c90c3b718930d86eeeb9cc7477bd Mon Sep 17 00:00:00 2001 From: neviyn Date: Sun, 9 May 2021 20:25:19 +0100 Subject: [PATCH] Added page for viewing a single image, tracking number of images a tag has. --- .../kotlin/uk/co/neviyn/booru/Controller.kt | 11 ++++++ src/main/kotlin/uk/co/neviyn/booru/Entity.kt | 1 + .../kotlin/uk/co/neviyn/booru/Repository.kt | 4 ++ src/main/kotlin/uk/co/neviyn/booru/Request.kt | 5 +++ .../db/migration/V1_0__Database_Init.sql | 3 +- src/main/resources/templates/gallery.html | 5 ++- src/main/resources/templates/single.html | 37 +++++++++++++++++++ 7 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/templates/single.html diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index c353bb4..4aa7498 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -8,6 +8,7 @@ import org.springframework.stereotype.Controller import org.springframework.ui.Model import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.ModelAttribute +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam @@ -36,6 +37,15 @@ class BaseController return "login" } + @GetMapping("/view/{id}") + fun viewSingleImage(@PathVariable id: Long, model: Model) : String { + val image = imageRepository.findById(id).get() + val tagData = image.tags.sortedBy { it.tag } + model.addAttribute("image", image) + model.addAttribute("tags", tagData) + model.addAttribute("title", tagData.joinToString { it.tag }) + return "single" + } } @Controller @@ -137,6 +147,7 @@ class UploadController val user = memberRepository.findByName(userDetails.username)!! val hash: String = generateFileHash(file) val tagData = tags.split(" ").map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(tag = it)) }.toMutableSet() + tagData.map { it.amount = it.amount + 1 } storage.addImageFile(hash, extension, file.bytes) imageRepository.save(Image("$hash.$extension", user, tagData)) return "upload" // TODO: Show success on page diff --git a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt index f8a0ff9..f2f0147 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt @@ -72,6 +72,7 @@ open class Tag( @Column(unique = true) @Pattern(regexp = "[a-zA-Z0-9_]*") // Only allow alphanumeric and underscores open var tag: String = "", + open var amount: Long = 0, @Id @SequenceGenerator(name="tag_id_seq", sequenceName = "tag_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tag_id_seq") diff --git a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt index 804231d..a666fab 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -21,6 +21,10 @@ interface ImageRepository : JpaRepository { fun findByTagsOrderById(tagIDs: List, tagCount: Long, pageable: Pageable): Page fun findAllByTagsContainingOrderById(tag: Tag, pageable: Pageable): Page + + @Suppress("SqlResolve") + @Query(value = "DELETE FROM BOORU.TAG_IMAGE WHERE IMAGE_ID = ?1", nativeQuery = true) + fun deleteTagAssociations(imagesID: Long) } interface TagRepository : CrudRepository { diff --git a/src/main/kotlin/uk/co/neviyn/booru/Request.kt b/src/main/kotlin/uk/co/neviyn/booru/Request.kt index a26d2d9..759a82b 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Request.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Request.kt @@ -9,4 +9,9 @@ data class DisplayUser( val email: String, val password: String, @field:NotBlank val oldPassword: String +) + +data class TagWithCount( + val tagName: String, + val count: Int ) \ No newline at end of file diff --git a/src/main/resources/db/migration/V1_0__Database_Init.sql b/src/main/resources/db/migration/V1_0__Database_Init.sql index 15a82d4..1dce03a 100644 --- a/src/main/resources/db/migration/V1_0__Database_Init.sql +++ b/src/main/resources/db/migration/V1_0__Database_Init.sql @@ -28,7 +28,8 @@ CREATE TABLE booru.role CREATE TABLE booru.tag ( id bigserial NOT NULL PRIMARY KEY, - tag varchar NOT NULL UNIQUE + tag varchar NOT NULL UNIQUE, + amount bigint NOT NULL ); CREATE TABLE booru.tag_image diff --git a/src/main/resources/templates/gallery.html b/src/main/resources/templates/gallery.html index b7f7e8e..3e279a6 100644 --- a/src/main/resources/templates/gallery.html +++ b/src/main/resources/templates/gallery.html @@ -24,8 +24,9 @@
- - + + +
diff --git a/src/main/resources/templates/single.html b/src/main/resources/templates/single.html new file mode 100644 index 0000000..3061fc0 --- /dev/null +++ b/src/main/resources/templates/single.html @@ -0,0 +1,37 @@ + + + + + Image + + +
+
+
+
+ + +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+ + \ No newline at end of file