Skip to content

Commit

Permalink
release: 0.7.0 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Oct 28, 2024
2 parents 44b6e03 + edc109e commit 53c9a73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.gitanimals.shop.controller

import org.gitanimals.shop.app.BuyBackgroundFacade
import org.gitanimals.shop.controller.response.ErrorResponse
import org.gitanimals.shop.controller.request.BuyBackgroundRequest
import org.gitanimals.shop.controller.response.BackgroundResponse
import org.gitanimals.shop.domain.SaleService
import org.gitanimals.shop.domain.SaleType
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
Expand All @@ -18,12 +20,15 @@ class BuySaleController(
fun getBackgrounds(): BackgroundResponse =
BackgroundResponse.from(saleService.findAllByType(SaleType.BACKGROUND))


@PostMapping("/shops/backgrounds")
fun buyBackground(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
@RequestBody buyBackgroundRequest: BuyBackgroundRequest
) {
buyBackgroundFacade.buyBackground(token, buyBackgroundRequest.type)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException::class)
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse = ErrorResponse.from(exception)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.gitanimals.shop.controller.response

data class ErrorResponse(
val message: String,
) {

companion object {
fun from(exception: Exception): ErrorResponse =
ErrorResponse(exception.message ?: exception.localizedMessage)
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/org/gitanimals/shop/infra/RestRenderApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class RestRenderApi(
.header(HttpHeaders.AUTHORIZATION, token)
.header("Internal-Secret", internalSecret)
.exchange { _, response ->
require(response.statusCode.is2xxSuccessful) { "Cannot add background by backgroundName: \"$backgroundName\"" }
if (response.statusCode.is4xxClientError) {
response.bodyTo(ErrorResponse::class.java)?.let {
throw IllegalArgumentException(it.message)
}
}
check(response.statusCode.is2xxSuccessful) { "Cannot add background by backgroundName: \"$backgroundName\"" }
}
}

Expand Down Expand Up @@ -138,4 +143,8 @@ class RestRenderApi(
val visible: Boolean,
val dropRate: String,
)

data class ErrorResponse(
val message: String,
)
}

0 comments on commit 53c9a73

Please sign in to comment.