콘텐츠 카테고리
- 삭제 API 추가
This commit is contained in:
		| @@ -8,6 +8,7 @@ interface CategoryContentRepository : JpaRepository<CategoryContent, Long>, Cate | |||||||
|  |  | ||||||
| interface CategoryContentQueryRepository { | interface CategoryContentQueryRepository { | ||||||
|     fun findByContentIdAndCategoryId(contentId: Long, categoryId: Long): CategoryContent? |     fun findByContentIdAndCategoryId(contentId: Long, categoryId: Long): CategoryContent? | ||||||
|  |     fun deleteByCategoryId(categoryId: Long) | ||||||
| } | } | ||||||
|  |  | ||||||
| class CategoryContentQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : CategoryContentQueryRepository { | class CategoryContentQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : CategoryContentQueryRepository { | ||||||
| @@ -20,4 +21,12 @@ class CategoryContentQueryRepositoryImpl(private val queryFactory: JPAQueryFacto | |||||||
|             ) |             ) | ||||||
|             .fetchFirst() |             .fetchFirst() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     override fun deleteByCategoryId(categoryId: Long) { | ||||||
|  |         queryFactory | ||||||
|  |             .update(categoryContent) | ||||||
|  |             .set(categoryContent.isActive, false) | ||||||
|  |             .where(categoryContent.category.id.eq(categoryId)) | ||||||
|  |             .execute() | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ import kr.co.vividnext.sodalive.common.SodaException | |||||||
| import kr.co.vividnext.sodalive.member.Member | import kr.co.vividnext.sodalive.member.Member | ||||||
| import org.springframework.security.access.prepost.PreAuthorize | import org.springframework.security.access.prepost.PreAuthorize | ||||||
| import org.springframework.security.core.annotation.AuthenticationPrincipal | import org.springframework.security.core.annotation.AuthenticationPrincipal | ||||||
|  | import org.springframework.web.bind.annotation.DeleteMapping | ||||||
|  | import org.springframework.web.bind.annotation.PathVariable | ||||||
| import org.springframework.web.bind.annotation.PostMapping | import org.springframework.web.bind.annotation.PostMapping | ||||||
| import org.springframework.web.bind.annotation.RequestBody | import org.springframework.web.bind.annotation.RequestBody | ||||||
| import org.springframework.web.bind.annotation.RequestMapping | import org.springframework.web.bind.annotation.RequestMapping | ||||||
| @@ -21,8 +23,17 @@ class CategoryController(private val service: CategoryService) { | |||||||
|     ) = run { |     ) = run { | ||||||
|         if (member == null) throw SodaException("로그인 정보를 확인해주세요.") |         if (member == null) throw SodaException("로그인 정보를 확인해주세요.") | ||||||
|  |  | ||||||
|         ApiResponse.ok( |         ApiResponse.ok(service.createCategory(request = request, member = member)) | ||||||
|             service.createCategory(request = request, member = member) |     } | ||||||
|         ) |  | ||||||
|  |     @DeleteMapping("/{id}") | ||||||
|  |     @PreAuthorize("hasRole('CREATOR')") | ||||||
|  |     fun deleteCategory( | ||||||
|  |         @PathVariable("id") categoryId: Long, | ||||||
|  |         @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member? | ||||||
|  |     ) = run { | ||||||
|  |         if (member == null) throw SodaException("로그인 정보를 확인해주세요.") | ||||||
|  |  | ||||||
|  |         ApiResponse.ok(service.deleteCategory(categoryId = categoryId, member = member)) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -8,6 +8,8 @@ interface CategoryRepository : JpaRepository<Category, Long>, CategoryQueryRepos | |||||||
|  |  | ||||||
| interface CategoryQueryRepository { | interface CategoryQueryRepository { | ||||||
|     fun findByTitleAndMemberId(title: String, memberId: Long): Category? |     fun findByTitleAndMemberId(title: String, memberId: Long): Category? | ||||||
|  |  | ||||||
|  |     fun findByIdAndMemberId(categoryId: Long, memberId: Long): Category? | ||||||
| } | } | ||||||
|  |  | ||||||
| class CategoryQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : CategoryQueryRepository { | class CategoryQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : CategoryQueryRepository { | ||||||
| @@ -20,4 +22,14 @@ class CategoryQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : C | |||||||
|             ) |             ) | ||||||
|             .fetchFirst() |             .fetchFirst() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     override fun findByIdAndMemberId(categoryId: Long, memberId: Long): Category? { | ||||||
|  |         return queryFactory | ||||||
|  |             .selectFrom(category) | ||||||
|  |             .where( | ||||||
|  |                 category.id.eq(categoryId) | ||||||
|  |                     .and(category.member.id.eq(memberId)) | ||||||
|  |             ) | ||||||
|  |             .fetchFirst() | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| package kr.co.vividnext.sodalive.content.category | package kr.co.vividnext.sodalive.content.category | ||||||
|  |  | ||||||
|  | import kr.co.vividnext.sodalive.common.SodaException | ||||||
| import kr.co.vividnext.sodalive.content.AudioContentRepository | import kr.co.vividnext.sodalive.content.AudioContentRepository | ||||||
| import kr.co.vividnext.sodalive.member.Member | import kr.co.vividnext.sodalive.member.Member | ||||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||||
| @@ -37,4 +38,13 @@ class CategoryService( | |||||||
|             categoryContent.isActive = true |             categoryContent.isActive = true | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Transactional | ||||||
|  |     fun deleteCategory(categoryId: Long, member: Member) { | ||||||
|  |         val category = repository.findByIdAndMemberId(categoryId = categoryId, memberId = member.id!!) | ||||||
|  |             ?: throw SodaException("잘못된 요청입니다.") | ||||||
|  |         category.isActive = false | ||||||
|  |  | ||||||
|  |         categoryContentRepository.deleteByCategoryId(categoryId = categoryId) | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user