feat(creator-admin): 팬톡 조회와 답글 관리를 추가한다
This commit is contained in:
+102
@@ -0,0 +1,102 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.adapter.`in`.web
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.adapter.`in`.web.CreatorAdminCommentRequestParser
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.adapter.`in`.web.requireCreator
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentPolicy
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentPageResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.application.CreatorAdminFanTalkService
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.dto.CreatorAdminFanTalkResponse
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
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
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v2/creator-admin/comment-management/fantalks")
|
||||
class CreatorAdminFanTalkController(
|
||||
private val service: CreatorAdminFanTalkService,
|
||||
private val requestParser: CreatorAdminCommentRequestParser
|
||||
) {
|
||||
@GetMapping
|
||||
fun list(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@RequestParam(defaultValue = "0") page: Int,
|
||||
@RequestParam(defaultValue = "10") size: Int,
|
||||
@RequestParam(defaultValue = "latest") order: String
|
||||
): ApiResponse<CreatorAdminCommentPageResponse<CreatorAdminFanTalkResponse>> {
|
||||
val creator = requireCreator(member)
|
||||
return ApiResponse.ok(
|
||||
service.list(
|
||||
requireNotNull(creator.id),
|
||||
CreatorAdminCommentPolicy.parsePage(page, size),
|
||||
CreatorAdminCommentPolicy.parseOrder(order)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@DeleteMapping("/{fanTalkId}")
|
||||
fun delete(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@PathVariable fanTalkId: Long
|
||||
): ApiResponse<Unit> {
|
||||
val creator = requireCreator(member)
|
||||
service.delete(requireNotNull(creator.id), CreatorAdminCommentPolicy.requirePositiveId(fanTalkId))
|
||||
return ApiResponse.ok()
|
||||
}
|
||||
|
||||
@PostMapping("/{fanTalkId}/replies", consumes = [MediaType.APPLICATION_JSON_VALUE])
|
||||
fun createReply(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@PathVariable fanTalkId: Long,
|
||||
@RequestBody body: String
|
||||
): ApiResponse<Unit> {
|
||||
val creator = requireCreator(member)
|
||||
service.createReply(
|
||||
creator,
|
||||
CreatorAdminCommentPolicy.requirePositiveId(fanTalkId),
|
||||
requestParser.parseReply(body)
|
||||
)
|
||||
return ApiResponse.ok()
|
||||
}
|
||||
|
||||
@PutMapping("/{fanTalkId}/replies/{replyId}", consumes = [MediaType.APPLICATION_JSON_VALUE])
|
||||
fun updateReply(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@PathVariable fanTalkId: Long,
|
||||
@PathVariable replyId: Long,
|
||||
@RequestBody body: String
|
||||
): ApiResponse<Unit> {
|
||||
val creator = requireCreator(member)
|
||||
service.updateReply(
|
||||
requireNotNull(creator.id),
|
||||
CreatorAdminCommentPolicy.requirePositiveId(fanTalkId),
|
||||
CreatorAdminCommentPolicy.requirePositiveId(replyId),
|
||||
requestParser.parseReply(body)
|
||||
)
|
||||
return ApiResponse.ok()
|
||||
}
|
||||
|
||||
@DeleteMapping("/{fanTalkId}/replies/{replyId}")
|
||||
fun deleteReply(
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
|
||||
@PathVariable fanTalkId: Long,
|
||||
@PathVariable replyId: Long
|
||||
): ApiResponse<Unit> {
|
||||
val creator = requireCreator(member)
|
||||
service.deleteReply(
|
||||
requireNotNull(creator.id),
|
||||
CreatorAdminCommentPolicy.requirePositiveId(fanTalkId),
|
||||
CreatorAdminCommentPolicy.requirePositiveId(replyId)
|
||||
)
|
||||
return ApiResponse.ok()
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.adapter.out.persistence
|
||||
|
||||
import com.querydsl.core.types.dsl.BooleanExpression
|
||||
import com.querydsl.jpa.JPAExpressions
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory
|
||||
import kr.co.vividnext.sodalive.explorer.profile.CreatorCheers
|
||||
import kr.co.vividnext.sodalive.explorer.profile.QCreatorCheers
|
||||
import kr.co.vividnext.sodalive.explorer.profile.QCreatorCheers.creatorCheers
|
||||
import kr.co.vividnext.sodalive.member.QMember.member
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentOrder
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentPage
|
||||
import org.springframework.stereotype.Repository
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.LockModeType
|
||||
|
||||
@Repository
|
||||
class CreatorAdminFanTalkRepository(
|
||||
private val queryFactory: JPAQueryFactory,
|
||||
private val entityManager: EntityManager
|
||||
) {
|
||||
fun countRoots(creatorId: Long): Long = requireNotNull(
|
||||
queryFactory.select(creatorCheers.id.count())
|
||||
.from(creatorCheers)
|
||||
.where(rootCondition(creatorId))
|
||||
.fetchOne()
|
||||
)
|
||||
|
||||
fun findRoots(
|
||||
creatorId: Long,
|
||||
page: CreatorAdminCommentPage,
|
||||
order: CreatorAdminCommentOrder
|
||||
): List<CreatorCheers> {
|
||||
val sorting = when (order) {
|
||||
CreatorAdminCommentOrder.LATEST -> arrayOf(creatorCheers.createdAt.desc(), creatorCheers.id.desc())
|
||||
CreatorAdminCommentOrder.OLDEST -> arrayOf(creatorCheers.createdAt.asc(), creatorCheers.id.asc())
|
||||
}
|
||||
return queryFactory.selectFrom(creatorCheers)
|
||||
.join(creatorCheers.member, member).fetchJoin()
|
||||
.where(rootCondition(creatorId))
|
||||
.orderBy(*sorting)
|
||||
.offset(page.offset)
|
||||
.limit(page.size.toLong())
|
||||
.fetch()
|
||||
}
|
||||
|
||||
fun findLatestOwnerReplies(creatorId: Long, rootIds: List<Long>): List<CreatorCheers> {
|
||||
if (rootIds.isEmpty()) return emptyList()
|
||||
val newer = QCreatorCheers("newerCreatorAdminFanTalkReply")
|
||||
return queryFactory.selectFrom(creatorCheers)
|
||||
.join(creatorCheers.member, member).fetchJoin()
|
||||
.where(
|
||||
creatorCheers.parent.id.`in`(rootIds),
|
||||
creatorCheers.creator.id.eq(creatorId),
|
||||
creatorCheers.member.id.eq(creatorId),
|
||||
creatorCheers.isActive.isTrue,
|
||||
JPAExpressions.selectOne().from(newer)
|
||||
.where(
|
||||
newer.parent.id.eq(creatorCheers.parent.id),
|
||||
newer.creator.id.eq(creatorId),
|
||||
newer.member.id.eq(creatorId),
|
||||
newer.isActive.isTrue,
|
||||
newer.createdAt.gt(creatorCheers.createdAt).or(
|
||||
newer.createdAt.eq(creatorCheers.createdAt).and(newer.id.gt(creatorCheers.id))
|
||||
)
|
||||
)
|
||||
.notExists()
|
||||
)
|
||||
.fetch()
|
||||
}
|
||||
|
||||
fun hasActiveOwnerReplyForUpdate(creatorId: Long, rootId: Long): Boolean = queryFactory.selectFrom(creatorCheers)
|
||||
.where(
|
||||
creatorCheers.parent.id.eq(rootId),
|
||||
creatorCheers.creator.id.eq(creatorId),
|
||||
creatorCheers.member.id.eq(creatorId),
|
||||
creatorCheers.isActive.isTrue
|
||||
)
|
||||
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
|
||||
.fetchFirst() != null
|
||||
|
||||
fun lockReply(rootId: Long, replyId: Long): CreatorCheers? = queryFactory.selectFrom(creatorCheers)
|
||||
.where(creatorCheers.id.eq(replyId), creatorCheers.parent.id.eq(rootId))
|
||||
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
|
||||
.fetchOne()
|
||||
?.also { entityManager.refresh(it, LockModeType.PESSIMISTIC_WRITE) }
|
||||
|
||||
private fun rootCondition(creatorId: Long): BooleanExpression = creatorCheers.creator.id.eq(creatorId)
|
||||
.and(creatorCheers.parent.isNull)
|
||||
.and(creatorCheers.isActive.isTrue)
|
||||
.and(creatorCheers.member.id.ne(creatorId))
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.application
|
||||
|
||||
import kr.co.vividnext.sodalive.explorer.profile.CreatorCheers
|
||||
import kr.co.vividnext.sodalive.explorer.profile.CreatorCheersRepository
|
||||
import kr.co.vividnext.sodalive.extensions.removeDeletedNicknamePrefix
|
||||
import kr.co.vividnext.sodalive.extensions.toUtcIso
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.v2.commentthread.port.out.CommentThreadLockPort
|
||||
import kr.co.vividnext.sodalive.v2.common.domain.toCdnUrl
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentError
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentException
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentOrder
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.domain.CreatorAdminCommentPage
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentPageResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentReplyRequest
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentReplyResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentWriterResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.adapter.out.persistence.CreatorAdminFanTalkRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.dto.CreatorAdminFanTalkResponse
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
|
||||
@Service
|
||||
class CreatorAdminFanTalkService(
|
||||
private val repository: CreatorAdminFanTalkRepository,
|
||||
private val creatorCheersRepository: CreatorCheersRepository,
|
||||
private val threadLock: CommentThreadLockPort,
|
||||
@Value("\${cloud.aws.cloud-front.host}") private val cloudFrontHost: String
|
||||
) {
|
||||
@Transactional(readOnly = true)
|
||||
fun list(
|
||||
creatorId: Long,
|
||||
page: CreatorAdminCommentPage,
|
||||
order: CreatorAdminCommentOrder
|
||||
): CreatorAdminCommentPageResponse<CreatorAdminFanTalkResponse> {
|
||||
val roots = repository.findRoots(creatorId, page, order)
|
||||
val replies = repository.findLatestOwnerReplies(creatorId, roots.map { requireNotNull(it.id) })
|
||||
.associate { reply ->
|
||||
requireNotNull(reply.parent).id to CreatorAdminCommentReplyResponse(
|
||||
replyId = requireNotNull(reply.id),
|
||||
writer = writer(requireNotNull(reply.member)),
|
||||
content = reply.cheers,
|
||||
createdAtUtc = requireNotNull(reply.createdAt).toUtcIso(),
|
||||
isSecret = false
|
||||
)
|
||||
}
|
||||
return CreatorAdminCommentPageResponse(
|
||||
items = roots.map { root ->
|
||||
CreatorAdminFanTalkResponse(
|
||||
fanTalkId = requireNotNull(root.id),
|
||||
writer = writer(requireNotNull(root.member)),
|
||||
content = root.cheers,
|
||||
createdAtUtc = requireNotNull(root.createdAt).toUtcIso(),
|
||||
creatorReply = replies[root.id]
|
||||
)
|
||||
},
|
||||
totalCount = repository.countRoots(creatorId),
|
||||
page = page.page,
|
||||
size = page.size
|
||||
)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun delete(creatorId: Long, fanTalkId: Long) {
|
||||
lockOwnedRoot(creatorId, fanTalkId).isActive = false
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun createReply(creator: Member, fanTalkId: Long, request: CreatorAdminCommentReplyRequest) {
|
||||
val creatorId = requireNotNull(creator.id)
|
||||
val root = lockOwnedRoot(creatorId, fanTalkId)
|
||||
if (!root.isActive) throw CreatorAdminCommentException(CreatorAdminCommentError.CONFLICT)
|
||||
// A locking read after the root lock sees replies committed after an earlier REPEATABLE READ snapshot.
|
||||
if (repository.hasActiveOwnerReplyForUpdate(creatorId, fanTalkId)) {
|
||||
throw CreatorAdminCommentException(CreatorAdminCommentError.CONFLICT)
|
||||
}
|
||||
creatorCheersRepository.save(
|
||||
CreatorCheers(cheers = request.content, languageCode = null).apply {
|
||||
member = creator
|
||||
this.creator = creator
|
||||
parent = root
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun updateReply(creatorId: Long, fanTalkId: Long, replyId: Long, request: CreatorAdminCommentReplyRequest) {
|
||||
val root = lockOwnedRoot(creatorId, fanTalkId)
|
||||
val reply = lockOwnedReply(creatorId, root, replyId)
|
||||
if (reply.cheers != request.content) reply.cheers = request.content
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun deleteReply(creatorId: Long, fanTalkId: Long, replyId: Long) {
|
||||
val root = lockOwnedRoot(creatorId, fanTalkId)
|
||||
lockOwnedReply(creatorId, root, replyId).isActive = false
|
||||
}
|
||||
|
||||
private fun lockOwnedRoot(creatorId: Long, fanTalkId: Long): CreatorCheers {
|
||||
val root = threadLock.lockFanTalkRoot(fanTalkId)
|
||||
?: throw CreatorAdminCommentException(CreatorAdminCommentError.NOT_FOUND)
|
||||
if (root.creator?.id != creatorId) throw CreatorAdminCommentException(CreatorAdminCommentError.ACCESS_DENIED)
|
||||
if (root.member?.id == creatorId) {
|
||||
throw CreatorAdminCommentException(CreatorAdminCommentError.NOT_FOUND)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
private fun lockOwnedReply(creatorId: Long, root: CreatorCheers, replyId: Long): CreatorCheers {
|
||||
val reply = repository.lockReply(requireNotNull(root.id), replyId)
|
||||
?: throw CreatorAdminCommentException(CreatorAdminCommentError.NOT_FOUND)
|
||||
if (reply.creator?.id != creatorId) {
|
||||
throw CreatorAdminCommentException(CreatorAdminCommentError.NOT_FOUND)
|
||||
}
|
||||
if (reply.member?.id != creatorId) throw CreatorAdminCommentException(CreatorAdminCommentError.ACCESS_DENIED)
|
||||
if (!root.isActive || !reply.isActive) throw CreatorAdminCommentException(CreatorAdminCommentError.NOT_FOUND)
|
||||
return reply
|
||||
}
|
||||
|
||||
private fun writer(member: Member): CreatorAdminCommentWriterResponse = CreatorAdminCommentWriterResponse(
|
||||
memberId = requireNotNull(member.id),
|
||||
nickname = member.nickname.removeDeletedNicknamePrefix(),
|
||||
profileImageUrl = member.profileImage.toCdnUrl(cloudFrontHost) ?: "$cloudFrontHost/profile/default-profile.png"
|
||||
)
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.fantalk.dto
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentReplyResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.admin.commentmanagement.dto.CreatorAdminCommentWriterResponse
|
||||
|
||||
data class CreatorAdminFanTalkResponse(
|
||||
val fanTalkId: Long,
|
||||
val writer: CreatorAdminCommentWriterResponse,
|
||||
val content: String,
|
||||
val createdAtUtc: String,
|
||||
val creatorReply: CreatorAdminCommentReplyResponse?,
|
||||
@get:JsonProperty("isSecret")
|
||||
val isSecret: Boolean = false
|
||||
)
|
||||
Reference in New Issue
Block a user