Skip to content

Commit

Permalink
feat: admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
jbj338033 committed Aug 2, 2024
1 parent 02b2108 commit a2e4b9a
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ class AdminSongController(
@PreAuthorize("hasRole('ADMIN')")
fun getSongs() = BaseResponse(adminSongService.getSongs(), 200).toEntity()

@Operation(summary = "대기중인 음악 목록")
@GetMapping("/pending")
@PreAuthorize("hasRole('ADMIN')")
fun getPendingSongs() = BaseResponse(adminSongService.getPendingSongs(), 200).toEntity()

@Operation(summary = "승인된 음악 목록")
@GetMapping("/approved")
@PreAuthorize("hasRole('ADMIN')")
fun getApprovedSongs() = BaseResponse(adminSongService.getApprovedSongs(), 200).toEntity()

@Operation(summary = "거부된 음악 목록")
@GetMapping("/rejected")
@PreAuthorize("hasRole('ADMIN')")
fun getRejectedSongs() = BaseResponse(adminSongService.getRejectedSongs(), 200).toEntity()

@Operation(summary = "삭제된 음악 목록")
@GetMapping("/deleted")
@PreAuthorize("hasRole('ADMIN')")
fun getDeletedSongs() = BaseResponse(adminSongService.getDeletedSongs(), 200).toEntity()

@Operation(summary = "음악 승인")
@PutMapping("/{songId}/approve")
@PreAuthorize("hasRole('ADMIN')")
fun approveSong(@PathVariable songId: Long) = BaseResponse(adminSongService.approveSong(songId), 200).toEntity()

@Operation(summary = "음악 거부")
@PutMapping("/{songId}/reject")
@PreAuthorize("hasRole('ADMIN')")
fun rejectSong(@PathVariable songId: Long) = BaseResponse(adminSongService.rejectSong(songId), 200).toEntity()

@Operation(summary = "음악 삭제")
@DeleteMapping("/{songId}")
@PreAuthorize("hasRole('ADMIN')")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.open3r.openmusic.domain.admin.song.repository

import com.open3r.openmusic.domain.song.domain.entity.SongEntity

interface AdminSongQueryRepository {
fun getPendingSongs(): List<SongEntity>
fun getApprovedSongs(): List<SongEntity>
fun getRejectedSongs(): List<SongEntity>
fun getDeletedSongs(): List<SongEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.open3r.openmusic.domain.admin.song.repository.impl

import com.open3r.openmusic.domain.admin.song.repository.AdminSongQueryRepository
import com.open3r.openmusic.domain.song.domain.entity.QSongEntity.songEntity
import com.open3r.openmusic.domain.song.domain.entity.SongEntity
import com.open3r.openmusic.domain.song.domain.enums.SongStatus
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

@Repository
class AdminSongQueryRepositoryImpl(
private val jpaQueryFactory: JPAQueryFactory
): AdminSongQueryRepository {
@Transactional
override fun getPendingSongs(): List<SongEntity> {
return jpaQueryFactory.selectFrom(songEntity)
.where(songEntity.status.eq(SongStatus.PENDING))
.fetch()
}

@Transactional
override fun getApprovedSongs(): List<SongEntity> {
return jpaQueryFactory.selectFrom(songEntity)
.where(songEntity.status.eq(SongStatus.APPROVED))
.fetch()
}

@Transactional
override fun getRejectedSongs(): List<SongEntity> {
return jpaQueryFactory.selectFrom(songEntity)
.where(songEntity.status.eq(SongStatus.REJECTED))
.fetch()
}

@Transactional
override fun getDeletedSongs(): List<SongEntity> {
return jpaQueryFactory.selectFrom(songEntity)
.where(songEntity.status.eq(SongStatus.DELETED))
.fetch()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ import com.open3r.openmusic.domain.song.dto.response.SongResponse

interface AdminSongService {
fun getSongs(): List<SongResponse>
fun getPendingSongs(): List<SongResponse>
fun getApprovedSongs(): List<SongResponse>
fun getRejectedSongs(): List<SongResponse>
fun getDeletedSongs(): List<SongResponse>

fun approveSong(songId: Long)
fun rejectSong(songId: Long)

fun deleteSong(songId: Long)
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
package com.open3r.openmusic.domain.admin.song.service.impl

import com.open3r.openmusic.domain.admin.song.repository.AdminSongQueryRepository
import com.open3r.openmusic.domain.admin.song.repository.impl.AdminSongQueryRepositoryImpl
import com.open3r.openmusic.domain.admin.song.service.AdminSongService
import com.open3r.openmusic.domain.song.dto.response.SongResponse
import com.open3r.openmusic.domain.song.repository.SongRepository
import com.open3r.openmusic.global.error.CustomException
import com.open3r.openmusic.global.error.ErrorCode
import com.open3r.openmusic.global.security.UserSecurity
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class AdminSongServiceImpl(
private val userSecurity: UserSecurity,
private val songRepository: SongRepository
private val songRepository: SongRepository,
private val adminSongQueryRepository: AdminSongQueryRepository
) : AdminSongService {
@Transactional(readOnly = true)
override fun getSongs() = songRepository.findAll().map { SongResponse.of(it, userSecurity.user) }

@Transactional(readOnly = true)
override fun getPendingSongs() = adminSongQueryRepository.getPendingSongs().map { SongResponse.of(it) }

@Transactional(readOnly = true)
override fun getApprovedSongs() = adminSongQueryRepository.getApprovedSongs().map { SongResponse.of(it) }

@Transactional(readOnly = true)
override fun getRejectedSongs() = adminSongQueryRepository.getRejectedSongs().map { SongResponse.of(it) }

@Transactional(readOnly = true)
override fun getDeletedSongs() = adminSongQueryRepository.getDeletedSongs().map { SongResponse.of(it) }

@Transactional
override fun approveSong(songId: Long) {
val song = songRepository.findByIdOrNull(songId) ?: throw CustomException(ErrorCode.SONG_NOT_FOUND)

song.approve()

songRepository.save(song)
}

@Transactional
override fun rejectSong(songId: Long) {
val song = songRepository.findByIdOrNull(songId) ?: throw CustomException(ErrorCode.SONG_NOT_FOUND)

song.reject()

songRepository.save(song)
}

@Transactional
override fun deleteSong(songId: Long) {
val song = songRepository.findById(songId).orElseThrow { throw CustomException(ErrorCode.SONG_NOT_FOUND) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AlbumEntity(
@Column(name = "title", nullable = true)
var title: String,

@Column(name = "description", nullable = true)
@Column(name = "description", nullable = true, columnDefinition = "LONGTEXT")
var description: String,

@Column(name = "cover_url", nullable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.open3r.openmusic.domain.album.repository.AlbumRepository
import com.open3r.openmusic.domain.album.service.AlbumService
import com.open3r.openmusic.domain.song.domain.entity.SongEntity
import com.open3r.openmusic.domain.song.domain.entity.SongLyricsEntity
import com.open3r.openmusic.domain.song.domain.enums.SongStatus
import com.open3r.openmusic.domain.song.repository.SongRepository
import com.open3r.openmusic.global.error.CustomException
import com.open3r.openmusic.global.error.ErrorCode
Expand Down Expand Up @@ -68,6 +69,7 @@ class AlbumServiceImpl(
genre = album.genre,
scope = album.scope,
artist = user,
status = SongStatus.PENDING
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.open3r.openmusic.domain.song.domain.entity
import com.open3r.openmusic.domain.album.domain.entity.AlbumEntity
import com.open3r.openmusic.domain.album.domain.enums.AlbumScope
import com.open3r.openmusic.domain.song.domain.enums.SongGenre
import com.open3r.openmusic.domain.song.domain.enums.SongStatus
import com.open3r.openmusic.domain.user.domain.entity.UserEntity
import com.open3r.openmusic.global.common.domain.entity.BaseEntity
import com.open3r.openmusic.global.error.CustomException
import com.open3r.openmusic.global.error.ErrorCode
import jakarta.persistence.*

@Entity
Expand All @@ -26,6 +29,10 @@ class SongEntity(
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL], orphanRemoval = true, mappedBy = "song")
val lyrics: MutableList<SongLyricsEntity> = mutableListOf(),

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
var status: SongStatus,

@Enumerated(EnumType.STRING)
@Column(name = "scope", nullable = false)
var scope: AlbumScope,
Expand All @@ -41,4 +48,16 @@ class SongEntity(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "artist_id", nullable = false)
val artist: UserEntity
) : BaseEntity()
) : BaseEntity() {
fun approve() {
if (status != SongStatus.PENDING) throw CustomException(ErrorCode.SONG_IS_NOT_PENDING)

status = SongStatus.APPROVED
}

fun reject() {
if (status != SongStatus.PENDING) throw CustomException(ErrorCode.SONG_IS_NOT_PENDING)

status = SongStatus.REJECTED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.open3r.openmusic.domain.song.domain.enums

enum class SongStatus {
PENDING,
APPROVED,
REJECTED,
DELETED
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.open3r.openmusic.domain.song.dto.response
import com.open3r.openmusic.domain.album.domain.enums.AlbumScope
import com.open3r.openmusic.domain.song.domain.entity.SongEntity
import com.open3r.openmusic.domain.song.domain.enums.SongGenre
import com.open3r.openmusic.domain.song.domain.enums.SongStatus
import com.open3r.openmusic.domain.user.domain.entity.UserEntity
import com.open3r.openmusic.domain.user.dto.response.UserResponse
import java.time.LocalDateTime
Expand All @@ -15,6 +16,7 @@ data class SongResponse(
// val likes: List<Long>,
val liked: Boolean,
val likeCount: Long,
val status: SongStatus,
val genre: SongGenre,
val scope: AlbumScope,
val artist: UserResponse,
Expand All @@ -31,6 +33,7 @@ data class SongResponse(
// likes = song.likes.map { it.id!! },
liked = user?.let { song.likes.any { it.user.id == user.id } } ?: false,
likeCount = song.likes.size.toLong(),
status = song.status,
genre = song.genre,
scope = song.scope,
thumbnailUrl = song.album.coverUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package com.open3r.openmusic.domain.song.repository

import com.open3r.openmusic.domain.song.domain.entity.SongEntity
import com.open3r.openmusic.domain.song.domain.enums.SongGenre
import com.open3r.openmusic.domain.user.domain.entity.UserEntity
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice

interface SongQueryRepository {
fun getSongs(pageable: Pageable): Page<SongEntity>
fun getSongsByArtist(artist: UserEntity): List<SongEntity>
fun getMySongs(pageable: Pageable): Page<SongEntity>

fun getRankingSongs(pageable: Pageable): Slice<SongEntity>
fun getLatestSongs(pageable: Pageable): Slice<SongEntity>
fun getGenreSongs(genre: SongGenre, pageable: Pageable): Page<SongEntity>
fun getSongsByGenreIn(genres: List<SongGenre>, pageable: Pageable): Page<SongEntity>

fun searchSongs(query: String, pageable: Pageable): Slice<SongEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository

interface SongRepository : JpaRepository<SongEntity, Long> {
fun findAllByArtist(artist: UserEntity): List<SongEntity>
fun findAllByArtist(artist: UserEntity, pageable: Pageable): Page<SongEntity>
fun findAllByTitleContainingIgnoreCase(title: String): List<SongEntity>
}
Loading

0 comments on commit a2e4b9a

Please sign in to comment.