From f501b88413fa244b69c17f25f0f9a4ca7e1a95ae Mon Sep 17 00:00:00 2001 From: gkwon Date: Mon, 4 Mar 2024 16:57:50 +0900 Subject: [PATCH 1/8] =?UTF-8?q?[FEAT]=20=EB=B0=A9=EB=82=98=EA=B0=80?= =?UTF-8?q?=EA=B8=B0=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/gg/data/party/UserRoom.java | 11 ++++++ .../java/gg/data/party/type/RoomType.java | 30 +++++++++++++--- .../user/room/controller/RoomController.java | 13 +++++++ .../response/LeaveRoomResponseDto.java | 10 ++++++ .../api/user/room/service/RoomService.java | 36 +++++++++++++++++++ .../java/gg/repo/party/RoomRepository.java | 2 ++ .../java/gg/utils/exception/ErrorCode.java | 5 ++- .../party/RoomNotFoundException.java | 14 ++++++++ .../exception/party/RoomNotOpenException.java | 14 ++++++++ .../party/RoomNotParticipantException.java | 14 ++++++++ 10 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/RoomNotFoundException.java create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/RoomNotOpenException.java create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/RoomNotParticipantException.java diff --git a/gg-data/src/main/java/gg/data/party/UserRoom.java b/gg-data/src/main/java/gg/data/party/UserRoom.java index a14b9a425..11c132b83 100644 --- a/gg-data/src/main/java/gg/data/party/UserRoom.java +++ b/gg-data/src/main/java/gg/data/party/UserRoom.java @@ -1,5 +1,8 @@ package gg.data.party; +import static gg.utils.exception.BusinessChecker.*; +import static gg.utils.exception.ErrorCode.*; + import java.time.LocalDateTime; import javax.persistence.Column; @@ -18,10 +21,13 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity +@SuperBuilder public class UserRoom extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -47,4 +53,9 @@ public UserRoom(User user, Room room, String randomNickname) { this.nickname = randomNickname; this.isExist = true; } + + public void updateIsExist(Boolean isExist) { + mustNotNull(isExist, NULL_POINT); + this.isExist = isExist; + } } diff --git a/gg-data/src/main/java/gg/data/party/type/RoomType.java b/gg-data/src/main/java/gg/data/party/type/RoomType.java index 2132a5381..238f63115 100644 --- a/gg-data/src/main/java/gg/data/party/type/RoomType.java +++ b/gg-data/src/main/java/gg/data/party/type/RoomType.java @@ -1,13 +1,35 @@ package gg.data.party.type; +import java.util.Locale; + +import com.fasterxml.jackson.annotation.JsonCreator; + import lombok.Getter; import lombok.RequiredArgsConstructor; @Getter @RequiredArgsConstructor public enum RoomType { - OPEN, // Ordinal 0 - START, // Ordinal 1 - FINISH, // Ordinal 2 - HIDDEN // Ordinal 3 + OPEN("open", "방 시작 전"), + START("live", "방 진행 중"), + FINISH("end", "방 종료"), + HIDDEN("end", "방 종료 후 가림"); + + private final String code; + private final String desc; + + @JsonCreator + public static RoomType getEnumFromValue(String value) { + if (value == null) { + return null; + } + for (RoomType e : values()) { + if (e.name().equals(value)) { + return e; + } else if (e.code.toUpperCase(Locale.ROOT).equals(value.toUpperCase(Locale.ROOT))) { + return e; + } + } + return null; + } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java index 48b3b6f6d..f19e4d231 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java @@ -3,12 +3,15 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import gg.party.api.user.room.controller.request.RoomCreateReqDto; +import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; import gg.party.api.user.room.controller.response.RoomListResDto; import gg.party.api.user.room.service.RoomService; import gg.pingpong.api.global.utils.argumentresolver.Login; @@ -64,4 +67,14 @@ public ResponseEntity myHistoryRoomList(@Parameter(hidden = true RoomListResDto roomListResDto = roomService.findOrderMyHistoryRoomList(user.getId()); return ResponseEntity.status(HttpStatus.OK).body(roomListResDto); } + + /** + * 참여한 방을 나가기 한다 + * @return 나간 사람의 닉네임 + */ + @PatchMapping("/party/rooms/{room_id}") + public ResponseEntity leaveRoom(@PathVariable Long room_id, + @Parameter(hidden = true) @Login UserDto user) { + return ResponseEntity.status(HttpStatus.OK).body(roomService.leaveRoom(room_id, user)); + } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java new file mode 100644 index 000000000..e42743260 --- /dev/null +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java @@ -0,0 +1,10 @@ +package gg.party.api.user.room.controller.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class LeaveRoomResponseDto { + private String nickname; +} diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java index 5832e1507..2464cc436 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java @@ -20,12 +20,17 @@ import gg.party.api.user.room.controller.request.RoomCreateReqDto; import gg.party.api.user.room.controller.response.RoomListResDto; import gg.party.api.user.room.controller.response.RoomResDto; +import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; import gg.pingpong.api.user.user.dto.UserDto; import gg.repo.party.CategoryRepository; import gg.repo.party.RoomRepository; import gg.repo.party.UserRoomRepository; import gg.repo.user.UserRepository; +import gg.utils.exception.ErrorCode; import gg.utils.exception.party.CategoryNotFoundException; +import gg.utils.exception.party.RoomNotFoundException; +import gg.utils.exception.party.RoomNotOpenException; +import gg.utils.exception.party.RoomNotParticipantException; import gg.utils.exception.user.UserNotFoundException; import lombok.RequiredArgsConstructor; @@ -123,4 +128,35 @@ public RoomListResDto findOrderMyHistoryRoomList(Long userId) { return new RoomListResDto(roomListResDto); } + + /** + *

유저가 방을 나간다

+ *

참가자가 방에 참가한 상태일때만 취소해 준다.

+ * @param roomId + * @param user 참여 유저(사용자 본인) + * @throws RoomNotFoundException 방 없음 || 방 입장자가 아님 + * @return + */ + @Transactional + public LeaveRoomResponseDto leaveRoom(Long roomId, UserDto user) { + Room targetRoom = roomRepository.findById(roomId) + .orElseThrow(RoomNotFoundException::new); + if (!targetRoom.getStatus().equals(RoomType.OPEN)) { + throw new RoomNotOpenException(); + } + + List userRoomList = userRoomRepository.findByUserId(user.getId()); + UserRoom targetUserRoom = userRoomList.stream() + .filter(tu -> (tu.getUser().getId().equals(roomId))) + .findAny() + .orElseThrow(() -> new RoomNotParticipantException(ErrorCode.ROOM_NOT_PARTICIPANT)); + + //룸에서 인원 수정 + targetRoom + //중간 테이블의 불리언 값 수정 + targetUserRoom.updateIsExist(Boolean.FALSE); + userRoomRepository.save(targetUserRoom); + + return new LeaveRoomResponseDto(targetUserRoom.getNickname()); + } } diff --git a/gg-repo/src/main/java/gg/repo/party/RoomRepository.java b/gg-repo/src/main/java/gg/repo/party/RoomRepository.java index b8048a7e2..c768cd251 100644 --- a/gg-repo/src/main/java/gg/repo/party/RoomRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/RoomRepository.java @@ -10,4 +10,6 @@ public interface RoomRepository extends JpaRepository { List findByStatus(RoomType status, Sort sort); + + Long findById(Long id, Sort sort); } diff --git a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java index 92a322c04..b32241951 100644 --- a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java +++ b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java @@ -154,7 +154,10 @@ public enum ErrorCode { TOURNAMENT_IS_BEFORE(403, "TN017", "before인 토너먼트에서 점수 수정할 수 없습니다."), // PARTY - CATEGORY_NOT_FOUND(404, "PT101", "유효하지 않은 카테고리 입니다."); + CATEGORY_NOT_FOUND(404, "PT101", "유효하지 않은 카테고리 입니다."), + ROOM_NOT_FOUND(404, "PT001", "해당 방이 존재하지 않습니다."), + ROOM_NOT_PARTICIPANT(400, "PT002", "해당 방의 참여자가 아닙니다."), + ROOM_NOT_OPEN(400, "PT003", "해당 방의 참여자가 아닙니다."); private final int status; private final String errCode; diff --git a/gg-utils/src/main/java/gg/utils/exception/party/RoomNotFoundException.java b/gg-utils/src/main/java/gg/utils/exception/party/RoomNotFoundException.java new file mode 100644 index 000000000..a3d453f9b --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/RoomNotFoundException.java @@ -0,0 +1,14 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.NotExistException; + +public class RoomNotFoundException extends NotExistException { + public RoomNotFoundException() { + super(ErrorCode.ROOM_NOT_FOUND.getMessage(), ErrorCode.ROOM_NOT_FOUND); + } + + public RoomNotFoundException(ErrorCode errorCode) { + super(errorCode.getMessage(), errorCode); + } +} diff --git a/gg-utils/src/main/java/gg/utils/exception/party/RoomNotOpenException.java b/gg-utils/src/main/java/gg/utils/exception/party/RoomNotOpenException.java new file mode 100644 index 000000000..38c9ff0ab --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/RoomNotOpenException.java @@ -0,0 +1,14 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.NotExistException; + +public class RoomNotOpenException extends NotExistException { + public RoomNotOpenException() { + super(ErrorCode.ROOM_NOT_OPEN.getMessage(), ErrorCode.ROOM_NOT_OPEN); + } + + public RoomNotOpenException(ErrorCode errorCode) { + super(errorCode.getMessage(), errorCode); + } +} diff --git a/gg-utils/src/main/java/gg/utils/exception/party/RoomNotParticipantException.java b/gg-utils/src/main/java/gg/utils/exception/party/RoomNotParticipantException.java new file mode 100644 index 000000000..e1d7563a6 --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/RoomNotParticipantException.java @@ -0,0 +1,14 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.NotExistException; + +public class RoomNotParticipantException extends NotExistException { + public RoomNotParticipantException() { + super(ErrorCode.ROOM_NOT_PARTICIPANT.getMessage(), ErrorCode.ROOM_NOT_PARTICIPANT); + } + + public RoomNotParticipantException(ErrorCode errorCode) { + super(errorCode.getMessage(), errorCode); + } +} From 7892a6102188a397094d1de39fa1daed1839bbab Mon Sep 17 00:00:00 2001 From: gkwon Date: Mon, 4 Mar 2024 22:26:10 +0900 Subject: [PATCH 2/8] [FIX] After merge modified --- gg-data/src/main/java/gg/data/party/Room.java | 9 +++------ .../java/gg/party/api/user/room/service/RoomService.java | 1 - .../db/migration/V1.3__update_party_content.sql | 1 + 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/gg-data/src/main/java/gg/data/party/Room.java b/gg-data/src/main/java/gg/data/party/Room.java index be3847b20..3273ee9b2 100644 --- a/gg-data/src/main/java/gg/data/party/Room.java +++ b/gg-data/src/main/java/gg/data/party/Room.java @@ -85,18 +85,15 @@ public Room(User host, User creator, Category category, String title, String con this.status = status; } - public void updateCurrentPeople(int currentPeople) - { + public void updateCurrentPeople(int currentPeople){ this.currentPeople = currentPeople; } - public void updateStatus(RoomType status) - { + public void updateStatus(RoomType status){ this.status = status; } - public void updateHost(User host) - { + public void updateHost(User host){ this.host = host; } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java index 7ecb3455c..27f4cc4d0 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java @@ -26,7 +26,6 @@ import gg.party.api.user.room.controller.response.RoomResDto; import gg.party.api.user.room.controller.response.UserRoomResDto; import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; -import gg.pingpong.api.user.user.dto.UserDto; import gg.repo.party.CategoryRepository; import gg.repo.party.CommentRepository; import gg.repo.party.RoomRepository; diff --git a/gg-pingpong-api/src/main/resources/db/migration/V1.3__update_party_content.sql b/gg-pingpong-api/src/main/resources/db/migration/V1.3__update_party_content.sql index f11c51916..77fac87c8 100644 --- a/gg-pingpong-api/src/main/resources/db/migration/V1.3__update_party_content.sql +++ b/gg-pingpong-api/src/main/resources/db/migration/V1.3__update_party_content.sql @@ -12,6 +12,7 @@ CREATE TABLE room ( category_id BIGINT NOT NULL, title VARCHAR(15) NOT NULL, content VARCHAR(100) NOT NULL, + current_people INT NOT NULL, max_people INT NOT NULL, min_people INT NOT NULL DEFAULT 2, due_date DATETIME NOT NULL, From 7d4e0868f7303fa5b33b6547004aafe4bd9b4c98 Mon Sep 17 00:00:00 2001 From: gkwon Date: Tue, 5 Mar 2024 01:15:40 +0900 Subject: [PATCH 3/8] [FIX] checkstyle at utils.TestDataUtils "import gg.auth.utils.AuthTokenProvider;" --- gg-utils/src/testFixtures/java/gg/utils/TestDataUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gg-utils/src/testFixtures/java/gg/utils/TestDataUtils.java b/gg-utils/src/testFixtures/java/gg/utils/TestDataUtils.java index c4ba5a62f..1e5e8f05e 100644 --- a/gg-utils/src/testFixtures/java/gg/utils/TestDataUtils.java +++ b/gg-utils/src/testFixtures/java/gg/utils/TestDataUtils.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Component; +import gg.auth.utils.AuthTokenProvider; import gg.data.game.Game; import gg.data.game.PChange; import gg.data.game.Team; @@ -37,7 +38,6 @@ import gg.data.user.type.SnsType; import gg.pingpong.api.admin.tournament.controller.request.TournamentAdminCreateRequestDto; import gg.pingpong.api.admin.tournament.controller.request.TournamentAdminUpdateRequestDto; -import gg.auth.utils.AuthTokenProvider; import gg.pingpong.api.user.tournament.controller.response.TournamentResponseDto; import gg.pingpong.api.user.user.dto.UserImageDto; import gg.repo.game.GameRepository; From fc29d03685485969e4cc4338766a50d887c0774a Mon Sep 17 00:00:00 2001 From: gkwon Date: Tue, 5 Mar 2024 01:35:09 +0900 Subject: [PATCH 4/8] [FIX] checkstyle fix --- gg-data/src/main/java/gg/data/party/Room.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gg-data/src/main/java/gg/data/party/Room.java b/gg-data/src/main/java/gg/data/party/Room.java index 3273ee9b2..fe3b624c7 100644 --- a/gg-data/src/main/java/gg/data/party/Room.java +++ b/gg-data/src/main/java/gg/data/party/Room.java @@ -85,15 +85,15 @@ public Room(User host, User creator, Category category, String title, String con this.status = status; } - public void updateCurrentPeople(int currentPeople){ + public void updateCurrentPeople(int currentPeople) { this.currentPeople = currentPeople; } - public void updateStatus(RoomType status){ + public void updateStatus(RoomType status) { this.status = status; } - public void updateHost(User host){ + public void updateHost(User host) { this.host = host; } } From 4220bf53f1c60ee1c24c5b327a001399f2519b00 Mon Sep 17 00:00:00 2001 From: gkwon Date: Tue, 5 Mar 2024 01:44:51 +0900 Subject: [PATCH 5/8] [FIX] checkstyle fix --- .../gg/party/api/user/room/controller/RoomController.java | 6 +++--- .../java/gg/party/api/user/room/service/RoomService.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java index cab139753..64b625f0e 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java @@ -3,8 +3,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -87,8 +87,8 @@ public ResponseEntity roomDetailInfo(@Parameter(hidden = true) * @return 나간 사람의 닉네임 */ @PatchMapping("/party/rooms/{room_id}") - public ResponseEntity leaveRoom(@PathVariable Long room_id, + public ResponseEntity leaveRoom(@PathVariable("room_id") Long roomId, @Parameter(hidden = true) @Login UserDto user) { - return ResponseEntity.status(HttpStatus.OK).body(roomService.leaveRoom(room_id, user)); + return ResponseEntity.status(HttpStatus.OK).body(roomService.leaveRoom(roomId, user)); } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java index 27f4cc4d0..5f7a97790 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java @@ -21,11 +21,11 @@ import gg.data.user.User; import gg.party.api.user.room.controller.request.RoomCreateReqDto; import gg.party.api.user.room.controller.response.CommentResDto; +import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; import gg.party.api.user.room.controller.response.RoomDetailResDto; import gg.party.api.user.room.controller.response.RoomListResDto; import gg.party.api.user.room.controller.response.RoomResDto; import gg.party.api.user.room.controller.response.UserRoomResDto; -import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; import gg.repo.party.CategoryRepository; import gg.repo.party.CommentRepository; import gg.repo.party.RoomRepository; @@ -34,9 +34,9 @@ import gg.utils.exception.ErrorCode; import gg.utils.exception.party.CategoryNotFoundException; import gg.utils.exception.party.RoomNotFoundException; -import gg.utils.exception.party.RoomReportedException; import gg.utils.exception.party.RoomNotOpenException; import gg.utils.exception.party.RoomNotParticipantException; +import gg.utils.exception.party.RoomReportedException; import gg.utils.exception.user.UserNotFoundException; import lombok.RequiredArgsConstructor; From edb9f59c503482491caf94eb5b7cac9675788fee Mon Sep 17 00:00:00 2001 From: gkwon Date: Tue, 5 Mar 2024 16:23:55 +0900 Subject: [PATCH 6/8] [FIX] fixed service naming convention and api endpoint and RoomType val --- gg-data/src/main/java/gg/data/party/type/RoomType.java | 2 +- .../gg/party/api/user/room/controller/RoomController.java | 4 ++-- .../main/java/gg/party/api/user/room/service/RoomService.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gg-data/src/main/java/gg/data/party/type/RoomType.java b/gg-data/src/main/java/gg/data/party/type/RoomType.java index 714166802..0909cefef 100644 --- a/gg-data/src/main/java/gg/data/party/type/RoomType.java +++ b/gg-data/src/main/java/gg/data/party/type/RoomType.java @@ -13,7 +13,7 @@ public enum RoomType { OPEN("open", "방 시작 전"), START("live", "방 진행 중"), FINISH("end", "방 종료"), - HIDDEN("end", "방 종료 후 가림"), + HIDDEN("end", "신고로 인한 가림 상태"), FAIL("end", "매칭 실패한 방"); private final String code; diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java index 64b625f0e..85dfdfcad 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java @@ -86,9 +86,9 @@ public ResponseEntity roomDetailInfo(@Parameter(hidden = true) * 참여한 방을 나가기 한다 * @return 나간 사람의 닉네임 */ - @PatchMapping("/party/rooms/{room_id}") + @PatchMapping("/{room_id}") public ResponseEntity leaveRoom(@PathVariable("room_id") Long roomId, @Parameter(hidden = true) @Login UserDto user) { - return ResponseEntity.status(HttpStatus.OK).body(roomService.leaveRoom(roomId, user)); + return ResponseEntity.status(HttpStatus.OK).body(roomService.modifyOrderLeaveRoom(roomId, user)); } } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java index 5f7a97790..fd87cc64d 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java @@ -145,7 +145,7 @@ public RoomListResDto findOrderMyHistoryRoomList(Long userId) { * @return */ @Transactional - public LeaveRoomResponseDto leaveRoom(Long roomId, UserDto user) { + public LeaveRoomResponseDto modifyOrderLeaveRoom(Long roomId, UserDto user) { Room targetRoom = roomRepository.findById(roomId) .orElseThrow(RoomNotFoundException::new); if (!targetRoom.getStatus().equals(RoomType.OPEN)) { From 37970df488d96ee8978adafe52562221c5928a13 Mon Sep 17 00:00:00 2001 From: gkwon Date: Tue, 5 Mar 2024 16:56:18 +0900 Subject: [PATCH 7/8] [FIX] fixed controller naming convention --- .../java/gg/party/api/user/room/controller/RoomController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java index 85dfdfcad..aa900b9ea 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java @@ -87,7 +87,7 @@ public ResponseEntity roomDetailInfo(@Parameter(hidden = true) * @return 나간 사람의 닉네임 */ @PatchMapping("/{room_id}") - public ResponseEntity leaveRoom(@PathVariable("room_id") Long roomId, + public ResponseEntity leaveRoomModify(@PathVariable("room_id") Long roomId, @Parameter(hidden = true) @Login UserDto user) { return ResponseEntity.status(HttpStatus.OK).body(roomService.modifyOrderLeaveRoom(roomId, user)); } From 19d06198ab7de6b3ecc7e9761e4afbb9950b3164 Mon Sep 17 00:00:00 2001 From: yes-ee Date: Tue, 5 Mar 2024 20:23:48 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[FIX]=20=EB=B0=A9=20=EB=82=98=EA=B0=80?= =?UTF-8?q?=EA=B8=B0=20=EA=B4=80=EB=A0=A8=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95,=20=EB=A6=AC=EB=B7=B0=20=EB=82=B4=EC=9A=A9=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#640)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 참여 인원 0명 됐을 때 저장 안 되는 에러 수정 - 유저의 참여 방 여러 개 일 때 다른 방에서 나가는 에러 수정 --- .../src/main/java/gg/data/party/UserRoom.java | 6 ----- .../user/room/controller/RoomController.java | 4 +-- ...mResponseDto.java => LeaveRoomResDto.java} | 2 +- .../api/user/room/service/RoomService.java | 26 +++++++++++-------- .../gg/repo/party/UserRoomRepository.java | 2 +- .../java/gg/utils/exception/ErrorCode.java | 1 + .../party/UserNotFoundException.java | 11 ++++++++ 7 files changed, 31 insertions(+), 21 deletions(-) rename gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/{LeaveRoomResponseDto.java => LeaveRoomResDto.java} (82%) create mode 100644 gg-utils/src/main/java/gg/utils/exception/party/UserNotFoundException.java diff --git a/gg-data/src/main/java/gg/data/party/UserRoom.java b/gg-data/src/main/java/gg/data/party/UserRoom.java index f69c2ea44..43c3ff642 100644 --- a/gg-data/src/main/java/gg/data/party/UserRoom.java +++ b/gg-data/src/main/java/gg/data/party/UserRoom.java @@ -3,8 +3,6 @@ import static gg.utils.exception.BusinessChecker.*; import static gg.utils.exception.ErrorCode.*; -import java.time.LocalDateTime; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; @@ -15,14 +13,10 @@ import javax.persistence.ManyToOne; import gg.data.BaseTimeEntity; -import gg.data.tournament.Tournament; import gg.data.user.User; import lombok.AccessLevel; -import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.Setter; -import lombok.experimental.SuperBuilder; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java index 85dfdfcad..ee70918f6 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/RoomController.java @@ -13,7 +13,7 @@ import gg.auth.UserDto; import gg.auth.argumentresolver.Login; import gg.party.api.user.room.controller.request.RoomCreateReqDto; -import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; +import gg.party.api.user.room.controller.response.LeaveRoomResDto; import gg.party.api.user.room.controller.response.RoomDetailResDto; import gg.party.api.user.room.controller.response.RoomListResDto; import gg.party.api.user.room.service.RoomService; @@ -87,7 +87,7 @@ public ResponseEntity roomDetailInfo(@Parameter(hidden = true) * @return 나간 사람의 닉네임 */ @PatchMapping("/{room_id}") - public ResponseEntity leaveRoom(@PathVariable("room_id") Long roomId, + public ResponseEntity leaveRoom(@PathVariable("room_id") Long roomId, @Parameter(hidden = true) @Login UserDto user) { return ResponseEntity.status(HttpStatus.OK).body(roomService.modifyOrderLeaveRoom(roomId, user)); } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResDto.java similarity index 82% rename from gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java rename to gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResDto.java index e42743260..1ad9729f3 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResponseDto.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/controller/response/LeaveRoomResDto.java @@ -5,6 +5,6 @@ @AllArgsConstructor @Getter -public class LeaveRoomResponseDto { +public class LeaveRoomResDto { private String nickname; } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java index fd87cc64d..b194830af 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomService.java @@ -21,7 +21,7 @@ import gg.data.user.User; import gg.party.api.user.room.controller.request.RoomCreateReqDto; import gg.party.api.user.room.controller.response.CommentResDto; -import gg.party.api.user.room.controller.response.LeaveRoomResponseDto; +import gg.party.api.user.room.controller.response.LeaveRoomResDto; import gg.party.api.user.room.controller.response.RoomDetailResDto; import gg.party.api.user.room.controller.response.RoomListResDto; import gg.party.api.user.room.controller.response.RoomResDto; @@ -145,37 +145,41 @@ public RoomListResDto findOrderMyHistoryRoomList(Long userId) { * @return */ @Transactional - public LeaveRoomResponseDto modifyOrderLeaveRoom(Long roomId, UserDto user) { + public LeaveRoomResDto modifyOrderLeaveRoom(Long roomId, UserDto user) { Room targetRoom = roomRepository.findById(roomId) .orElseThrow(RoomNotFoundException::new); if (!targetRoom.getStatus().equals(RoomType.OPEN)) { throw new RoomNotOpenException(); } - List userRoomList = userRoomRepository.findByUserId(user.getId()); - UserRoom targetUserRoom = userRoomList.stream() - .filter(tu -> (tu.getUser().getId().equals(user.getId()))) - .findAny() - .orElseThrow(() -> new RoomNotParticipantException(ErrorCode.ROOM_NOT_PARTICIPANT)); + UserRoom targetUserRoom = userRoomRepository.findByUserAndRoom(userRepository.findById(user.getId()).get(), + targetRoom).orElseThrow(RoomNotParticipantException::new); // 모두 나갈 때 방 fail처리 if (targetRoom.getCurrentPeople() == 1) { targetRoom.updateCurrentPeople(0); targetRoom.updateStatus(RoomType.FAIL); - return new LeaveRoomResponseDto(targetUserRoom.getNickname()); + targetUserRoom.updateIsExist(false); + roomRepository.save(targetRoom); + userRoomRepository.save(targetUserRoom); + return new LeaveRoomResDto(targetUserRoom.getNickname()); } // 방장 이권 if (user.getId().equals(targetRoom.getHost().getId())) { - List existUserRooms = userRoomRepository.findByIsExist(roomId); - targetRoom.updateHost(existUserRooms.get(0).getUser()); + List existUser = userRoomRepository.findByIsExist(roomId); + if (existUser.isEmpty()) { + throw new RoomNotFoundException(ErrorCode.ROOM_NOT_FOUND); + } + targetRoom.updateHost(existUser.get(0)); } targetRoom.updateCurrentPeople(targetRoom.getCurrentPeople() - 1); targetUserRoom.updateIsExist(false); userRoomRepository.save(targetUserRoom); + roomRepository.save(targetRoom); - return new LeaveRoomResponseDto(targetUserRoom.getNickname()); + return new LeaveRoomResDto(targetUserRoom.getNickname()); } /** diff --git a/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java b/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java index f78dacdf6..a62affe53 100644 --- a/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java @@ -19,7 +19,7 @@ public interface UserRoomRepository extends JpaRepository { @Query("SELECT ur.user FROM UserRoom ur WHERE ur.room.id = :roomId AND ur.isExist = true " + "ORDER BY ur.modifiedAt ASC") - List findByIsExist(@Param("roomId") Long roomId); + List findByIsExist(@Param("roomId") Long roomId); Optional findByUserAndRoom(User user, Room room); diff --git a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java index d9731f314..7934acf14 100644 --- a/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java +++ b/gg-utils/src/main/java/gg/utils/exception/ErrorCode.java @@ -158,6 +158,7 @@ public enum ErrorCode { ROOM_NOT_FOUND(404, "PT102", "존재하지 않는 방입니다."), USER_NOT_IN_ROOM(404, "PT103", "방에 존재하지 않는 유저입니다."), ROOM_REPORTED_ERROR(404, "PT104", "신고 상태로 접근이 불가능합니다."), + USER_NOT_EXIST(404, "PT105", "방에 유저가 존재하지 않습니다."), COMMENT_TOO_LONG(400, "PT201", "댓글은 100자 이하로 작성해주세요."), ROOM_FINISHED(403, "PT501", "마감된 방입니다."), ROOM_NOT_PARTICIPANT(400, "PT002", "해당 방의 참여자가 아닙니다."), diff --git a/gg-utils/src/main/java/gg/utils/exception/party/UserNotFoundException.java b/gg-utils/src/main/java/gg/utils/exception/party/UserNotFoundException.java new file mode 100644 index 000000000..20a75854d --- /dev/null +++ b/gg-utils/src/main/java/gg/utils/exception/party/UserNotFoundException.java @@ -0,0 +1,11 @@ +package gg.utils.exception.party; + +import gg.utils.exception.ErrorCode; +import gg.utils.exception.custom.NotExistException; + +public class UserNotFoundException extends NotExistException { + public UserNotFoundException() { + super(ErrorCode.USER_NOT_EXIST.getMessage(), ErrorCode.USER_NOT_EXIST); + } + +}