Skip to content

Commit 269a0f8

Browse files
committed
feat: 진화가능한 펫인지 확인하는 API를 추가한다
1 parent 6642ff4 commit 269a0f8

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

src/main/kotlin/org/gitanimals/render/app/PersonaEvolutionFacade.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ class PersonaEvolutionFacade(
1818
personaId = personaId,
1919
)
2020
}
21+
22+
fun isEvoluationable(token: String, personaId: Long): Boolean {
23+
val user = identityApi.getUserByToken(token)
24+
25+
return userService.isEvoluationable(
26+
name = user.username,
27+
personaId = personaId,
28+
)
29+
}
2130
}

src/main/kotlin/org/gitanimals/render/controller/PersonaEvolutionController.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.gitanimals.render.controller
33
import org.gitanimals.core.auth.RequiredUserEntryPoints
44
import org.gitanimals.core.auth.UserEntryPoint
55
import org.gitanimals.render.app.PersonaEvolutionFacade
6+
import org.gitanimals.render.controller.response.PersonaEvolutionableResponse
67
import org.gitanimals.render.controller.response.PersonaResponse
78
import org.springframework.http.HttpHeaders
89
import org.springframework.http.HttpStatus
@@ -27,4 +28,19 @@ class PersonaEvolutionController(
2728
)
2829
)
2930
}
31+
32+
@GetMapping("/personas/{personaId}/evolution")
33+
@ResponseStatus(HttpStatus.OK)
34+
@RequiredUserEntryPoints([UserEntryPoint.GITHUB])
35+
fun isEvolutionablePersona(
36+
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
37+
@PathVariable personaId: Long,
38+
): PersonaEvolutionableResponse {
39+
return PersonaEvolutionableResponse(
40+
evolutionAble = personaEvolutionFacade.isEvoluationable(
41+
token = token,
42+
personaId = personaId,
43+
)
44+
)
45+
}
3046
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.gitanimals.render.controller.response
2+
3+
data class PersonaEvolutionableResponse(
4+
val evolutionAble: Boolean
5+
)

src/main/kotlin/org/gitanimals/render/domain/User.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,30 @@ class User(
345345
it.id == personaId
346346
} ?: throw IllegalArgumentException("Cannot evolution persona cause cannot find matched persona by id: \"$personaId\"")
347347

348+
require(persona.level() >= EVOLUTION_REQUIRED_LEVEL) {
349+
"Cannot evolution persona cause, ${persona.level()} is not enough level for evolution."
350+
}
351+
348352
persona.evolution()
349353
return persona
350354
}
351355

356+
fun isEvolutionable(personaId: Long): Boolean {
357+
val persona = personas.firstOrNull {
358+
it.id == personaId
359+
} ?: throw IllegalArgumentException("Cannot evolution persona cause cannot find matched persona by id: \"$personaId\"")
360+
361+
return persona.level() >= EVOLUTION_REQUIRED_LEVEL && persona.getType().personaEvolution != PersonaEvolution.nothing
362+
}
363+
352364
companion object {
353365
private const val MAX_PERSONA_COUNT = 30L
354366
private const val MAX_INIT_PERSONA_COUNT = 10L
355367
private const val FOR_NEW_PERSONA_COUNT = 30L
356368
private const val FOR_INIT_PERSONA_COUNT = 100L
357369

370+
private const val EVOLUTION_REQUIRED_LEVEL = 100L
371+
358372
private val nameConvention = Regex("[^a-zA-Z0-9-]")
359373

360374
private val logger = LoggerFactory.getLogger(this::class.simpleName)

src/main/kotlin/org/gitanimals/render/domain/UserService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ class UserService(
214214
return PersonaResponse.from(persona = evolutionedPersona)
215215
}
216216

217+
@Transactional(readOnly = true)
218+
fun isEvoluationable(name: String, personaId: Long): Boolean {
219+
val user = getUserByName(name)
220+
221+
return user.isEvolutionable(
222+
personaId = personaId
223+
)
224+
}
225+
217226
private fun getUserById(id: Long): User {
218227
return userRepository.findByIdOrNull(id)
219228
?: throw IllegalArgumentException("Cannot find user by id : \"$id\"")

src/test/kotlin/org/gitanimals/render/domain/UserServiceTest.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal class UserServiceTest(
5252
id = personaId,
5353
idempotencyKey = IdGenerator.generate().toString(),
5454
personaType = PersonaType.RABBIT.name,
55-
level = 0,
55+
level = 100,
5656
)
5757

5858
it("진화를 성공한다.") {
@@ -73,7 +73,7 @@ internal class UserServiceTest(
7373
id = personaId,
7474
idempotencyKey = IdGenerator.generate().toString(),
7575
personaType = PersonaType.CAT.name,
76-
level = 0,
76+
level = 100,
7777
)
7878

7979
it("진화를 실패한다.") {
@@ -87,6 +87,29 @@ internal class UserServiceTest(
8787
result.message!!.contains("Evolution fail cause, not support evolution type") shouldBe true
8888
}
8989
}
90+
91+
context("name과, 진화타입이 nothing이 아니지만, 레벨이 부족한 펫의 personaId를 받으면,") {
92+
val user = userRepository.save(user)
93+
val personaId = IdGenerator.generate()
94+
userService.addPersona(
95+
name = user.getName(),
96+
id = personaId,
97+
idempotencyKey = IdGenerator.generate().toString(),
98+
personaType = PersonaType.CAT.name,
99+
level = 99,
100+
)
101+
102+
it("진화를 실패한다.") {
103+
val result = shouldThrowExactly<IllegalArgumentException> {
104+
userService.evolutionPersona(
105+
name = user.getName(),
106+
personaId = personaId,
107+
)
108+
}
109+
110+
result.message!!.contains("Cannot evolution persona cause") shouldBe true
111+
}
112+
}
90113
}
91114
}) {
92115

0 commit comments

Comments
 (0)