From b8b387c33dd1b971709c44cab5f9caec498ec03b Mon Sep 17 00:00:00 2001 From: Klaus Date: Thu, 1 Feb 2024 22:49:40 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BD=98=ED=85=90=EC=B8=A0=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC=20-=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EC=88=98=EC=A0=95=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sodalive/content/category/Category.kt | 2 +- .../content/category/CategoryController.kt | 12 +++++++ .../content/category/CategoryService.kt | 34 +++++++++++++++++++ .../content/category/ModifyCategoryRequest.kt | 8 +++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/kr/co/vividnext/sodalive/content/category/ModifyCategoryRequest.kt diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/Category.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/Category.kt index 4d576ae..0b6c50d 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/Category.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/Category.kt @@ -9,7 +9,7 @@ import javax.persistence.ManyToOne @Entity data class Category( - val title: String, + var title: String, var isActive: Boolean = true ) : BaseEntity() { @ManyToOne(fetch = FetchType.LAZY) diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryController.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryController.kt index ac308bc..b3db249 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryController.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryController.kt @@ -9,6 +9,7 @@ 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.RequestParam @@ -28,6 +29,17 @@ class CategoryController(private val service: CategoryService) { ApiResponse.ok(service.createCategory(request = request, member = member)) } + @PutMapping + @PreAuthorize("hasRole('CREATOR')") + fun modifyCategory( + @RequestBody request: ModifyCategoryRequest, + @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member? + ) = run { + if (member == null) throw SodaException("로그인 정보를 확인해주세요.") + + ApiResponse.ok(service.modifyCategory(request = request, member = member)) + } + @DeleteMapping("/{id}") @PreAuthorize("hasRole('CREATOR')") fun deleteCategory( diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryService.kt index 3265f70..01e168e 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/CategoryService.kt @@ -41,6 +41,40 @@ class CategoryService( } } + @Transactional + fun modifyCategory(request: ModifyCategoryRequest, member: Member) { + val category = repository.findByIdAndMemberId(categoryId = request.categoryId, memberId = member.id!!) + ?: throw SodaException("잘못된 요청입니다.") + + if (!request.title.isNullOrBlank()) { + category.title = request.title + } + + for (contentId in request.addContentIdList) { + val content = contentRepository.findByIdAndActive(contentId = contentId) + ?: continue + + val categoryContent = categoryContentRepository.findByContentIdAndCategoryId( + contentId = contentId, + categoryId = category.id!! + ) ?: categoryContentRepository.save( + CategoryContent().apply { + this.content = content + this.category = category + } + ) + categoryContent.isActive = true + } + + for (contentId in request.removeContentIdList) { + val categoryContent = categoryContentRepository.findByContentIdAndCategoryId( + contentId = contentId, + categoryId = category.id!! + ) ?: continue + categoryContent.isActive = false + } + } + @Transactional fun deleteCategory(categoryId: Long, member: Member) { val category = repository.findByIdAndMemberId(categoryId = categoryId, memberId = member.id!!) diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/category/ModifyCategoryRequest.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/ModifyCategoryRequest.kt new file mode 100644 index 0000000..5bd5b37 --- /dev/null +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/category/ModifyCategoryRequest.kt @@ -0,0 +1,8 @@ +package kr.co.vividnext.sodalive.content.category + +data class ModifyCategoryRequest( + val categoryId: Long, + val title: String?, + val addContentIdList: List, + val removeContentIdList: List +)