Skip to content

Commit

Permalink
feat: banner
Browse files Browse the repository at this point in the history
  • Loading branch information
jbj338033 committed Aug 1, 2024
1 parent d20d7c7 commit 0e99b53
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.open3r.openmusic.domain.admin.banner.controller

import com.open3r.openmusic.domain.admin.banner.dto.request.BannerCreateRequest
import com.open3r.openmusic.domain.admin.banner.service.AdminBannerService
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.*

@Tag(name = "관리자: 배너", description = "Admin: Banner")
@RestController
@RequestMapping("/admin/banners")
class AdminBannerController(
private val adminBannerService: AdminBannerService
) {
@Operation(summary = "배너 생성")
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
fun createBanner(@RequestBody request: BannerCreateRequest) =
BaseResponse(adminBannerService.createBanner(request), 201).toEntity()

@Operation(summary = "배너 삭제")
@DeleteMapping("/{bannerId}")
@PreAuthorize("hasRole('ADMIN')")
fun deleteBanner(@PathVariable bannerId: Long) =
BaseResponse(adminBannerService.deleteBanner(bannerId), 204).toEntity()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.open3r.openmusic.domain.admin.banner.dto.request

import jakarta.validation.constraints.NotBlank
import org.hibernate.validator.constraints.URL

data class BannerCreateRequest(
@field:NotBlank
@field:URL
val url: String,
@field:NotBlank
@field:URL
val imageUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.open3r.openmusic.domain.admin.banner.service

import com.open3r.openmusic.domain.admin.banner.dto.request.BannerCreateRequest
import com.open3r.openmusic.domain.banner.dto.response.BannerResponse

interface AdminBannerService {
fun createBanner(request: BannerCreateRequest): BannerResponse
fun deleteBanner(bannerId: Long)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.open3r.openmusic.domain.admin.banner.service.impl

import com.open3r.openmusic.domain.admin.banner.dto.request.BannerCreateRequest
import com.open3r.openmusic.domain.admin.banner.service.AdminBannerService
import com.open3r.openmusic.domain.banner.domain.entity.BannerEntity
import com.open3r.openmusic.domain.banner.dto.response.BannerResponse
import com.open3r.openmusic.domain.banner.repository.BannerRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class AdminBannerServiceImpl(
private val bannerRepository: BannerRepository
) : AdminBannerService {
@Transactional
override fun createBanner(request: BannerCreateRequest): BannerResponse {
return BannerResponse.of(bannerRepository.save(BannerEntity(url = request.url, imageUrl = request.imageUrl)))
}

@Transactional
override fun deleteBanner(bannerId: Long) {
bannerRepository.deleteById(bannerId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.open3r.openmusic.domain.banner.controller

import com.open3r.openmusic.domain.banner.service.BannerService
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.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Tag(name = "배너", description = "Banner")
@RestController
@RequestMapping("/banners")
class BannerController(
private val bannerService: BannerService
) {
@Operation(summary = "배너 목록 조회")
@GetMapping
fun getBanners() = BaseResponse(bannerService.getBanners(), 200).toEntity()

@Operation(summary = "배너 조회")
@GetMapping("/{bannerId}")
fun getBanner(@PathVariable bannerId: Long) = BaseResponse(bannerService.getBanner(bannerId), 200).toEntity()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.open3r.openmusic.domain.banner.domain.entity

import com.open3r.openmusic.global.common.domain.entity.BaseEntity
import jakarta.persistence.*

@Entity
@Table(name = "banners")
class BannerEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,

@Column(name = "url", nullable = false, updatable = false)
val url: String,

@Column(name = "image_url", nullable = false, updatable = false)
val imageUrl: String,
) : BaseEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.open3r.openmusic.domain.banner.dto.response

import com.open3r.openmusic.domain.banner.domain.entity.BannerEntity
import java.time.LocalDateTime

data class BannerResponse(
val id: Long,
val url: String,
val imageUrl: String,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime,
) {
companion object {
fun of(banner: BannerEntity) = BannerResponse(
id = banner.id!!,
url = banner.url,
imageUrl = banner.imageUrl,
createdAt = banner.createdAt!!,
updatedAt = banner.updatedAt!!,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.open3r.openmusic.domain.banner.repository

import com.open3r.openmusic.domain.banner.domain.entity.BannerEntity
import org.springframework.data.jpa.repository.JpaRepository

interface BannerRepository : JpaRepository<BannerEntity, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.open3r.openmusic.domain.banner.service

import com.open3r.openmusic.domain.banner.dto.response.BannerResponse

interface BannerService {
fun getBanners(): List<BannerResponse>
fun getBanner(bannerId: Long): BannerResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.open3r.openmusic.domain.banner.service.impl

import com.open3r.openmusic.domain.banner.dto.response.BannerResponse
import com.open3r.openmusic.domain.banner.repository.BannerRepository
import com.open3r.openmusic.domain.banner.service.BannerService
import com.open3r.openmusic.global.error.CustomException
import com.open3r.openmusic.global.error.ErrorCode
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class BannerServiceImpl(
private val bannerRepository: BannerRepository
) : BannerService {
@Transactional(readOnly = true)
override fun getBanners(): List<BannerResponse> {
return bannerRepository.findAll().map { BannerResponse.of(it) }
}

@Transactional(readOnly = true)
override fun getBanner(bannerId: Long): BannerResponse {
return BannerResponse.of(
bannerRepository.findByIdOrNull(bannerId) ?: throw CustomException(ErrorCode.BANNER_NOT_FOUND)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class FileServiceImpl(

override fun uploadFiles(files: List<MultipartFile>): List<FileUploadResponse> {
return files.map {
val type = it.contentType?.substringBefore("/") ?: "file"

if (type != "image" && type != "audio") {
throw IllegalArgumentException("지원하지 않는 파일 형식입니다.")
}

val name = "${UUID.randomUUID()}.${it.originalFilename?.substringAfterLast(".")}"

val metadata = ObjectMetadata().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class UserController(
@Operation(summary = "나 수정")
@PatchMapping("/me")
@PreAuthorize("isAuthenticated()")
fun updateMe(@RequestBody @Valid request: UserUpdateRequest) = BaseResponse(userService.updateMe(request), 200).toEntity()
fun updateMe(@RequestBody @Valid request: UserUpdateRequest) =
BaseResponse(userService.updateMe(request), 200).toEntity()

@Operation(summary = "현재 재생 중인 노래 조회")
@GetMapping("/me/now-playing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.time.Instant
class StartListener(
private val jda: JDA,
private val discordProperties: DiscordProperties
): ApplicationListener<ApplicationStartedEvent> {
) : ApplicationListener<ApplicationStartedEvent> {
override fun onApplicationEvent(event: ApplicationStartedEvent) {
jda.getTextChannelById(discordProperties.channelId)?.sendMessageEmbeds(
EmbedBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.time.Instant
class StopListener(
private val jda: JDA,
private val discordProperties: DiscordProperties
): ApplicationListener<ContextClosedEvent> {
) : ApplicationListener<ContextClosedEvent> {
override fun onApplicationEvent(event: ContextClosedEvent) {
jda.getTextChannelById(discordProperties.channelId)?.sendMessageEmbeds(
EmbedBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class SwaggerConfig {
)
.termsOfService("https://open3r.com/terms")
)
.servers(listOf(Server().apply { url = "https://api.openmusic.kr" }))
.servers(listOf(
Server().apply { url = "https://api.openmusic.kr" },
Server().apply { url = "http://localhost:8080" }
))
.addSecurityItem(SecurityRequirement().addList("Authorization"))
.components(
Components()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ enum class ErrorCode(
USER_QUEUE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "User queue already exists"),

// Else
MAX_UPLOAD_SIZE_EXCEEDED(HttpStatus.BAD_REQUEST, "Max upload size exceeded")
MAX_UPLOAD_SIZE_EXCEEDED(HttpStatus.BAD_REQUEST, "Max upload size exceeded"),

// Banner
BANNER_NOT_FOUND(HttpStatus.NOT_FOUND, "Banner not found"),
}

0 comments on commit 0e99b53

Please sign in to comment.