Skip to content

Commit

Permalink
[FIX] startRoom API without 복면가왕 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ghyen committed Mar 6, 2024
1 parent d57611c commit 4397cce
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public ResponseEntity<RoomDetailResDto> roomDetailInfo(@Parameter(hidden = true)
@PatchMapping("/{room_id}")
public ResponseEntity<LeaveRoomResDto> leaveRoom(@PathVariable("room_id") Long roomId,
@Parameter(hidden = true) @Login UserDto user) {
return ResponseEntity.status(HttpStatus.OK).body(roomService.modifyOrderLeaveRoom(roomId, user));
return ResponseEntity.status(HttpStatus.OK).body(roomService.modifyLeaveRoom(roomId, user));
}

/**
* 방 시작하기
* @return 방 id
*/
@PostMapping("/{room_id}/start")
public ResponseEntity<Long> startRoom(@PathVariable("room_id") Long roomId,
@Parameter(hidden = true) @Login UserDto user) {
return ResponseEntity.status(HttpStatus.OK).body(roomService.modifyStartRoom(roomId, user));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
import gg.repo.user.UserRepository;
import gg.utils.exception.ErrorCode;
import gg.utils.exception.party.CategoryNotFoundException;
import gg.utils.exception.party.NotHostException;
import gg.utils.exception.party.RoomNotEnoughPeopleException;
import gg.utils.exception.party.RoomNotFoundException;
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 gg.utils.exception.party.UserNotFoundException;
import lombok.RequiredArgsConstructor;

@Service
Expand Down Expand Up @@ -139,20 +141,26 @@ public RoomListResDto findOrderMyHistoryRoomList(Long userId) {
/**
* 유저가 방을 나간다
* 참가자가 방에 참가한 상태일때만 취소해 준다.
* @param roomId
* @param roomId, user
* @param user 참여 유저(사용자 본인)
* @throws RoomNotFoundException 방 없음 || 방 입장자가 아님
* @return
* @throws RoomNotFoundException 방 없음
* @throws RoomNotOpenException 방이 대기 상태가 아님
* @throws RoomNotParticipantException 방 입장자가 아님
* @return 나간 사람의 닉네임
*/
@Transactional
public LeaveRoomResDto modifyOrderLeaveRoom(Long roomId, UserDto user) {
public LeaveRoomResDto modifyLeaveRoom(Long roomId, UserDto user) {
Room targetRoom = roomRepository.findById(roomId)
.orElseThrow(RoomNotFoundException::new);
if (!targetRoom.getStatus().equals(RoomType.OPEN)) {
throw new RoomNotOpenException();
throw new RoomNotOpenException(ErrorCode.ROOM_NOT_OPEN);
}

UserRoom targetUserRoom = userRoomRepository.findByUserAndRoom(userRepository.findById(user.getId()).get(),
Optional<User> userEntity = userRepository.findById(user.getId());
if (userEntity.isEmpty()) {
throw new UserNotFoundException();
}
UserRoom targetUserRoom = userRoomRepository.findByUserAndRoom(userEntity.get(),
targetRoom).orElseThrow(RoomNotParticipantException::new);

// 모두 나갈 때 방 fail처리
Expand All @@ -168,9 +176,6 @@ public LeaveRoomResDto modifyOrderLeaveRoom(Long roomId, UserDto user) {
// 방장 이권
if (user.getId().equals(targetRoom.getHost().getId())) {
List<User> existUser = userRoomRepository.findByIsExist(roomId);
if (existUser.isEmpty()) {
throw new RoomNotFoundException(ErrorCode.ROOM_NOT_FOUND);
}
targetRoom.updateHost(existUser.get(0));
}

Expand All @@ -182,6 +187,43 @@ public LeaveRoomResDto modifyOrderLeaveRoom(Long roomId, UserDto user) {
return new LeaveRoomResDto(targetUserRoom.getNickname());
}

/**
* <p>방을 시작 상태로 바꾼다</p>
* <p>방의 상태를 시작 상태로 변경.</p>
* @param roomId, user
* @throws RoomNotFoundException 방 없음
* @throws RoomNotOpenException 방이 열리지 않은 상태
* @throws RoomNotEnoughPeopleException 방에 충분한 인원이 없음
* @throws RoomNotParticipantException 방에 참가하지 않은 사용자
* @throws NotHostException 방장이 아닌 경우
* @return 방 id
*/
@Transactional
public Long modifyStartRoom(Long roomId, UserDto user) {
Room targetRoom = roomRepository.findById(roomId)
.orElseThrow(RoomNotFoundException::new);
if (!targetRoom.getStatus().equals(RoomType.OPEN)) {
throw new RoomNotOpenException();
}
if (targetRoom.getMinPeople() > targetRoom.getCurrentPeople()
|| targetRoom.getMaxPeople() < targetRoom.getCurrentPeople()) {
throw new RoomNotEnoughPeopleException();
}
Optional<User> userEntity = userRepository.findById(user.getId());
if (userEntity.isEmpty()) {
throw new UserNotFoundException();
}
UserRoom targetUserRoom = userRoomRepository.findByUserAndRoom(userEntity.get(),
targetRoom).orElseThrow(RoomNotParticipantException::new);
if (targetRoom.getHost() != targetUserRoom.getUser()) {
throw new NotHostException();
}
targetRoom.updateStatus(RoomType.START);
roomRepository.save(targetRoom);

return roomId;
}

/**
* 방의 상세정보를 조회한다
* @param userId 자신의 id
Expand Down
4 changes: 3 additions & 1 deletion gg-utils/src/main/java/gg/utils/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public enum ErrorCode {
COMMENT_TOO_LONG(400, "PT201", "댓글은 100자 이하로 작성해주세요."),
ROOM_FINISHED(403, "PT501", "마감된 방입니다."),
ROOM_NOT_PARTICIPANT(400, "PT002", "해당 방의 참여자가 아닙니다."),
ROOM_NOT_OPEN(400, "PT003", "해당 방의 참여자가 아닙니다.");
ROOM_NOT_OPEN(400, "PT003", "해당 방의 참여자가 아닙니다."),
ROOM_NOT_ENOUGH_PEOPLE(400, "PT202", "시작할 수 있는 인원이 아닙니다."),
NOT_HOST(400, "PT203", "방장이 아닙니다");

private final int status;
private final String errCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

public class CategoryNotFoundException extends NotExistException {
public CategoryNotFoundException() {
super("해당 카테고리가 없습니다.", ErrorCode.CATEGORY_NOT_FOUND);
super(ErrorCode.CATEGORY_NOT_FOUND.getMessage(), ErrorCode.CATEGORY_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.ForbiddenException;

public class InvaildPeopleException extends ForbiddenException {
public InvaildPeopleException() {
super(ErrorCode.NOT_HOST.getMessage(), ErrorCode.NOT_HOST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.ForbiddenException;
import gg.utils.exception.custom.NotExistException;

public class NotHostException extends ForbiddenException {
public NotHostException() {
super(ErrorCode.NOT_HOST.getMessage(), ErrorCode.NOT_HOST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.ForbiddenException;
import gg.utils.exception.custom.NotExistException;

public class RoomNotEnoughPeopleException extends ForbiddenException {
public RoomNotEnoughPeopleException() {
super(ErrorCode.ROOM_NOT_ENOUGH_PEOPLE.getMessage(), ErrorCode.ROOM_NOT_ENOUGH_PEOPLE);
}
}

0 comments on commit 4397cce

Please sign in to comment.