Can now remove tags from images

This commit is contained in:
neviyn 2021-05-10 20:33:09 +01:00
parent a78bea1167
commit 1bce01e07e
2 changed files with 30 additions and 2 deletions

View File

@ -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

View File

@ -14,7 +14,12 @@
<li th:each="tag : ${tags}">
<a class="text-decoration-none"
th:href="${#mvc.url('IC#getGalleryPage').arg(1, tag.tag).build()}"
th:text="${tag.tag} + ' '">tag</a><span th:text="${tag.amount}"></span></li>
th:text="${tag.tag} + ' '">tag</a><span th:text="${tag.amount}"></span>
<form th:action="${'/view/' + image.id + '/removetag'}" method="post" th:if="${isUploader}">
<input type="hidden" th:value="${tag.id}" name="tagId">
<button type="submit">-</button>
</form>
</li>
</ul>
<p>Uploaded by: <span th:text="${image.uploader.name}"></span></p>
<form th:action="'/upload/d/' + ${image.id}"