신고 API 추가
This commit is contained in:
parent
472b8d36f5
commit
5d6eb5da4f
|
@ -0,0 +1,34 @@
|
|||
package kr.co.vividnext.sodalive.report
|
||||
|
||||
import kr.co.vividnext.sodalive.common.BaseEntity
|
||||
import kr.co.vividnext.sodalive.explorer.profile.CreatorCheers
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.EnumType
|
||||
import javax.persistence.Enumerated
|
||||
import javax.persistence.FetchType
|
||||
import javax.persistence.JoinColumn
|
||||
import javax.persistence.ManyToOne
|
||||
|
||||
@Entity
|
||||
data class Report(
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
val type: ReportType,
|
||||
val reason: String
|
||||
) : BaseEntity() {
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "member_id", nullable = false)
|
||||
var member: Member? = null
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "reported_member_id", nullable = true)
|
||||
var reportedAccount: Member? = null
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "cheers_id", nullable = true)
|
||||
var cheers: CreatorCheers? = null
|
||||
}
|
||||
|
||||
enum class ReportType {
|
||||
REVIEW, PROFILE, USER, CHEERS, AUDIO_CONTENT
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package kr.co.vividnext.sodalive.report
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/report")
|
||||
class ReportController(private val service: ReportService) {
|
||||
@PostMapping
|
||||
fun report(
|
||||
@RequestBody request: ReportRequest,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
) = run {
|
||||
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
|
||||
ApiResponse.ok(service.save(member, request), "신고가 접수되었습니다.")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package kr.co.vividnext.sodalive.report
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface ReportRepository : JpaRepository<Report, Long>
|
|
@ -0,0 +1,9 @@
|
|||
package kr.co.vividnext.sodalive.report
|
||||
|
||||
data class ReportRequest(
|
||||
val type: ReportType,
|
||||
val reason: String,
|
||||
val reportedMemberId: Long? = null,
|
||||
val cheersId: Long? = null,
|
||||
val audioContentId: Long? = null
|
||||
)
|
|
@ -0,0 +1,52 @@
|
|||
package kr.co.vividnext.sodalive.report
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.explorer.profile.CreatorCheersRepository
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
class ReportService(
|
||||
private val repository: ReportRepository,
|
||||
private val memberRepository: MemberRepository,
|
||||
private val cheersRepository: CreatorCheersRepository
|
||||
) {
|
||||
@Transactional
|
||||
fun save(member: Member, request: ReportRequest) {
|
||||
if (conditionAllIsNull(request, isNull = true) || conditionAllIsNull(request, isNull = false)) {
|
||||
throw SodaException("신고가 접수되었습니다.")
|
||||
}
|
||||
|
||||
val reportedAccount = if (request.reportedMemberId != null) {
|
||||
memberRepository.findByIdOrNull(request.reportedMemberId)
|
||||
?: throw SodaException("신고가 접수되었습니다.")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val cheers = if (request.cheersId != null) {
|
||||
cheersRepository.findByIdOrNull(request.cheersId)
|
||||
?: throw SodaException("신고가 접수되었습니다.")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val report = Report(type = request.type, reason = request.reason)
|
||||
report.member = member
|
||||
report.reportedAccount = reportedAccount
|
||||
report.cheers = cheers
|
||||
repository.save(report)
|
||||
}
|
||||
|
||||
private fun conditionAllIsNull(request: ReportRequest, isNull: Boolean): Boolean {
|
||||
return if (isNull) {
|
||||
request.reportedMemberId == null && request.cheersId == null && request.audioContentId == null
|
||||
} else {
|
||||
request.reportedMemberId != null && request.cheersId != null && request.audioContentId != null
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue