-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Change my background | ||
|
||
배경을 변경합니다 | ||
|
||
## Request | ||
### HTTP METHOD : `PUT` | ||
|
||
### url : `https://render.gitanimals.org/users/backgrounds` | ||
|
||
### PathVariable | ||
- {username}: 사용자의 이름 | ||
|
||
### RequestHeader | ||
- Authorization: `{token}` | ||
|
||
## Request | ||
|
||
```json | ||
{ | ||
"type": "SNOWY" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## Get my background | ||
|
||
내가 갖고있는 모든 배경을 조회합니다. | ||
|
||
## Request | ||
### HTTP METHOD : `GET` | ||
|
||
### url : `https://render.gitanimals.org/users/{username}/backgrounds` | ||
|
||
### PathVariable | ||
- {username}: 사용자의 이름 | ||
|
||
## Response | ||
|
||
```json | ||
{ | ||
"id": "1", | ||
"name": "devxb", | ||
"backgrounds": [ | ||
{ | ||
"type": "SNOWY" | ||
}, | ||
{ | ||
"type": "WHITE" | ||
} | ||
... | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Buy background | ||
|
||
배경화면을 구매합니다. | ||
|
||
## Request | ||
|
||
### HTTP METHOD : `POST` | ||
|
||
### url : `https://api.gitanimals.org/shops/backgrounds` | ||
|
||
### RequestHeader | ||
|
||
- Authorization: `{token}` | ||
|
||
### Request Body | ||
|
||
200 OK | ||
|
||
```json | ||
{ | ||
"type": "SNOWY" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## Get background | ||
|
||
모든 구매가능한 배경을 조회합니다. | ||
|
||
## Request | ||
|
||
### HTTP METHOD : `GET` | ||
|
||
### url : `https://api.gitanimals.org/shops/backgrounds` | ||
|
||
### Response Body | ||
|
||
200 OK | ||
|
||
```json | ||
{ | ||
"backgrounds": [ | ||
{ | ||
"type": "SHOP", | ||
"price": "100000" | ||
}, | ||
{ | ||
"type": "SNOWY", | ||
"price": "100000" | ||
}, | ||
... | ||
] | ||
} | ||
``` |
72 changes: 72 additions & 0 deletions
72
src/main/kotlin/org/gitanimals/shop/app/BuyBackgroundFacade.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.gitanimals.shop.app | ||
|
||
import org.gitanimals.shop.domain.SaleService | ||
import org.gitanimals.shop.domain.SaleType | ||
import org.rooftop.netx.api.Orchestrator | ||
import org.rooftop.netx.api.OrchestratorFactory | ||
import org.springframework.stereotype.Service | ||
import java.util.* | ||
|
||
@Service | ||
class BuyBackgroundFacade( | ||
orchestratorFactory: OrchestratorFactory, | ||
identityApi: IdentityApi, | ||
renderApi: RenderApi, | ||
|
||
private val saleService: SaleService, | ||
) { | ||
|
||
private lateinit var backgroundBuyOrchestrator: Orchestrator<String, Unit> | ||
|
||
fun buyBackground(token: String, item: String) { | ||
backgroundBuyOrchestrator.sagaSync( | ||
item, | ||
mapOf("token" to token, "idempotencyKey" to UUID.randomUUID().toString()) | ||
).decodeResultOrThrow(Unit::class) | ||
} | ||
|
||
init { | ||
backgroundBuyOrchestrator = orchestratorFactory.create<String>("buy background facade") | ||
.start({ | ||
val sale = saleService.getByTypeAndItem(SaleType.BACKGROUND, it) | ||
|
||
require(sale.getCount() > 0) { | ||
"Cannot buy item : \"${sale.type}\" cause its count : \"${sale.getCount()}\" == 0" | ||
} | ||
|
||
sale | ||
}) | ||
.joinWithContext( | ||
contextOrchestrate = { context, sale -> | ||
val token = context.decodeContext("token", String::class) | ||
val idempotencyKey = context.decodeContext("idempotencyKey", String::class) | ||
identityApi.decreasePoint(token, idempotencyKey, sale.price.toString()) | ||
|
||
sale | ||
}, | ||
contextRollback = { context, sale -> | ||
val token = context.decodeContext("token", String::class) | ||
val idempotencyKey = context.decodeContext("idempotencyKey", String::class) | ||
identityApi.increasePoint(token, idempotencyKey, sale.price.toString()) | ||
} | ||
) | ||
.joinWithContext( | ||
contextOrchestrate = { context, sale -> | ||
val token = context.decodeContext("token", String::class) | ||
val idempotencyKey = context.decodeContext("idempotencyKey", String::class) | ||
|
||
renderApi.addBackground(token, idempotencyKey, sale.item) | ||
sale | ||
}, | ||
contextRollback = { context, sale -> | ||
val token = context.decodeContext("token", String::class) | ||
val idempotencyKey = context.decodeContext("idempotencyKey", String::class) | ||
|
||
renderApi.deleteBackground(token, idempotencyKey, sale.item) | ||
} | ||
) | ||
.commit { sale -> | ||
saleService.buyBySaleTypeAndItem(sale.type, sale.item) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/org/gitanimals/shop/controller/BuySaleController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.gitanimals.shop.controller | ||
|
||
import org.gitanimals.shop.app.BuyBackgroundFacade | ||
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.web.bind.annotation.* | ||
|
||
@RestController | ||
class BuySaleController( | ||
private val saleService: SaleService, | ||
private val buyBackgroundFacade: BuyBackgroundFacade, | ||
) { | ||
|
||
@GetMapping("/shops/backgrounds") | ||
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) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/org/gitanimals/shop/controller/request/BuyBackgroundRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.gitanimals.shop.controller.request | ||
|
||
data class BuyBackgroundRequest( | ||
val type: String, | ||
) |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/org/gitanimals/shop/controller/response/BackgroundResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.gitanimals.shop.controller.response | ||
|
||
import org.gitanimals.shop.domain.Sale | ||
|
||
data class BackgroundResponse( | ||
val backgrounds: List<Background>, | ||
) { | ||
|
||
data class Background( | ||
val type: String, | ||
val price: String, | ||
) | ||
|
||
companion object { | ||
fun from(sales: List<Sale>): BackgroundResponse { | ||
return BackgroundResponse( | ||
sales.map { | ||
Background( | ||
type = it.item, | ||
price = it.price.toString(), | ||
) | ||
}.toList() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.gitanimals.shop.domain | ||
|
||
import jakarta.persistence.* | ||
import org.gitanimals.shop.core.AggregateRoot | ||
|
||
@AggregateRoot | ||
@Entity(name = "sale") | ||
@Table( | ||
name = "sale", indexes = [ | ||
Index(name = "sale_idx_type", columnList = "type", unique = true) | ||
] | ||
) | ||
class Sale( | ||
@Id | ||
@Column(name = "id") | ||
val id: Long, | ||
|
||
@Enumerated | ||
@Column(name = "type", nullable = false, unique = true) | ||
val type: SaleType, | ||
|
||
@Column(name = "item", nullable = false) | ||
val item: String, | ||
|
||
@Column(name = "price", nullable = false) | ||
val price: Long, | ||
|
||
@Column(name = "count", nullable = false) | ||
private var count: Long, | ||
|
||
@Version | ||
private var version: Long? = null, | ||
) { | ||
|
||
fun getCount(): Long = this.count | ||
|
||
fun buy() { | ||
require(this.count > 0) { | ||
"Cannot buy item : \"$type\" cause its count : \"$count\" == 0" | ||
} | ||
|
||
this.count -= 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.gitanimals.shop.domain | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface SaleRepository : JpaRepository<Sale, Long> { | ||
fun getByItem(item: String): Sale | ||
|
||
fun findAllByType(saleType: SaleType): List<Sale> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.gitanimals.shop.domain | ||
|
||
import org.springframework.orm.ObjectOptimisticLockingFailureException | ||
import org.springframework.retry.annotation.Retryable | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
class SaleService( | ||
private val saleRepository: SaleRepository, | ||
) { | ||
|
||
fun findAllByType(saleType: SaleType): List<Sale> = saleRepository.findAllByType(saleType) | ||
|
||
@Transactional | ||
@Retryable(ObjectOptimisticLockingFailureException::class) | ||
fun buyBySaleTypeAndItem(saleType: SaleType, item: String) { | ||
val sale = getByTypeAndItem(saleType, item) | ||
|
||
sale.buy() | ||
} | ||
|
||
fun getByTypeAndItem(saleType: SaleType, item: String): Sale { | ||
val sale = saleRepository.getByItem(item) | ||
|
||
require(sale.type == saleType) { | ||
"Cannot find sale by type: \"$saleType\" and item: \"$item\"" | ||
} | ||
|
||
return sale | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.gitanimals.shop.domain | ||
|
||
enum class SaleType { | ||
|
||
BACKGROUND, | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters