[#26] feat: ApplicationResponse Builder 클래스 적용

- ApplicationResponse Generic type 지정하기 위해 중간 팩토리 클래스 적용
  (ApplicationResponseBuilder)
This commit is contained in:
Hanbin Lee
2022-12-31 19:43:42 +09:00
parent b6acd3578a
commit 4caac76514
7 changed files with 66 additions and 54 deletions

View File

@@ -17,7 +17,7 @@ class AuthController(
private val jwtTokenUtils: JwtTokenUtils
) {
@PostMapping("/authenticate")
fun signIn(@RequestBody signInRequest: SignInRequest): ApplicationResponse {
fun signIn(@RequestBody signInRequest: SignInRequest): ApplicationResponse<TokenResponse> {
val authentication = authService.signIn(
email = signInRequest.email,
password = signInRequest.password

View File

@@ -14,7 +14,7 @@ class MemberController(
private val memberService: MemberService
) {
@PostMapping("/sign-up")
fun signUp(@RequestBody resource: MemberRegisterRequest): ApplicationResponse {
fun signUp(@RequestBody resource: MemberRegisterRequest): ApplicationResponse<Long> {
val registerMemberId = memberService.registerMember(resource)
return ApplicationResponse

View File

@@ -20,9 +20,9 @@ class BasicControllerAdvice {
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception::class)
fun handleException(e: Exception): ApplicationResponse {
fun handleException(e: Exception): ApplicationResponse<Nothing> {
logger.error { "[COMMON][${e.javaClass.simpleName}] $e" }
return ApplicationResponse.fail(errorCode = ErrorCode.COMMON_SERVER_ERROR)
return ApplicationResponse.fail(errorCode = ErrorCode.COMMON_SERVER_ERROR).build()
}
/**
@@ -31,8 +31,8 @@ class BasicControllerAdvice {
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(BusinessException::class)
fun handleBusinessException(e: BusinessException): ApplicationResponse {
fun handleBusinessException(e: BusinessException): ApplicationResponse<Nothing> {
logger.error { "[${BusinessException::class.simpleName}] <ErrorCode>: ${e.errorCode.name}, <ErrorMessage>: ${e.message}" }
return ApplicationResponse.fail(errorCode = e.errorCode)
return ApplicationResponse.fail(errorCode = e.errorCode).build()
}
}

View File

@@ -4,47 +4,46 @@ import com.fasterxml.jackson.annotation.JsonInclude
import io.beaniejoy.dongnecafe.common.error.constant.ErrorCode
@JsonInclude(JsonInclude.Include.NON_NULL)
class ApplicationResponse {
var result: ResultCode
private set
var data: Any? = null
private set
var message: String?
private set
var errorCode: String? = null
private set
constructor(resultCode: ResultCode, message: String?) {
this.result = resultCode
this.message = message
}
constructor(resultCode: ResultCode, errorCode: ErrorCode, message: String?) {
this.result = resultCode
this.errorCode = errorCode.name
this.message = message
}
class ApplicationResponse<T>(
val result: ResultCode,
val message: String? = null,
val errorCode: String? = null,
val data: T? = null
) {
companion object {
fun success(message: String? = null): ApplicationResponse {
return ApplicationResponse(resultCode = ResultCode.SUCCESS, message = message)
}
fun fail(errorCode: ErrorCode, message: String? = null): ApplicationResponse {
return ApplicationResponse(
resultCode = ResultCode.FAIL,
errorCode = errorCode,
fun success(message: String? = null): ApplicationResponseBuilder {
return ApplicationResponseBuilder(
result = ResultCode.SUCCESS,
message = message
)
}
fun fail(errorCode: ErrorCode, message: String? = null): ApplicationResponseBuilder {
return ApplicationResponseBuilder(
result = ResultCode.FAIL,
message = message,
errorCode = errorCode.name
)
}
}
}
class ApplicationResponseBuilder(
var result: ResultCode,
var message: String? = null,
var errorCode: String? = null
) {
fun build(): ApplicationResponse<Nothing> {
return data(null)
}
fun data(data: Any): ApplicationResponse {
this.data = data
return this
fun <T> data(data: T?): ApplicationResponse<T> {
return ApplicationResponse(
result = this.result,
message = this.message,
data = data,
errorCode = errorCode
)
}
}

View File

@@ -24,6 +24,7 @@ class SecurityConfig {
.csrf().disable()
.formLogin().disable()
// FIXME 임시 permitAll 설정
.authorizeRequests()
.anyRequest().permitAll()

View File

@@ -3,7 +3,10 @@ package io.beaniejoy.dongnecafe.domain.cafe.controller
import io.beaniejoy.dongnecafe.common.response.ApplicationResponse
import io.beaniejoy.dongnecafe.domain.cafe.model.request.CafeRegisterRequest
import io.beaniejoy.dongnecafe.domain.cafe.model.request.CafeUpdateRequest
import io.beaniejoy.dongnecafe.domain.cafe.model.response.CafeDetailedInfo
import io.beaniejoy.dongnecafe.domain.cafe.model.response.CafeSearchInfo
import io.beaniejoy.dongnecafe.domain.cafe.service.CafeService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
@@ -18,7 +21,7 @@ class CafeController(
* 신규 카페 생성
*/
@PostMapping
fun createNewCafe(@RequestBody resource: CafeRegisterRequest): ApplicationResponse {
fun createNewCafe(@RequestBody resource: CafeRegisterRequest): ApplicationResponse<Long> {
val newCafeId = cafeService.createNew(
name = resource.name!!,
address = resource.address!!,
@@ -38,7 +41,7 @@ class CafeController(
@GetMapping
fun searchCafeList(
@PageableDefault(sort = ["name"], direction = Sort.Direction.ASC, page = 0, size = 10) pageable: Pageable
): ApplicationResponse {
): ApplicationResponse<Page<CafeSearchInfo>> {
val searchCafes = cafeService.searchCafeList(pageable)
return ApplicationResponse
@@ -50,7 +53,7 @@ class CafeController(
* 단일 카페 상세 조회
*/
@GetMapping("/{id}")
fun getDetailedInfo(@PathVariable("id") id: Long): ApplicationResponse {
fun getDetailedInfo(@PathVariable("id") id: Long): ApplicationResponse<CafeDetailedInfo> {
val cafeDetailedInfo = cafeService.getDetailedInfoByCafeId(id)
return ApplicationResponse
@@ -66,7 +69,7 @@ class CafeController(
fun updateInfo(
@PathVariable("id") id: Long,
@RequestBody resource: CafeUpdateRequest
): ApplicationResponse {
): ApplicationResponse<Nothing> {
cafeService.updateInfo(
id = id,
name = resource.name!!,
@@ -75,6 +78,8 @@ class CafeController(
description = resource.description!!
)
return ApplicationResponse.success("Successfully Cafe[$id] Info Updated")
return ApplicationResponse
.success("Successfully Cafe[$id] Info Updated")
.build()
}
}

View File

@@ -3,6 +3,7 @@ package io.beaniejoy.dongnecafe.domain.cafe.controller
import io.beaniejoy.dongnecafe.common.response.ApplicationResponse
import io.beaniejoy.dongnecafe.domain.cafe.model.request.CafeMenuBulkDeleteRequest
import io.beaniejoy.dongnecafe.domain.cafe.model.request.CafeMenuUpdateRequest
import io.beaniejoy.dongnecafe.domain.cafe.model.response.CafeMenuDetailedInfo
import io.beaniejoy.dongnecafe.domain.cafe.service.CafeMenuService
import org.springframework.web.bind.annotation.*
@@ -18,7 +19,7 @@ class CafeMenuController(
fun getDetailedInfo(
@PathVariable("cafeId") cafeId: Long,
@PathVariable("menuId") menuId: Long
): ApplicationResponse {
): ApplicationResponse<CafeMenuDetailedInfo> {
val cafeMenuDetailedInfo = cafeMenuService.getDetailedInfoByMenuId(
menuId = menuId,
cafeId = cafeId
@@ -40,14 +41,16 @@ class CafeMenuController(
@PathVariable("cafeId") cafeId: Long,
@PathVariable("menuId") menuId: Long,
@RequestBody cafeMenuUpdateRequest: CafeMenuUpdateRequest
): ApplicationResponse {
): ApplicationResponse<Nothing> {
cafeMenuService.updateInfoAndBulkUpdate(
menuId = menuId,
cafeId = cafeId,
resource = cafeMenuUpdateRequest
)
return ApplicationResponse.success("Success Update Cafe[$cafeId]'s CafeMenu[$menuId]")
return ApplicationResponse
.success("Success Update Cafe[$cafeId]'s CafeMenu[$menuId]")
.build()
}
/**
@@ -57,13 +60,15 @@ class CafeMenuController(
fun delete(
@PathVariable("cafeId") cafeId: Long,
@PathVariable("menuId") menuId: Long
): ApplicationResponse {
): ApplicationResponse<Nothing> {
cafeMenuService.deleteByCafeMenuId(
menuId = menuId,
cafeId = cafeId
)
return ApplicationResponse.success("Success Delete Cafe[$cafeId]'s CafeMenu[$menuId]")
return ApplicationResponse
.success("Success Delete Cafe[$cafeId]'s CafeMenu[$menuId]")
.build()
}
/**
@@ -73,9 +78,11 @@ class CafeMenuController(
fun bulkDelete(
@PathVariable("cafeId") cafeId: Long,
@RequestBody resource: CafeMenuBulkDeleteRequest
): ApplicationResponse {
): ApplicationResponse<Nothing> {
cafeMenuService.bulkDelete(cafeId, resource.cafeMenuIdList)
return ApplicationResponse.success("Success Delete Cafe[$cafeId]'s CafeMenu List")
return ApplicationResponse
.success("Success Delete Cafe[$cafeId]'s CafeMenu List")
.build()
}
}