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) { + for (index in ids.indices) { + val tag = repository.findByIdOrNull(ids[index]) + + if (tag != null) { + tag.orders = index + 1 + } + } + } + fun getTags(member: Member): List { return repository.getTags(role = member.role, isAdult = member.auth != null, cloudFrontHost = cloudFrontHost) }