diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 3ec386b..94634a5 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -38,8 +38,16 @@ class BaseController if (logout != null) model.addAttribute("message", "You have been logged out successfully.") return "login" } +} - @GetMapping("/view/{id}") +@Controller +@RequestMapping("/view/{id}") +class SingleImageViewController +@Autowired constructor( + val imageRepository: ImageRepository, + val tagRepository: TagRepository +){ + @GetMapping fun viewSingleImage(@PathVariable id: Long, model: Model, @AuthenticationPrincipal userDetails: CustomUserDetails?) : String { val image = imageRepository.findById(id).get() val tagData = image.tags.sortedBy { it.tag } @@ -49,6 +57,21 @@ class BaseController model.addAttribute("isUploader", (userDetails != null && (userDetails.authorities.any { it.authority == "ADMIN" } || userDetails.getId() == image.uploader.id))) return "single" } + + @PostMapping("/removetag") + @Transactional + fun removeTagFromImage(@PathVariable id: Long, @RequestParam("tagId") tagID: Long, model: Model, @AuthenticationPrincipal userDetails: CustomUserDetails?) : String { + val image = imageRepository.findById(id).get() + if(userDetails == null || userDetails.getId() != image.uploader.id || !userDetails.authorities.any { it.authority == "ADMIN" }) return "redirect:/view/$id" + val targetTag = image.tags.find { it.id == tagID } + if(targetTag != null){ + targetTag.amount -= 1 + tagRepository.save(targetTag) + image.tags.remove(targetTag) + imageRepository.save(image) + } + return "redirect:/view/$id" + } } @Controller diff --git a/src/main/resources/templates/single.html b/src/main/resources/templates/single.html index 09b1ede..45f48c6 100644 --- a/src/main/resources/templates/single.html +++ b/src/main/resources/templates/single.html @@ -14,7 +14,12 @@
Uploaded by: