From bb05e602e380e39043fe1a7967995e32c42d7716 Mon Sep 17 00:00:00 2001 From: gidskql6671 Date: Sun, 26 May 2024 02:46:32 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B4=88=EB=8C=80=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=9C=20=EA=B7=B8=EB=A3=B9=20?= =?UTF-8?q?=EC=B0=B8=EC=97=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GroupController.kt | 31 +++++++++++++++++++ .../dto/JoinGroupWithInviteCodeRequest.kt | 9 ++++++ .../repository/GroupRepository.kt | 3 ++ .../onedayonebaek/service/GroupService.kt | 15 +++++++++ 4 files changed, 58 insertions(+) create mode 100644 back/src/main/kotlin/knu/dong/onedayonebaek/dto/JoinGroupWithInviteCodeRequest.kt diff --git a/back/src/main/kotlin/knu/dong/onedayonebaek/controller/GroupController.kt b/back/src/main/kotlin/knu/dong/onedayonebaek/controller/GroupController.kt index 5c988d2..58dc704 100644 --- a/back/src/main/kotlin/knu/dong/onedayonebaek/controller/GroupController.kt +++ b/back/src/main/kotlin/knu/dong/onedayonebaek/controller/GroupController.kt @@ -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 @@ -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 = "해당 그룹에서 나간다. 그룹장은 나갈 수 없다." diff --git a/back/src/main/kotlin/knu/dong/onedayonebaek/dto/JoinGroupWithInviteCodeRequest.kt b/back/src/main/kotlin/knu/dong/onedayonebaek/dto/JoinGroupWithInviteCodeRequest.kt new file mode 100644 index 0000000..48511da --- /dev/null +++ b/back/src/main/kotlin/knu/dong/onedayonebaek/dto/JoinGroupWithInviteCodeRequest.kt @@ -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 +) \ No newline at end of file diff --git a/back/src/main/kotlin/knu/dong/onedayonebaek/repository/GroupRepository.kt b/back/src/main/kotlin/knu/dong/onedayonebaek/repository/GroupRepository.kt index 288a0bb..36b86c4 100644 --- a/back/src/main/kotlin/knu/dong/onedayonebaek/repository/GroupRepository.kt +++ b/back/src/main/kotlin/knu/dong/onedayonebaek/repository/GroupRepository.kt @@ -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 { + + fun findByInviteCode(inviteCode: String): Optional } \ No newline at end of file diff --git a/back/src/main/kotlin/knu/dong/onedayonebaek/service/GroupService.kt b/back/src/main/kotlin/knu/dong/onedayonebaek/service/GroupService.kt index d6cfc58..4e5a1a2 100644 --- a/back/src/main/kotlin/knu/dong/onedayonebaek/service/GroupService.kt +++ b/back/src/main/kotlin/knu/dong/onedayonebaek/service/GroupService.kt @@ -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("해당 그룹이 없습니다.") }