콘텐츠 카테고리

- 카테고리 수정 API 추가
This commit is contained in:
Klaus 2024-02-01 22:49:40 +09:00
parent a2e6d09ee8
commit b8b387c33d
4 changed files with 55 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
package kr.co.vividnext.sodalive.content.category
data class ModifyCategoryRequest(
val categoryId: Long,
val title: String?,
val addContentIdList: List<Long>,
val removeContentIdList: List<Long>
)