Skip to content

Commit

Permalink
feat: leave guild api를 추가한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Dec 23, 2024
1 parent 673caa8 commit debf5b1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/app/LeaveGuildFacade.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gitanimals.guild.app

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

@Component
class LeaveGuildFacade(
private val identityApi: IdentityApi,
private val guildService: GuildService,
) {

fun leave(token: String, guildId: Long) {
val user = identityApi.getUserByToken(token)

guildService.leave(guildId, user.id.toLong())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GuildController(
private val joinedGuildFacade: GetJoinedGuildFacade,
private val searchGuildFacade: SearchGuildFacade,
private val changeMainPersonaFacade: ChangeMainPersonaFacade,
private val leaveGuildFacade: LeaveGuildFacade,
) {

@ResponseStatus(HttpStatus.OK)
Expand Down Expand Up @@ -128,4 +129,10 @@ class GuildController(
guildId = guildId,
personaId = personaId,
)

@DeleteMapping("/guilds/{guildId}/leave")
fun leaveGuild(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
@PathVariable("guildId") guildId: Long,
) = leaveGuildFacade.leave(token, guildId)
}
8 changes: 8 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/domain/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ class Guild(
}
}

fun leave(userId: Long) {
require(userId != leader.userId) {
"Leader cannot leave guild guildId: \"$id\", userId: \"$userId\""
}

members.removeIf { it.userId == userId }
}

companion object {

fun create(
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/domain/GuildService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.hibernate.Hibernate
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.orm.ObjectOptimisticLockingFailureException
import org.springframework.retry.annotation.Retryable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand Down Expand Up @@ -43,6 +45,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun joinGuild(
guildId: Long,
memberUserId: Long,
Expand All @@ -63,6 +66,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun acceptJoin(acceptorId: Long, guildId: Long, acceptUserId: Long) {
val guild = guildRepository.findGuildByIdAndLeaderId(guildId, acceptorId)
?: throw IllegalArgumentException("Cannot accept join cause your not a leader.")
Expand All @@ -71,6 +75,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun denyJoin(denierId: Long, guildId: Long, denyUserId: Long) {
val guild = guildRepository.findGuildByIdAndLeaderId(guildId, denierId)
?: throw IllegalArgumentException("Cannot deny join cause your not a leader.")
Expand All @@ -79,6 +84,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun kickMember(kickerId: Long, guildId: Long, kickUserId: Long) {
val guild = guildRepository.findGuildByIdAndLeaderId(guildId, kickerId)
?: throw IllegalArgumentException("Cannot kick member cause your not a leader.")
Expand All @@ -87,6 +93,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun changeGuild(changeRequesterId: Long, guildId: Long, request: ChangeGuildRequest) {
val guild = guildRepository.findGuildByIdAndLeaderId(guildId, changeRequesterId)
?: throw IllegalArgumentException("Cannot kick member cause your not a leader.")
Expand All @@ -95,12 +102,21 @@ class GuildService(
}

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

guild.changeMainPersona(userId, personaId, personaType)
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun leave(guildId: Long, userId: Long) {
val guild = getGuildById(guildId)

guild.leave(userId)
}

fun getGuildById(id: Long, vararg lazyLoading: (Guild) -> Unit): Guild {
val guild = guildRepository.findByIdOrNull(id)
?: throw IllegalArgumentException("Cannot fint guild by id \"$id\"")
Expand All @@ -110,6 +126,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun updateContribution(username: String, contributions: Long) {
val guilds = guildRepository.findAllGuildByUsernameWithMembers(username)

Expand Down Expand Up @@ -138,6 +155,7 @@ class GuildService(
}

@Transactional
@Retryable(ObjectOptimisticLockingFailureException::class)
fun deletePersonaSync(
userId: Long,
deletedPersonaId: Long,
Expand Down

0 comments on commit debf5b1

Please sign in to comment.