Skip to content

Commit

Permalink
초대 코드를 사용한 그룹 참여 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
gidskql6671 committed May 25, 2024
1 parent 4e8b8e3 commit bb05e60
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import knu.dong.onedayonebaek.domain.User
import knu.dong.onedayonebaek.dto.CreateGroupRequest
import knu.dong.onedayonebaek.dto.GroupDetailDto
import knu.dong.onedayonebaek.dto.JoinGroupRequest
import knu.dong.onedayonebaek.dto.JoinGroupWithInviteCodeRequest
import knu.dong.onedayonebaek.exception.InvalidReqParamException
import knu.dong.onedayonebaek.exception.response.BadRequestResponse
import knu.dong.onedayonebaek.exception.response.ForbiddenResponse
Expand Down Expand Up @@ -118,6 +119,36 @@ class GroupController(
return groupService.joinGroup(user, groupId, requestDto.password)
}

@Operation(
summary = "스터디 그룹 참가(초대 코드 사용)",
description = "초대 코드를 사용하여 지정된 스터디 그룹을 참가한다."
)
@PostMapping("/invited")
@ApiResponses(
ApiResponse(responseCode = "200", description = "참가한 스터디 그룹의 상세 정보"),
ApiResponse(responseCode = "400", description = "잘못된 Request Parameter",
content = [Content(schema = Schema(implementation = BadRequestResponse::class))]),
ApiResponse(responseCode = "404", description = "존재하지 않는 스터디 그룹",
content = [Content(schema = Schema(implementation = NotFoundResponse::class))]),
ApiResponse(responseCode = "409", description = "- 해당 스터디 그룹에 이미 속해있음",
content = [Content(
examples = [
ExampleObject(
name = "이미 가입된 그룹",
value = "{\"code\": \"already_joined\", \"message\":\"이미 가입된 스터디 그룹입니다.\"}"
)
],
mediaType = MediaType.APPLICATION_JSON_VALUE)])
)
fun joinGroupWithInviteCode(
@Validated @RequestBody requestDto: JoinGroupWithInviteCodeRequest,
authentication: Authentication
): GroupDetailDto {
val user = authentication.principal as User

return groupService.joinGroupWithInviteCode(user, requestDto.inviteCode)
}

@Operation(
summary = "그룹 나가기",
description = "해당 그룹에서 나간다. 그룹장은 나갈 수 없다."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package knu.dong.onedayonebaek.dto

import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "초대 코드로 그룹 가입 요청 DTO")
data class JoinGroupWithInviteCodeRequest(
@Schema(description = "초대 코드", required = true, example = "q123we")
var inviteCode: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package knu.dong.onedayonebaek.repository

import knu.dong.onedayonebaek.domain.Group
import org.springframework.data.jpa.repository.JpaRepository
import java.util.*

interface GroupRepository: JpaRepository<Group, Long> {

fun findByInviteCode(inviteCode: String): Optional<Group>
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ class GroupService (
throw ConflictException("incorrect_password", "비밀번호가 다릅니다.")
}

@Transactional
fun joinGroupWithInviteCode(user: User, inviteCode: String): GroupDetailDto {
val group = groupRepository
.findByInviteCode(inviteCode)
.orElseThrow{ NotFoundException("해당 그룹이 없습니다.") }

if (containGroupRepository.existsByGroupAndUser(group, user)) {
throw ConflictException("already_joined", "이미 가입된 스터디 그룹입니다.")
}

containGroupRepository.save(ContainGroup(user, group))

return group.toGroupDetailDto()
}

@Transactional
fun leaveGroup(user: User, groupId: Long) {
val group = groupRepository.findById(groupId).orElseThrow{ NotFoundException("해당 그룹이 없습니다.") }
Expand Down

0 comments on commit bb05e60

Please sign in to comment.