Skip to content

Commit

Permalink
release: 0.7.5 (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Sep 8, 2024
2 parents 01b2cd2 + a57efae commit 329f56d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/kotlin/org/gitanimals/render/app/UserFacade.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gitanimals.render.app

import org.gitanimals.render.app.request.MergePersonaRequest
import org.gitanimals.render.domain.UserService
import org.gitanimals.render.domain.request.PersonaChangeRequest
import org.gitanimals.render.domain.response.PersonaResponse
Expand Down Expand Up @@ -40,4 +41,14 @@ class UserFacade(

return userService.getPersona(user.username, personaId)
}

fun mergePersona(token: String, request: MergePersonaRequest) {
val user = identityApi.getUserByToken(token)

return userService.mergePersona(
user.id.toLong(),
request.increasePersonaId,
request.deletePersonaId,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.gitanimals.render.app.request

data class MergePersonaRequest(
val increasePersonaId: Long,
val deletePersonaId: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.gitanimals.render.controller

import org.gitanimals.render.app.AuthorizationException
import org.gitanimals.render.app.UserFacade
import org.gitanimals.render.app.request.MergePersonaRequest
import org.gitanimals.render.controller.request.AddMultiplyPersonaRequest
import org.gitanimals.render.controller.request.AddPersonaRequest
import org.gitanimals.render.controller.response.ErrorResponse
Expand Down Expand Up @@ -72,6 +73,13 @@ class PersonaController(
)
}

@PutMapping("/personas/merges")
@ResponseStatus(HttpStatus.OK)
fun mergePersona(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
@RequestBody meregPersonaRequest: MergePersonaRequest,
) = userFacade.mergePersona(token, meregPersonaRequest)

@ExceptionHandler(IllegalArgumentException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse =
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/org/gitanimals/render/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ class User(
.append("</svg>")
.toString()

fun mergePersona(increasePersonaId: Long, deletePersonaId: Long) {
val increasePersona = personas.first { it.id == increasePersonaId }
val deletePersona = personas.first { it.id == deletePersonaId }

increasePersona.level.value += deletePersona.level.value

personas.remove(deletePersona)
}

companion object {
private const val MAX_PERSONA_COUNT = 30L
private const val MAX_INIT_PERSONA_COUNT = 10L
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/org/gitanimals/render/domain/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ class UserService(
return user.deletePersona(personaId)
}

@Transactional
@Retryable(retryFor = [ObjectOptimisticLockingFailureException::class], maxAttempts = 100)
fun mergePersona(id: Long, increasePersonaId: Long, deletePersonaId: Long) {
val user = userRepository.findByIdOrNull(id)
?: throw IllegalArgumentException("Cannot find user by id \"$id\"")

user.mergePersona(increasePersonaId, deletePersonaId)
}

fun getPersona(name: String, personaId: Long): PersonaResponse {
return getUserByName(name).personas
.find { it.id == personaId }
Expand Down
16 changes: 16 additions & 0 deletions src/test/kotlin/org/gitanimals/render/domain/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ internal class UserTest : DescribeSpec({
}
}
}

describe("mergePersona 메소드는") {
context("increasePersonaId와 deletePersonaId를 받아서,") {
val user = User.newUser("devxb", mapOf())
user.updateContribution(30)
user.giveNewPersona()

val increasePersonaId = user.personas.minByOrNull { it.level.value }!!.id
val deletePersonaId = user.personas.maxByOrNull { it.level.value }!!.id
it("increasePersonaId에 deletePersonasId의 level을 추가하고, deletePersona를 삭제한다") {
user.mergePersona(increasePersonaId, deletePersonaId)

user.personas.size shouldBeEqual 1
}
}
}
}) {
private companion object {
private const val ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down

0 comments on commit 329f56d

Please sign in to comment.