콘텐츠 API 추가

This commit is contained in:
2023-08-03 20:36:37 +09:00
parent 5d6eb5da4f
commit 1fe5309fdc
55 changed files with 2740 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package kr.co.vividnext.sodalive.content.comment
import kr.co.vividnext.sodalive.common.BaseEntity
import kr.co.vividnext.sodalive.content.AudioContent
import kr.co.vividnext.sodalive.member.Member
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import javax.persistence.OneToMany
import javax.persistence.Table
@Entity
@Table(name = "content_comment")
data class AudioContentComment(
@Column(columnDefinition = "TEXT", nullable = false)
var comment: String,
@Column(nullable = true)
var donationCan: Int? = null,
var isActive: Boolean = true
) : BaseEntity() {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id", nullable = true)
var parent: AudioContentComment? = null
set(value) {
value?.children?.add(this)
field = value
}
@OneToMany(mappedBy = "parent")
var children: MutableList<AudioContentComment> = mutableListOf()
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
var member: Member? = null
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "content_id", nullable = false)
var audioContent: AudioContent? = null
}

View File

@@ -0,0 +1,80 @@
package kr.co.vividnext.sodalive.content.comment
import kr.co.vividnext.sodalive.common.ApiResponse
import kr.co.vividnext.sodalive.common.SodaException
import kr.co.vividnext.sodalive.member.Member
import org.springframework.data.domain.Pageable
import org.springframework.security.core.annotation.AuthenticationPrincipal
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.RequestParam
import org.springframework.web.bind.annotation.RestController
@RestController
class AudioContentCommentController(private val service: AudioContentCommentService) {
@PostMapping("/audio-content/comment")
fun registerComment(
@RequestBody request: RegisterCommentRequest,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(
service.registerComment(
comment = request.comment,
audioContentId = request.contentId,
parentId = request.parentId,
member = member
)
)
}
@PutMapping("/audio-content/comment")
fun modifyComment(
@RequestBody request: ModifyCommentRequest,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(service.modifyComment(request = request, member = member))
}
@GetMapping("/audio-content/{id}/comment")
fun getCommentList(
@PathVariable("id") audioContentId: Long,
@RequestParam timezone: String,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
pageable: Pageable
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(
service.getCommentList(
audioContentId = audioContentId,
timezone = timezone,
pageable = pageable
)
)
}
@GetMapping("/audio-content/comment/{id}")
fun getCommentReplyList(
@PathVariable("id") commentId: Long,
@RequestParam timezone: String,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?,
pageable: Pageable
): ApiResponse<GetAudioContentCommentListResponse> {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
return ApiResponse.ok(
service.getCommentReplyList(
commentId = commentId,
timezone = timezone,
pageable = pageable
)
)
}
}

View File

@@ -0,0 +1,141 @@
package kr.co.vividnext.sodalive.content.comment
import com.querydsl.jpa.impl.JPAQueryFactory
import kr.co.vividnext.sodalive.content.comment.QAudioContentComment.audioContentComment
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.time.ZoneId
import java.time.format.DateTimeFormatter
@Repository
interface AudioContentCommentRepository : JpaRepository<AudioContentComment, Long>, AudioContentCommentQueryRepository
interface AudioContentCommentQueryRepository {
fun findByContentId(
cloudFrontHost: String,
contentId: Long,
timezone: String,
offset: Long,
limit: Int
): List<GetAudioContentCommentListItem>
fun totalCountCommentByContentId(contentId: Long): Int
fun commentReplyCountByAudioContentCommentId(commentId: Long): Int
fun getAudioContentCommentReplyList(
cloudFrontHost: String,
commentId: Long,
timezone: String,
offset: Long,
limit: Int
): List<GetAudioContentCommentListItem>
}
@Repository
class AudioContentCommentQueryRepositoryImpl(
private val queryFactory: JPAQueryFactory
) : AudioContentCommentQueryRepository {
override fun findByContentId(
cloudFrontHost: String,
contentId: Long,
timezone: String,
offset: Long,
limit: Int
): List<GetAudioContentCommentListItem> {
return queryFactory.selectFrom(audioContentComment)
.where(
audioContentComment.audioContent.id.eq(contentId)
.and(audioContentComment.isActive.isTrue)
.and(audioContentComment.parent.isNull)
)
.offset(offset)
.limit(limit.toLong())
.orderBy(audioContentComment.createdAt.desc())
.fetch()
.asSequence()
.map {
val date = it.createdAt!!
.atZone(ZoneId.of("UTC"))
.withZoneSameInstant(ZoneId.of(timezone))
GetAudioContentCommentListItem(
id = it.id!!,
writerId = it.member!!.id!!,
nickname = it.member!!.nickname,
profileUrl = if (it.member!!.profileImage != null) {
"$cloudFrontHost/${it.member!!.profileImage}"
} else {
"$cloudFrontHost/profile/default-profile.png"
},
comment = it.comment,
donationCoin = it.donationCan ?: 0,
date = date.format(DateTimeFormatter.ofPattern("yyyy.MM.dd E hh:mm a")),
replyCount = commentReplyCountByAudioContentCommentId(it.id!!)
)
}
.toList()
}
override fun totalCountCommentByContentId(contentId: Long): Int {
return queryFactory.select(audioContentComment.id)
.from(audioContentComment)
.where(
audioContentComment.audioContent.id.eq(contentId)
.and(audioContentComment.isActive.isTrue)
)
.fetch()
.size
}
override fun commentReplyCountByAudioContentCommentId(commentId: Long): Int {
return queryFactory.select(audioContentComment.id)
.from(audioContentComment)
.where(
audioContentComment.parent.isNotNull
.and(audioContentComment.parent.id.eq(commentId))
.and(audioContentComment.isActive.isTrue)
)
.fetch()
.size
}
override fun getAudioContentCommentReplyList(
cloudFrontHost: String,
commentId: Long,
timezone: String,
offset: Long,
limit: Int
): List<GetAudioContentCommentListItem> {
return queryFactory.selectFrom(audioContentComment)
.where(
audioContentComment.parent.isNotNull
.and(audioContentComment.parent.id.eq(commentId))
.and(audioContentComment.isActive.isTrue)
)
.offset(offset)
.limit(limit.toLong())
.orderBy(audioContentComment.createdAt.desc())
.fetch()
.asSequence()
.map {
val date = it.createdAt!!
.atZone(ZoneId.of("UTC"))
.withZoneSameInstant(ZoneId.of(timezone))
GetAudioContentCommentListItem(
id = it.id!!,
writerId = it.member!!.id!!,
nickname = it.member!!.nickname,
profileUrl = if (it.member!!.profileImage != null) {
"$cloudFrontHost/${it.member!!.profileImage}"
} else {
"$cloudFrontHost/profile/default-profile.png"
},
comment = it.comment,
donationCoin = it.donationCan ?: 0,
date = date.format(DateTimeFormatter.ofPattern("yyyy.MM.dd E hh:mm a")),
replyCount = 0
)
}
.toList()
}
}

View File

@@ -0,0 +1,89 @@
package kr.co.vividnext.sodalive.content.comment
import kr.co.vividnext.sodalive.common.SodaException
import kr.co.vividnext.sodalive.content.AudioContentRepository
import kr.co.vividnext.sodalive.member.Member
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@Service
@Transactional(readOnly = true)
class AudioContentCommentService(
private val repository: AudioContentCommentRepository,
private val audioContentRepository: AudioContentRepository,
@Value("\${cloud.aws.cloud-front.host}")
private val cloudFrontHost: String
) {
@Transactional
fun registerComment(member: Member, comment: String, audioContentId: Long, parentId: Long? = null) {
val audioContent = audioContentRepository.findByIdOrNull(id = audioContentId)
?: throw SodaException("잘못된 콘텐츠 입니다.\n다시 시도해 주세요.")
val audioContentComment = AudioContentComment(comment = comment)
audioContentComment.audioContent = audioContent
audioContentComment.member = member
val parent = if (parentId != null) {
repository.findByIdOrNull(id = parentId)
} else {
null
}
if (parent != null) {
audioContentComment.parent = parent
}
repository.save(audioContentComment)
}
@Transactional
fun modifyComment(request: ModifyCommentRequest, member: Member) {
val audioContentComment = repository.findByIdOrNull(request.commentId)
?: throw SodaException("잘못된 접근 입니다.\n확인 후 다시 시도해 주세요.")
if (audioContentComment.audioContent!!.member!!.id!! != member.id!!) {
if (audioContentComment.member == null || audioContentComment.member!!.id!! != member.id!!) {
throw SodaException("잘못된 접근 입니다.\n확인 후 다시 시도해 주세요.")
}
if (request.comment != null) {
audioContentComment.comment = request.comment
}
}
if (request.isActive != null) {
audioContentComment.isActive = request.isActive
}
}
fun getCommentList(audioContentId: Long, timezone: String, pageable: Pageable): GetAudioContentCommentListResponse {
val commentList =
repository.findByContentId(
cloudFrontHost = cloudFrontHost,
contentId = audioContentId,
timezone = timezone,
offset = pageable.offset,
limit = pageable.pageSize
)
val totalCount = repository.totalCountCommentByContentId(audioContentId)
return GetAudioContentCommentListResponse(totalCount, commentList)
}
fun getCommentReplyList(commentId: Long, timezone: String, pageable: Pageable): GetAudioContentCommentListResponse {
val commentList = repository.getAudioContentCommentReplyList(
cloudFrontHost = cloudFrontHost,
commentId = commentId,
timezone = timezone,
offset = pageable.offset,
limit = pageable.pageSize
)
val totalCount = repository.commentReplyCountByAudioContentCommentId(commentId)
return GetAudioContentCommentListResponse(totalCount, commentList)
}
}

View File

@@ -0,0 +1,17 @@
package kr.co.vividnext.sodalive.content.comment
data class GetAudioContentCommentListResponse(
val totalCount: Int,
val items: List<GetAudioContentCommentListItem>
)
data class GetAudioContentCommentListItem(
val id: Long,
val writerId: Long,
val nickname: String,
val profileUrl: String,
val comment: String,
val donationCoin: Int,
val date: String,
val replyCount: Int
)

View File

@@ -0,0 +1,3 @@
package kr.co.vividnext.sodalive.content.comment
data class ModifyCommentRequest(val commentId: Long, val comment: String? = null, val isActive: Boolean? = null)

View File

@@ -0,0 +1,3 @@
package kr.co.vividnext.sodalive.content.comment
data class RegisterCommentRequest(val comment: String, val contentId: Long, val parentId: Long?)