Skip to content

Commit

Permalink
[feature] 방 참여 api 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
AreSain committed Mar 4, 2024
1 parent 4220bf5 commit cad21d9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 13 deletions.
6 changes: 0 additions & 6 deletions gg-data/src/main/java/gg/data/party/UserRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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.RoomDetailResDto;
import gg.party.api.user.room.controller.response.RoomJoinResDto;
import gg.party.api.user.room.controller.response.RoomListResDto;
import gg.party.api.user.room.service.RoomService;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -69,6 +70,16 @@ public ResponseEntity<RoomListResDto> myHistoryRoomList(@Parameter(hidden = true
return ResponseEntity.status(HttpStatus.OK).body(roomListResDto);
}

/**
* 참여한 방을 나가기 한다
* @return 나간 사람의 닉네임
*/
@PatchMapping("/{room_id}")
public ResponseEntity<LeaveRoomResponseDto> leaveRoom(@PathVariable("room_id") Long roomId,
@Parameter(hidden = true) @Login UserDto user) {
return ResponseEntity.status(HttpStatus.OK).body(roomService.leaveRoom(roomId, user));
}

/**
* 방의 상세정보를 조회한다
* @param roomId 방 id
Expand All @@ -83,12 +94,15 @@ public ResponseEntity<RoomDetailResDto> roomDetailInfo(@Parameter(hidden = true)
}

/**
* 참여한 방을 나가기 한다
* @return 나간 사람의 닉네임
* 방의 상세정보를 조회한다
* @param roomId 방 id
* @param user 유저 정보
* @return roomId
*/
@PatchMapping("/party/rooms/{room_id}")
public ResponseEntity<LeaveRoomResponseDto> leaveRoom(@PathVariable("room_id") Long roomId,
@Parameter(hidden = true) @Login UserDto user) {
return ResponseEntity.status(HttpStatus.OK).body(roomService.leaveRoom(roomId, user));
@PostMapping("/{room_id}")
public ResponseEntity<RoomJoinResDto> joinRoom(@Parameter(hidden = true) @Login UserDto user,
@PathVariable("room_id") Long roomId) {
RoomJoinResDto roomJoinResDto = roomService.addOrderjoinRoom(roomId, user);
return ResponseEntity.status(HttpStatus.CREATED).body(roomJoinResDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gg.party.api.user.room.controller.response;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RoomJoinResDto {
private Long roomId;

public RoomJoinResDto(Long roomId) {
this.roomId = roomId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.RoomJoinResDto;
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;
Expand All @@ -37,6 +38,7 @@
import gg.utils.exception.party.RoomNotOpenException;
import gg.utils.exception.party.RoomNotParticipantException;
import gg.utils.exception.party.RoomReportedException;
import gg.utils.exception.party.UserAlreadyInRoom;
import gg.utils.exception.user.UserNotFoundException;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -219,4 +221,42 @@ public RoomDetailResDto findOrderRoomDetail(Long userId, Long roomId) {

return new RoomDetailResDto(room, myNickname, hostNickname, roomUsers, comments);
}

/**
* 방에 참여한다
* @param roomId 방 id
* @param userDto 유저 정보
* @exception RoomNotFoundException 유효하지 않은 방 입력
* @exception UserAlreadyInRoom 이미 참여한 방 입력
* 과거 참여 이력이 있을 경우 그 아이디로, 없을경우 currentPeople을 증가시킨다
* @return roomId
*/
@Transactional
public RoomJoinResDto addOrderjoinRoom(Long roomId, UserDto userDto) {
User user = userRepository.findById(userDto.getId()).get();
Room room = roomRepository.findById(roomId).orElseThrow(RoomNotFoundException::new);

if (room.getStatus() != RoomType.OPEN) {
throw new RoomNotFoundException(ErrorCode.ROOM_FINISHED);
}
UserRoom userRoom = userRoomRepository.findByUserAndRoom(user, room)
.orElseGet(() -> {
String randomNickname = generateRandomNickname();
UserRoom newUserRoom = new UserRoom(user, room, randomNickname);
newUserRoom.updateIsExist(false);
return newUserRoom;
});
if (userRoom.getIsExist() == true) {
throw new UserAlreadyInRoom(ErrorCode.USER_ALREADY_IN_ROOM);
} else {
userRoom.updateIsExist(true);
userRoomRepository.save(userRoom);
}

room.updateCurrentPeople(room.getCurrentPeople() + 1);
// if (room.getCurrentPeople() + 1 == room.getMaxPeople()) 경우 게임 시작하는 로직 추가
roomRepository.save(room);

return new RoomJoinResDto(roomId);
}
}
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
@@ -1,5 +1,6 @@
package gg.utils.exception;

import gg.utils.exception.party.UserAlreadyInRoom;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand Down Expand Up @@ -161,7 +162,8 @@ 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", "해당 방의 참여자가 아닙니다."),
USER_ALREADY_IN_ROOM(409, "PT105", "이미 참여한 방 입니다.");

private final int status;
private final String errCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;

public class UserAlreadyInRoom extends RuntimeException {
private ErrorCode errorCode;

public UserAlreadyInRoom(ErrorCode errorCode) {
super(ErrorCode.USER_ALREADY_IN_ROOM.getMessage());
this.errorCode = errorCode;
}
}

0 comments on commit cad21d9

Please sign in to comment.