Skip to content

Commit

Permalink
GroupDetailDto를 사용하는 API의 응답에 isMember 필드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
gidskql6671 committed May 29, 2024
1 parent 61df48e commit 2ab3d81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ data class GroupDetailDto(
@Schema(description = "문제를 풀지 않은 날 매겨지는 벌금", nullable = false, example = "1000")
val fine: Int,

@Schema(description = "해당 그룹에 속해있는지 여부", nullable = false, example = "false")
val isMember: Boolean,

@Schema(description = "그룹에 속한 유저", nullable = false)
val users: List<UserDto>,

@Schema(description = "그룹장", nullable = false)
val owner: UserDto
)

fun Group.toGroupDetailDto() =
fun Group.toGroupDetailDto(isMember: Boolean) =
GroupDetailDto(
id!!,
name,
isPrivate,
inviteCode,
goalSolveCount,
fine,
users.map { it.user.toUserDto() },
owner.toUserDto()
id = id!!,
name = name,
isPrivate = isPrivate,
inviteCode = inviteCode,
goalSolveCount = goalSolveCount,
fine = fine,
isMember = isMember,
users = users.map { it.user.toUserDto() },
owner = owner.toUserDto()
)

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class GroupService (

fun getGroupDetail(user: User, groupId: Long): GroupDetailDto {
val group = groupRepository.findById(groupId).orElseThrow{ NotFoundException(message = "해당 그룹이 없습니다.") }
val isMember = containGroupRepository.existsByGroupAndUser(group, user)

if (!group.isPrivate || containGroupRepository.existsByGroupAndUser(group, user)) {
return group.toGroupDetailDto()
if (!group.isPrivate || isMember) {
return group.toGroupDetailDto(isMember = isMember)
}

throw ForbiddenException(message = "해당 비밀 그룹에 속해있지 않습니다.")
Expand All @@ -71,7 +72,7 @@ class GroupService (

createdGroup.users.add(createdCG)

return group.toGroupDetailDto()
return group.toGroupDetailDto(isMember = true)
}

@Transactional
Expand All @@ -95,7 +96,7 @@ class GroupService (

val updatedGroup = groupRepository.save(existingGroup)

return updatedGroup.toGroupDetailDto()
return updatedGroup.toGroupDetailDto(isMember = true)
}

@Transactional
Expand All @@ -109,7 +110,7 @@ class GroupService (
if (!group.isPrivate) {
containGroupRepository.save(ContainGroup(user, group))

return group.toGroupDetailDto()
return group.toGroupDetailDto(isMember = true)
}

if (password == null) {
Expand All @@ -121,7 +122,7 @@ class GroupService (
if (isCorrectPW) {
containGroupRepository.save(ContainGroup(user, group))

return group.toGroupDetailDto()
return group.toGroupDetailDto(isMember = true)
}

throw ConflictException("incorrect_password", "비밀번호가 다릅니다.")
Expand All @@ -139,7 +140,7 @@ class GroupService (

containGroupRepository.save(ContainGroup(user, group))

return group.toGroupDetailDto()
return group.toGroupDetailDto(isMember = true)
}

@Transactional
Expand Down

0 comments on commit 2ab3d81

Please sign in to comment.