parent
a2e6d09ee8
commit
b8b387c33d
|
@ -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)
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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!!)
|
||||
|
|
|
@ -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>
|
||||
)
|
Loading…
Reference in New Issue