From 38f6e8d870cf7b7319e7dd1861e79f131e624c83 Mon Sep 17 00:00:00 2001
From: Klaus <klaus@vividnext.co.kr>
Date: Sun, 6 Aug 2023 22:42:50 +0900
Subject: [PATCH] =?UTF-8?q?=EA=B4=80=EB=A6=AC=EC=9E=90=20-=20=EB=9D=BC?=
 =?UTF-8?q?=EC=9D=B4=EB=B8=8C=20=EA=B4=80=EC=8B=AC=EC=82=AC=20=EB=93=B1?=
 =?UTF-8?q?=EB=A1=9D/=EC=88=98=EC=A0=95/=EC=82=AD=EC=A0=9C=20API?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../sodalive/live/tag/LiveTagController.kt    | 23 +++++++++
 .../sodalive/live/tag/LiveTagService.kt       | 47 +++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagController.kt b/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagController.kt
index d72d1d6..0e29647 100644
--- a/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagController.kt
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagController.kt
@@ -1,12 +1,17 @@
 package kr.co.vividnext.sodalive.live.tag
 
+import kr.co.vividnext.sodalive.admin.member.tag.UpdateTagOrdersRequest
 import kr.co.vividnext.sodalive.common.ApiResponse
 import kr.co.vividnext.sodalive.common.SodaException
 import kr.co.vividnext.sodalive.member.Member
 import org.springframework.security.access.prepost.PreAuthorize
 import org.springframework.security.core.annotation.AuthenticationPrincipal
+import org.springframework.web.bind.annotation.DeleteMapping
 import org.springframework.web.bind.annotation.GetMapping
+import org.springframework.web.bind.annotation.PathVariable
 import org.springframework.web.bind.annotation.PostMapping
+import org.springframework.web.bind.annotation.PutMapping
+import org.springframework.web.bind.annotation.RequestBody
 import org.springframework.web.bind.annotation.RequestMapping
 import org.springframework.web.bind.annotation.RequestPart
 import org.springframework.web.bind.annotation.RestController
@@ -22,6 +27,24 @@ class LiveTagController(private val service: LiveTagService) {
         @RequestPart("request") requestString: String
     ) = ApiResponse.ok(service.enrollmentLiveTag(image, requestString), "등록되었습니다.")
 
+    @DeleteMapping("/{id}")
+    @PreAuthorize("hasRole('ADMIN')")
+    fun deleteSudaTag(@PathVariable id: Long) = ApiResponse.ok(service.deleteTag(id), "삭제되었습니다.")
+
+    @PutMapping("/{id}")
+    @PreAuthorize("hasRole('ADMIN')")
+    fun modifySudaTag(
+        @PathVariable id: Long,
+        @RequestPart("image") image: MultipartFile?,
+        @RequestPart("request") requestString: String
+    ) = ApiResponse.ok(service.modifyTag(id, image, requestString), "수정되었습니다.")
+
+    @PutMapping("/orders")
+    @PreAuthorize("hasRole('ADMIN')")
+    fun updateTagOrders(
+        @RequestBody request: UpdateTagOrdersRequest
+    ) = ApiResponse.ok(service.updateTagOrders(request.ids), "수정되었습니다.")
+
     @GetMapping
     fun getTags(
         @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagService.kt
index e168af4..6237a01 100644
--- a/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagService.kt
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/live/tag/LiveTagService.kt
@@ -7,7 +7,9 @@ import kr.co.vividnext.sodalive.common.SodaException
 import kr.co.vividnext.sodalive.member.Member
 import kr.co.vividnext.sodalive.utils.generateFileName
 import org.springframework.beans.factory.annotation.Value
+import org.springframework.data.repository.findByIdOrNull
 import org.springframework.stereotype.Service
+import org.springframework.transaction.annotation.Transactional
 import org.springframework.web.multipart.MultipartFile
 
 @Service
@@ -22,6 +24,7 @@ class LiveTagService(
     @Value("\${cloud.aws.cloud-front.host}")
     private val cloudFrontHost: String
 ) {
+    @Transactional
     fun enrollmentLiveTag(image: MultipartFile, requestString: String) {
         val request = objectMapper.readValue(requestString, CreateLiveTagRequest::class.java)
         tagExistCheck(request)
@@ -42,6 +45,50 @@ class LiveTagService(
         tag.image = tagImagePath
     }
 
+    @Transactional
+    fun deleteTag(id: Long) {
+        val tag = repository.findByIdOrNull(id)
+            ?: throw SodaException("잘못된 요청입니다.")
+
+        tag.tag = "${tag.tag}_deleted"
+        tag.isActive = false
+    }
+
+    @Transactional
+    fun modifyTag(id: Long, image: MultipartFile?, requestString: String) {
+        val tag = repository.findByIdOrNull(id)
+            ?: throw SodaException("잘못된 요청입니다.")
+
+        val request = objectMapper.readValue(requestString, CreateLiveTagRequest::class.java)
+        tag.tag = request.tag
+
+        if (image != null) {
+            val metadata = ObjectMetadata()
+            metadata.contentLength = image.size
+
+            val tagImageFileName = generateFileName(prefix = "${tag.id}-")
+            val tagImagePath = s3Uploader.upload(
+                inputStream = image.inputStream,
+                bucket = coverImageBucket,
+                filePath = "live_cover/${tag.id}/$tagImageFileName",
+                metadata = metadata
+            )
+
+            tag.image = tagImagePath
+        }
+    }
+
+    @Transactional
+    fun updateTagOrders(ids: List<Long>) {
+        for (index in ids.indices) {
+            val tag = repository.findByIdOrNull(ids[index])
+
+            if (tag != null) {
+                tag.orders = index + 1
+            }
+        }
+    }
+
     fun getTags(member: Member): List<GetLiveTagResponse> {
         return repository.getTags(role = member.role, isAdult = member.auth != null, cloudFrontHost = cloudFrontHost)
     }