Skip to content

Commit

Permalink
feat: 대표 펫 변경 api를 개발한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Dec 23, 2024
1 parent 9c7b5dd commit 673caa8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.gitanimals.guild.app

import org.gitanimals.guild.domain.GuildService
import org.springframework.stereotype.Component

@Component
class ChangeMainPersonaFacade(
private val renderApi: RenderApi,
private val identityApi: IdentityApi,
private val guildService: GuildService,
) {

fun changeMainPersona(token: String, guildId: Long, personaId: Long) {
val user = identityApi.getUserByToken(token)
val personas = renderApi.getUserByName(user.username).personas

val changedPersona = personas.firstOrNull { it.id.toLong() == personaId }
?: throw IllegalArgumentException("Cannot change persona to \"$personaId\" from user \"${user.username}\"")

guildService.changeMainPersona(
guildId = guildId,
userId = user.id.toLong(),
personaId = changedPersona.id.toLong(),
personaType = changedPersona.type,
)
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/controller/GuildController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GuildController(
private val changeGuildFacade: ChangeGuildFacade,
private val joinedGuildFacade: GetJoinedGuildFacade,
private val searchGuildFacade: SearchGuildFacade,
private val changeMainPersonaFacade: ChangeMainPersonaFacade,
) {

@ResponseStatus(HttpStatus.OK)
Expand Down Expand Up @@ -116,4 +117,15 @@ class GuildController(
GuildIcons.entries.map { it.getImagePath() }.toList()
)
}

@PostMapping("/guilds/{guildId}/personas")
fun changeMainPersona(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
@PathVariable("guildId") guildId: Long,
@RequestParam("persona-id") personaId: Long,
) = changeMainPersonaFacade.changeMainPersona(
token = token,
guildId = guildId,
personaId = personaId,
)
}
15 changes: 15 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/domain/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ class Guild(
return leader.personaType
}

fun changeMainPersona(userId: Long, personaId: Long, personaType: String) {
if (leader.userId == userId) {
leader.personaId = personaId
leader.personaType = personaType
return
}

members.first {
it.userId == userId
}.run {
this.personaId = personaId
this.personaType = personaType
}
}

companion object {

fun create(
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/domain/GuildService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class GuildService(
guild.change(request)
}

@Transactional
fun changeMainPersona(guildId: Long, userId: Long, personaId: Long, personaType: String) {
val guild = getGuildById(guildId)

guild.changeMainPersona(userId, personaId, personaType)
}

fun getGuildById(id: Long, vararg lazyLoading: (Guild) -> Unit): Guild {
val guild = guildRepository.findByIdOrNull(id)
?: throw IllegalArgumentException("Cannot fint guild by id \"$id\"")
Expand Down

0 comments on commit 673caa8

Please sign in to comment.