Compare commits
2 Commits
03d782850c
...
fcd435f470
Author | SHA1 | Date |
---|---|---|
|
fcd435f470 | |
|
88df83fdc0 |
|
@ -0,0 +1,36 @@
|
|||
package kr.co.vividnext.sodalive.admin.content.theme
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
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.RequestPart
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin/audio-content/theme")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
class AdminContentThemeController(private val service: AdminContentThemeService) {
|
||||
@PostMapping
|
||||
fun enrollmentTheme(
|
||||
@RequestPart("image") image: MultipartFile,
|
||||
@RequestPart("request") requestString: String
|
||||
) = ApiResponse.ok(service.uploadThemeImage(image, requestString), "등록되었습니다.")
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
fun deleteTheme(@PathVariable id: Long) = ApiResponse.ok(service.deleteTheme(id), "삭제되었습니다.")
|
||||
|
||||
@PutMapping("/orders")
|
||||
fun updateTagOrders(
|
||||
@RequestBody request: UpdateThemeOrdersRequest
|
||||
) = ApiResponse.ok(service.updateTagOrders(request.ids), "수정되었습니다.")
|
||||
|
||||
@GetMapping
|
||||
fun getThemes() = ApiResponse.ok(service.getThemes())
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package kr.co.vividnext.sodalive.admin.content.theme
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory
|
||||
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||
import kr.co.vividnext.sodalive.content.theme.GetAudioContentThemeResponse
|
||||
import kr.co.vividnext.sodalive.content.theme.QAudioContentTheme.audioContentTheme
|
||||
import kr.co.vividnext.sodalive.content.theme.QGetAudioContentThemeResponse
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface AdminContentThemeRepository : JpaRepository<AudioContentTheme, Long>, AdminContentThemeQueryRepository
|
||||
|
||||
interface AdminContentThemeQueryRepository {
|
||||
fun findIdByTheme(theme: String): Long?
|
||||
fun getActiveThemes(): List<GetAudioContentThemeResponse>
|
||||
}
|
||||
|
||||
class AdminContentThemeQueryRepositoryImpl(
|
||||
private val queryFactory: JPAQueryFactory,
|
||||
|
||||
@Value("\${cloud.aws.cloud-front.host}")
|
||||
private val cloudFrontHost: String
|
||||
) : AdminContentThemeQueryRepository {
|
||||
override fun findIdByTheme(theme: String): Long? {
|
||||
return queryFactory
|
||||
.select(audioContentTheme.id)
|
||||
.from(audioContentTheme)
|
||||
.where(audioContentTheme.theme.eq(theme))
|
||||
.fetchOne()
|
||||
}
|
||||
|
||||
override fun getActiveThemes(): List<GetAudioContentThemeResponse> {
|
||||
return queryFactory
|
||||
.select(
|
||||
QGetAudioContentThemeResponse(
|
||||
audioContentTheme.id,
|
||||
audioContentTheme.theme,
|
||||
audioContentTheme.image.prepend("/").prepend(cloudFrontHost)
|
||||
)
|
||||
)
|
||||
.from(audioContentTheme)
|
||||
.where(audioContentTheme.isActive.isTrue)
|
||||
.orderBy(audioContentTheme.orders.asc())
|
||||
.fetch()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package kr.co.vividnext.sodalive.admin.content.theme
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import kr.co.vividnext.sodalive.aws.s3.S3Uploader
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.content.theme.AudioContentTheme
|
||||
import kr.co.vividnext.sodalive.content.theme.GetAudioContentThemeResponse
|
||||
import kr.co.vividnext.sodalive.utils.generateFileName
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
|
||||
@Service
|
||||
class AdminContentThemeService(
|
||||
private val s3Uploader: S3Uploader,
|
||||
private val objectMapper: ObjectMapper,
|
||||
private val repository: AdminContentThemeRepository,
|
||||
|
||||
@Value("\${cloud.aws.s3.bucket}")
|
||||
private val bucket: String
|
||||
) {
|
||||
@Transactional
|
||||
fun uploadThemeImage(image: MultipartFile, requestString: String) {
|
||||
val request = objectMapper.readValue(requestString, CreateContentThemeRequest::class.java)
|
||||
themeExistCheck(request)
|
||||
|
||||
val fileName = generateFileName()
|
||||
val imagePath = s3Uploader.upload(
|
||||
inputStream = image.inputStream,
|
||||
bucket = bucket,
|
||||
filePath = "audio_content_theme/$fileName"
|
||||
)
|
||||
|
||||
return createTheme(request.theme, imagePath)
|
||||
}
|
||||
|
||||
fun createTheme(theme: String, imagePath: String) {
|
||||
repository.save(AudioContentTheme(theme = theme, image = imagePath))
|
||||
}
|
||||
|
||||
fun themeExistCheck(request: CreateContentThemeRequest) {
|
||||
repository.findIdByTheme(request.theme)?.let { throw SodaException("이미 등록된 테마 입니다.") }
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun deleteTheme(id: Long) {
|
||||
val theme = repository.findByIdOrNull(id)
|
||||
?: throw SodaException("잘못된 요청입니다.")
|
||||
|
||||
theme.theme = "${theme.theme}_deleted"
|
||||
theme.isActive = false
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun updateTagOrders(ids: List<Long>) {
|
||||
for (index in ids.indices) {
|
||||
val theme = repository.findByIdOrNull(ids[index])
|
||||
|
||||
if (theme != null) {
|
||||
theme.orders = index + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getThemes(): List<GetAudioContentThemeResponse> {
|
||||
return repository.getActiveThemes()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package kr.co.vividnext.sodalive.admin.content.theme
|
||||
|
||||
data class CreateContentThemeRequest(val theme: String)
|
|
@ -0,0 +1,5 @@
|
|||
package kr.co.vividnext.sodalive.admin.content.theme
|
||||
|
||||
data class UpdateThemeOrdersRequest(
|
||||
val ids: List<Long>
|
||||
)
|
|
@ -0,0 +1,12 @@
|
|||
package kr.co.vividnext.sodalive.menu
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import com.querydsl.core.annotations.QueryProjection
|
||||
|
||||
data class GetMenuResponse @QueryProjection constructor(
|
||||
val title: String,
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
val route: String? = null,
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
val items: List<GetMenuResponse>? = null
|
||||
)
|
|
@ -0,0 +1,32 @@
|
|||
package kr.co.vividnext.sodalive.menu
|
||||
|
||||
import kr.co.vividnext.sodalive.common.BaseEntity
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import javax.persistence.CascadeType
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.EnumType
|
||||
import javax.persistence.Enumerated
|
||||
import javax.persistence.FetchType
|
||||
import javax.persistence.JoinColumn
|
||||
import javax.persistence.ManyToOne
|
||||
import javax.persistence.OneToMany
|
||||
|
||||
@Entity
|
||||
data class Menu(
|
||||
@Column(nullable = false)
|
||||
val title: String,
|
||||
@Column(nullable = false)
|
||||
val route: String,
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
val roles: MemberRole,
|
||||
val orders: Int,
|
||||
val isActive: Boolean
|
||||
) : BaseEntity() {
|
||||
@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
|
||||
@JoinColumn(name = "parent_id", nullable = true)
|
||||
var parent: Menu? = null
|
||||
|
||||
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
|
||||
var children: MutableList<Menu> = mutableListOf()
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package kr.co.vividnext.sodalive.menu
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.security.core.userdetails.User
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/menu")
|
||||
class MenuController(private val service: MenuService) {
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAnyRole('AGENT', 'ADMIN')")
|
||||
fun getMenus(@AuthenticationPrincipal user: User) = ApiResponse.ok(service.getMenus(user))
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package kr.co.vividnext.sodalive.menu
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import kr.co.vividnext.sodalive.menu.QMenu.menu
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface MenuRepository : JpaRepository<Menu, Long>, MenuQueryRepository
|
||||
|
||||
interface MenuQueryRepository {
|
||||
fun getMenu(role: MemberRole): List<GetMenuResponse>
|
||||
}
|
||||
|
||||
class MenuQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : MenuQueryRepository {
|
||||
override fun getMenu(role: MemberRole): List<GetMenuResponse> {
|
||||
return queryFactory
|
||||
.selectFrom(menu)
|
||||
.where(
|
||||
menu.isActive.isTrue
|
||||
.and(menu.roles.eq(role))
|
||||
.and(
|
||||
(menu.route.isNotNull.and(menu.parent.isNull))
|
||||
.or(menu.route.isNull.and(menu.children.isNotEmpty))
|
||||
)
|
||||
)
|
||||
.orderBy(menu.orders.asc())
|
||||
.fetch()
|
||||
.asSequence()
|
||||
.map {
|
||||
GetMenuResponse(
|
||||
title = it.title,
|
||||
route = it.route,
|
||||
items = if (it.children.isNotEmpty()) {
|
||||
it.children
|
||||
.asSequence()
|
||||
.map { menu -> GetMenuResponse(menu.title, menu.route) }
|
||||
.toList()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
)
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package kr.co.vividnext.sodalive.menu
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import org.springframework.security.core.userdetails.User
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class MenuService(
|
||||
private val repository: MenuRepository,
|
||||
private val memberRepository: MemberRepository
|
||||
) {
|
||||
fun getMenus(user: User): List<GetMenuResponse> {
|
||||
val member = memberRepository.findByEmail(user.username)
|
||||
?: throw SodaException("로그인 정보를 확인해주세요.")
|
||||
return repository.getMenu(member.role)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue