Skip to content

Commit

Permalink
feat: admin album
Browse files Browse the repository at this point in the history
  • Loading branch information
jbj338033 committed Aug 5, 2024
1 parent a2e4b9a commit fedcb49
Show file tree
Hide file tree
Showing 22 changed files with 294 additions and 190 deletions.
10 changes: 6 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
plugins {
val kotlinVersion = "1.9.24"

id("org.springframework.boot") version "3.2.5"
id("io.spring.dependency-management") version "1.1.5"
kotlin("jvm") version "1.9.24"
kotlin("kapt") version "1.9.24"
kotlin("plugin.spring") version "1.9.24"
kotlin("plugin.jpa") version "1.9.24"
kotlin("jvm") version kotlinVersion
kotlin("kapt") version kotlinVersion
kotlin("plugin.spring") version kotlinVersion
kotlin("plugin.jpa") version kotlinVersion
}

group = "com.open3r"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.open3r.openmusic.domain.admin.album.controller

import com.open3r.openmusic.domain.admin.album.service.AdminAlbumService
import com.open3r.openmusic.global.common.dto.response.BaseResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.data.domain.Pageable
import org.springframework.data.web.PageableDefault
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*

@Tag(name = "어드민: 앨범", description = "Admin: Album")
@RestController
@RequestMapping("/admin/albums")
class AdminAlbumController(
private val adminAlbumService: AdminAlbumService
) {
@Operation(summary = "대기 중인 앨범 목록 조회", description = "대기 중인 앨범 목록을 조회합니다.")
@GetMapping("/pending")
@PreAuthorize("hasRole('ADMIN')")
fun getPendingAlbums(@PageableDefault pageable: Pageable) =
BaseResponse(adminAlbumService.getPendingAlbums(pageable), 200).toEntity()

@Operation(summary = "승인된 앨범 목록 조회", description = "승인된 앨범 목록을 조회합니다.")
@GetMapping("/approved")
@PreAuthorize("hasRole('ADMIN')")
fun getApprovedAlbums(@PageableDefault pageable: Pageable) =
BaseResponse(adminAlbumService.getApprovedAlbums(pageable), 200).toEntity()

@Operation(summary = "거부된 앨범 목록 조회", description = "거부된 앨범 목록을 조회합니다.")
@GetMapping("/rejected")
@PreAuthorize("hasRole('ADMIN')")
fun getRejectedAlbums(@PageableDefault pageable: Pageable) =
BaseResponse(adminAlbumService.getRejectedAlbums(pageable), 200).toEntity()

@Operation(summary = "삭제된 앨범 목록 조회", description = "삭제된 앨범 목록을 조회합니다.")
@GetMapping("/deleted")
@PreAuthorize("hasRole('ADMIN')")
fun getDeletedAlbums(@PageableDefault pageable: Pageable) =
BaseResponse(adminAlbumService.getDeletedAlbums(pageable), 200).toEntity()

@Operation(summary = "앨범 승인", description = "앨범을 승인합니다.")
@PostMapping("/approve")
@PreAuthorize("hasRole('ADMIN')")
fun approveAlbum(@RequestParam albumId: Long) =
BaseResponse(adminAlbumService.approveAlbum(albumId), 200).toEntity()

@Operation(summary = "앨범 거부", description = "앨범을 거부합니다.")
@PostMapping("/reject")
@PreAuthorize("hasRole('ADMIN')")
fun rejectAlbum(@RequestParam albumId: Long) = BaseResponse(adminAlbumService.rejectAlbum(albumId), 200).toEntity()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.open3r.openmusic.domain.admin.album.repository

import com.open3r.openmusic.domain.album.domain.entity.AlbumEntity
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable

interface AdminAlbumQueryRepository {
fun getPendingAlbums(pageable: Pageable): Page<AlbumEntity>
fun getApprovedAlbums(pageable: Pageable): Page<AlbumEntity>
fun getRejectedAlbums(pageable: Pageable): Page<AlbumEntity>
fun getDeletedAlbums(pageable: Pageable): Page<AlbumEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.open3r.openmusic.domain.admin.album.repository.impl

import com.open3r.openmusic.domain.admin.album.repository.AdminAlbumQueryRepository
import com.open3r.openmusic.domain.album.domain.entity.AlbumEntity
import com.open3r.openmusic.domain.album.domain.entity.QAlbumEntity.albumEntity
import com.open3r.openmusic.domain.album.domain.enums.AlbumStatus
import com.open3r.openmusic.global.util.paginate
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Repository

@Repository
class AdminAlbumQueryRepositoryImpl(
private val jpaQueryFactory: JPAQueryFactory
) : AdminAlbumQueryRepository {
override fun getPendingAlbums(pageable: Pageable): Page<AlbumEntity> {
val count = jpaQueryFactory.select(albumEntity.count())
.from(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.PENDING))
.fetchOne() ?: 0

val albums = jpaQueryFactory.selectFrom(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.PENDING))
.paginate(pageable)
.orderBy(albumEntity.createdAt.desc())
.fetch()

return PageImpl(albums, pageable, count)
}

override fun getApprovedAlbums(pageable: Pageable): Page<AlbumEntity> {
val count = jpaQueryFactory.select(albumEntity.count())
.from(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.APPROVED))
.fetchOne() ?: 0

val albums = jpaQueryFactory.selectFrom(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.APPROVED))
.paginate(pageable)
.orderBy(albumEntity.createdAt.desc())
.fetch()

return PageImpl(albums, pageable, count)
}

override fun getRejectedAlbums(pageable: Pageable): Page<AlbumEntity> {
val count = jpaQueryFactory.select(albumEntity.count())
.from(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.REJECTED))
.fetchOne() ?: 0

val albums = jpaQueryFactory.selectFrom(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.REJECTED))
.paginate(pageable)
.orderBy(albumEntity.createdAt.desc())
.fetch()

return PageImpl(albums, pageable, count)
}

override fun getDeletedAlbums(pageable: Pageable): Page<AlbumEntity> {
val count = jpaQueryFactory.select(albumEntity.count())
.from(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.DELETED))
.fetchOne() ?: 0

val albums = jpaQueryFactory.selectFrom(albumEntity)
.where(albumEntity.status.eq(AlbumStatus.DELETED))
.paginate(pageable)
.orderBy(albumEntity.createdAt.desc())
.fetch()

return PageImpl(albums, pageable, count)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.open3r.openmusic.domain.admin.album.service

import com.open3r.openmusic.domain.album.dto.response.AlbumResponse
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable

interface AdminAlbumService {
fun getPendingAlbums(pageable: Pageable): Page<AlbumResponse>
fun getApprovedAlbums(pageable: Pageable): Page<AlbumResponse>
fun getRejectedAlbums(pageable: Pageable): Page<AlbumResponse>
fun getDeletedAlbums(pageable: Pageable): Page<AlbumResponse>

fun approveAlbum(albumId: Long)
fun rejectAlbum(albumId: Long)

fun deleteAlbum(albumId: Long)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.open3r.openmusic.domain.admin.album.service.impl

import com.open3r.openmusic.domain.admin.album.repository.AdminAlbumQueryRepository
import com.open3r.openmusic.domain.admin.album.service.AdminAlbumService
import com.open3r.openmusic.domain.album.domain.enums.AlbumStatus
import com.open3r.openmusic.domain.album.dto.response.AlbumResponse
import com.open3r.openmusic.domain.album.repository.AlbumRepository
import com.open3r.openmusic.global.error.CustomException
import com.open3r.openmusic.global.error.ErrorCode
import org.springframework.data.domain.Page
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
class AdminAlbumServiceImpl(
private val albumRepository: AlbumRepository,
private val adminAlbumQueryRepository: AdminAlbumQueryRepository
) : AdminAlbumService {
override fun getPendingAlbums(pageable: Pageable): Page<AlbumResponse> {
return adminAlbumQueryRepository.getPendingAlbums(pageable).map { AlbumResponse.of(it) }
}

override fun getApprovedAlbums(pageable: Pageable): Page<AlbumResponse> {
return adminAlbumQueryRepository.getApprovedAlbums(pageable).map { AlbumResponse.of(it) }
}

override fun getRejectedAlbums(pageable: Pageable): Page<AlbumResponse> {
return adminAlbumQueryRepository.getRejectedAlbums(pageable).map { AlbumResponse.of(it) }
}

override fun getDeletedAlbums(pageable: Pageable): Page<AlbumResponse> {
return adminAlbumQueryRepository.getDeletedAlbums(pageable).map { AlbumResponse.of(it) }
}

override fun approveAlbum(albumId: Long) {
val album = albumRepository.findByIdOrNull(albumId) ?: throw CustomException(ErrorCode.ALBUM_NOT_FOUND)

if (album.status != AlbumStatus.PENDING) {
throw CustomException(ErrorCode.ALBUM_STATUS_INVALID)
}

album.status = AlbumStatus.APPROVED

album.songs.forEach { it.song.status = AlbumStatus.APPROVED }

albumRepository.save(album)
}

override fun rejectAlbum(albumId: Long) {
val album = albumRepository.findByIdOrNull(albumId) ?: throw CustomException(ErrorCode.ALBUM_NOT_FOUND)

if (album.status != AlbumStatus.PENDING) {
throw CustomException(ErrorCode.ALBUM_STATUS_INVALID)
}

album.status = AlbumStatus.REJECTED

album.songs.forEach { it.song.status = AlbumStatus.REJECTED }

albumRepository.save(album)
}

override fun deleteAlbum(albumId: Long) {
val album = albumRepository.findByIdOrNull(albumId) ?: throw CustomException(ErrorCode.ALBUM_NOT_FOUND)

album.status = AlbumStatus.DELETED

album.songs.forEach { it.song.status = AlbumStatus.DELETED }

albumRepository.save(album)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.open3r.openmusic.global.common.dto.response.BaseResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Tag(name = "관리자: 음악", description = "Admin: Song")
@RestController
Expand All @@ -17,39 +19,4 @@ class AdminSongController(
@GetMapping
@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')")
fun deleteSong(@PathVariable songId: Long) = BaseResponse(adminSongService.deleteSong(songId), 204).toEntity()
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ 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)
}
Loading

0 comments on commit fedcb49

Please sign in to comment.