
파티방 참가
< 작업 계획>
DTO 코드 작성
Service 코드 작성
Repository 쿼리 작성
Controller 코드 작성
Postman 확인
DTO 코드 작성
<ReqDto>
@Setter
@Getter
public static class JoinRoomReqDto {
private Long userId;
private Long roomId;
public Enter toEntity(User user, Room room) {
return Enter.builder()
.user(user)
.room(room)
.stay(true)
.build();
}
}
<RespDto>
@Setter
@Getter
public static class JoinRoomRespDto {
private Long id;
private Long userId;
private Long roomId;
// 나중에 채팅방 보여줘야함
public JoinRoomRespDto(Enter enter) {
this.id = enter.getId();
this.userId = enter.getUser().getId();
this.roomId = enter.getRoom().getId();
}
}
Service 코드 작성
@Transactional
public JoinRoomRespDto 파티방참가(JoinRoomReqDto joinRoomReqDto) {
log.debug("디버그 : 파티방 참가 서비스 호출");
// 검증
User userPS = userRepository.findById(joinRoomReqDto.getUserId())
.orElseThrow(
() -> new CustomApiException("해당 유저가 없습니다", HttpStatus.FORBIDDEN));
Room roomPS = roomRepository.findById(joinRoomReqDto.getRoomId())
.orElseThrow(
() -> new CustomApiException("해당 파티방이 없습니다", HttpStatus.FORBIDDEN));
// 실행
Enter enter = joinRoomReqDto.toEntity(userPS, roomPS);
Enter enterPS = enterRepository.save(enter);
// 응답
return new JoinRoomRespDto(enterPS);
}
Repository 쿼리 작성
@Query("select e from Enter e join fetch e.room r join fetch e.user u where e.room.id = :roomId and e.user.id = :userId")
Optional<Enter> findByRoomIdAndUserId(@Param("roomId") Long roomId, @Param("userId") Long userId);
Controller 코드 작성
// 파티방 참가
@PostMapping("/party/join/{roomId}")
public ResponseEntity<?> joinRoom(@RequestBody JoinRoomReqDto joinRoomReqDto, @PathVariable Long roomId,
@AuthenticationPrincipal LoginUser loginUser) {
log.debug("디버그 : 파티방 참가 컨트롤러 호출");
joinRoomReqDto.setUserId(loginUser.getUser().getId());
joinRoomReqDto.setRoomId(roomId);
JoinRoomRespDto joinRoomRespDto = partyService.파티방참가(joinRoomReqDto);
return new ResponseEntity<>(new ResponseDto<>("파티방 참가 완료", joinRoomRespDto), HttpStatus.CREATED);
}
Postman 확인
바디값으로 따로 입력할 것은 없었다.
주의 !!! 헤더에 토큰을 꼭 넣어주도록 하자 ...

'Project > GameFriendApp' 카테고리의 다른 글
| Ggamf : 파티방 나가기 Junit Test (0) | 2022.12.06 |
|---|---|
| Ggamf : 파티방 참가 Junit Test (0) | 2022.12.06 |
| Ggamf : 파티방 나가기 (0) | 2022.12.06 |
| Ggamf : 파티방 생성 Junit Test (0) | 2022.12.06 |
| Ggamf : 파티방 생성 (+게임카테고리 데이터 넘겨주기) (0) | 2022.12.06 |