Skip to content

Commit

Permalink
[feat #180] 미분류 링크 포킷이동 API (#182)
Browse files Browse the repository at this point in the history
* feat : 미분류 링크 이동(포킷으로) API

* feat : 미분류 링크 이동 유스케이스 구현

* feat : 컨텐츠 id 목록으로 조회 쿼리, 컨텐츠들의 카테고리 ID 수정 쿼리 구
  • Loading branch information
dlswns2480 authored Nov 17, 2024
1 parent 411e347 commit d2e13d8
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,16 @@ class ContentController(
.wrapUnit()
}

@PatchMapping
@Operation(summary = "미분류 링크 포킷으로 이동 API")
fun categorizeContents(
@AuthenticationPrincipal user: PrincipalUser,
@RequestBody request: CategorizeRequest
): ResponseEntity<Unit> {
return contentUseCase.categorize(user.id, request.toDto())
.wrapUnit()
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pokit.content.dto.request

import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotNull

data class CategorizeRequest(
val contentIds: List<Long>,
@Schema(description = "이동시키려는 카테고리 ID")
@NotNull(message = "카테고리 ID는 필수값입니다.")
val categoryId: Long
)

internal fun CategorizeRequest.toDto() = CategorizeCommand(
contentIds = this.contentIds,
categoryId = this.categoryId
)
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ class ContentAdapter(
contentRepository.deleteByContentIds(contentIds)
}

override fun loadAllByUserIdAndContentIds(userId: Long, contentIds: List<Long>): List<Content> {
return contentRepository.findAllByUserIdAndContentIds(userId, contentIds)
.map { it.toDomain() }
}

override fun updateCategoryId(contents: List<Content>, categoryId: Long) {
val contentIds = contents.map { it.id }
contentRepository.updateCategoryId(contentIds, categoryId)
}

override fun loadByContentIds(contentIds: List<Long>): List<Content> =
contentRepository.findByIdIn(contentIds)
.map { it.toDomain() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,29 @@ interface ContentRepository : JpaRepository<ContentEntity, Long>, ContentJdbcRep
"""
)
fun deleteByContentIds(@Param("contentIds") contentIds: List<Long>)

@Query(
"""
select c from ContentEntity c
join CategoryEntity ca on ca.id = c.categoryId
join UserEntity u on u.id = ca.userId
where u.id = :userId and c.id in :contentIds and c.deleted = false
"""
)
fun findAllByUserIdAndContentIds(
@Param("userId") userId: Long,
@Param("contentIds") contentIds: List<Long>
): List<ContentEntity>

@Modifying(clearAutomatically = true)
@Query(
"""
update ContentEntity c set c.categoryId = :categoryId
where c.id in :contentIds
"""
)
fun updateCategoryId(
@Param("contentIds") contentIds: List<Long>,
@Param("categoryId") categoryId: Long
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pokit.content.port.`in`

import com.pokit.content.dto.request.CategorizeCommand
import com.pokit.content.dto.request.ContentCommand
import com.pokit.content.dto.request.ContentSearchCondition
import com.pokit.content.dto.response.*
Expand Down Expand Up @@ -42,4 +43,6 @@ interface ContentUseCase {
fun getBookmarkCount(userId: Long): Int

fun deleteUncategorized(userId: Long, contentIds: List<Long>)

fun categorize(userId: Long, command: CategorizeCommand)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ interface ContentPort {

fun loadByContentIdsWithUser(contetIds: List<Long>): List<ContentWithUser>
fun deleteAllByIds(contentIds: List<Long>)

fun loadAllByUserIdAndContentIds(userId: Long, contentIds: List<Long>): List<Content>

fun updateCategoryId(contents: List<Content>, categoryId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.pokit.category.port.service.loadCategoryOrThrow
import com.pokit.common.exception.AlreadyExistsException
import com.pokit.common.exception.ClientValidationException
import com.pokit.common.exception.NotFoundCustomException
import com.pokit.content.dto.request.CategorizeCommand
import com.pokit.content.dto.request.ContentCommand
import com.pokit.content.dto.request.ContentSearchCondition
import com.pokit.content.dto.request.toDomain
Expand Down Expand Up @@ -181,6 +182,13 @@ class ContentService(
contentPort.deleteAllByIds(contentIds)
}

@Transactional
override fun categorize(userId: Long, command: CategorizeCommand) {
val category = verifyCategory(command.categoryId, userId)
val contents = contentPort.loadAllByUserIdAndContentIds(userId, command.contentIds)
contentPort.updateCategoryId(contents, category.categoryId)
}

private fun verifyContent(userId: Long, contentId: Long): Content {
return contentPort.loadByUserIdAndId(userId, contentId)
?: throw NotFoundCustomException(ContentErrorCode.NOT_FOUND_CONTENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ data class ContentCommand(
val thumbNail: String?
)

data class CategorizeCommand(
val contentIds: List<Long>,
val categoryId: Long
)

fun ContentCommand.toDomain() = Content(
categoryId = this.categoryId,
data = this.data,
Expand Down

0 comments on commit d2e13d8

Please sign in to comment.