
겜프 수락
<작업계획>
Entity 작성 & 수락 메서드 작성
DTO 코드 작성
Service 코드 작성
Controller 코드 작성
더미데이터 추가
Postman 확인
Entity 작성 & 수락 메서드 작성
겜프 수락 시 accept 만 true로 수정되므로,
엔티티에 수락 메서드를 작성해서 이용하기로 했다.
@NoArgsConstructor
@Getter
@Table(name = "follow")
@Entity
public class Follow {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Boolean accept;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "follower_id")
User follower;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "following_id")
User following;
@Builder
public Follow(Long id, Boolean accept, User follower, User following) {
this.id = id;
this.accept = accept;
this.follower = follower;
this.following = following;
}
// 겜프 수락
public void acceptGgamf() {
accept = true;
}
}
DTO 코드 작성
<ReqDto>
@Setter
@Getter
public static class AcceptGgamfReqDto {
private Long followId;
private Long userId;
}
<RespDto>
@Setter
@Getter
public static class AcceptGgamfRespDto {
private Long followId;
private Long userId;
private Long ggamfId;
private Boolean accept;
public AcceptGgamfRespDto(Follow follow) {
this.followId = follow.getId();
this.userId = follow.getFollowing().getId();
this.ggamfId = follow.getFollower().getId();
this.accept = follow.getAccept();
}
}
Service 코드 작성
@Transactional
public AcceptGgamfRespDto 겜프수락(AcceptGgamfReqDto acceptGgamfReqDto) {
Follow followPS = followRepository.findById(acceptGgamfReqDto.getFollowId())
.orElseThrow(() -> new CustomApiException("겜프 신청 중이 아닙니다", HttpStatus.FORBIDDEN));
if (followPS.getFollowing().getId() != acceptGgamfReqDto.getUserId()) {
throw new CustomApiException("당신은 수락 권한이 없습니다.", HttpStatus.BAD_REQUEST);
}
followPS.acceptGgamf();
return new AcceptGgamfRespDto(followPS);
}
Controller 코드 작성
// 겜프 수락
@PutMapping("/ggamf/accept/{followId}")
public ResponseEntity<?> acceptGgamf(@RequestBody AcceptGgamfReqDto acceptGgamfReqDto,
@PathVariable Long followId,
@AuthenticationPrincipal LoginUser loginUser) {
log.debug("디버그 : 겜프 수락 컨트롤러 호출");
acceptGgamfReqDto.setFollowId(followId);
acceptGgamfReqDto.setUserId(loginUser.getUser().getId());
AcceptGgamfRespDto acceptGgamfRespDto = ggamfService.겜프수락(acceptGgamfReqDto);
return new ResponseEntity<>(new ResponseDto<>("겜프 수락 완료", acceptGgamfRespDto), HttpStatus.CREATED);
}
더미데이터 추가
기존에 설정해 둔 User 정보

추가한 Follow 더미 데이터

Postman 확인
cos(userId=1) 가 dada(userId=3)의 요청을 수락하도록 해보자.

'Project > GameFriendApp' 카테고리의 다른 글
| Ggamf : 겜프 삭제 (0) | 2022.12.08 |
|---|---|
| Ggamf : 겜프 수락 Junit Test (0) | 2022.12.08 |
| Ggamf : 겜프 요청 Junit Test (0) | 2022.12.07 |
| Ggamf : 겜프 요청 (0) | 2022.12.07 |
| Ggamf : 참가중인 파티방 목록 보기 Junit Test (0) | 2022.12.07 |