KH정보교육원 파이널 프로젝트 MotionMate 정리

<aside> 💡

Follow기능 구현 및 PostMan테스트

</aside>

<aside> 💡

Notification(알림)기능 구현 및 PostMan 테스트

</aside>

1. 팔로우기능

  1. 반환타입 불일치
Specified result type [com.motionmate.domain.user.User] did not match Query selection type [com.motionmate.domain.follow.Follow] - multiple selections: use Tuple or array

팔로우 기능(BE)를 맡아서 기능 개발을 하게 되었다. 코드는 모두 작성을 하고 테스트를 해보기 위해서 포스트맨에 테스트를 해봤는데 팔로우/언팔로우, 팔로우 유무확인 모두 정상 작동 했지만 팔로우/팔로잉 목록 확인이 500서버에러 가 떴다 디버깅을 하기위해 인텔리제이 오류 로그를 확인해보니 위와같은 로그가 찍혀있었다. 확인해본 결과 FollowService 부분에서 팔로우/팔로잉 목록을 불러오는 로직을 짜는 중 반환타입은 List<FollowResponseDto> 였는데, 반환은 User엔티티를 반환하려고 해서 생긴 문제였다

정리하자면 Follow엔티티를 기준으로 쿼리를 작성하면서 반환은 User로 지정해놓았던게 문제였다.

처음 생각은 반환타입을 FollowResponseDto로 두고 User엔티티에서 jpa 메소드를 이용해 id를 찾아와서 리스트로 뽑으면 된다고 생각을 했는데 다 짜고 보니 반환타입 불일치로 오류가 난 것 같다. 즉, JPQL을 명시하지 않은 상태에서 Follow 엔티티를 기준으로 쿼리를짜면서 User를 반환하려 했기 때문에 에러가 난 것 이다.

2. 알림기능

  1. User객체 바인딩불가로 get요청시 빈배열 출력
// Controller
// 알림 조회
@GetMapping
public ResponseEntity<List<NotificationResponseDto>> getNotifications(@AuthenticationPrincipal User user) {
    List<Notification> notifications = notificationService.getNotificationByUser(user);
    List<NotificationResponseDto> responseDto = notifications.stream()
            .map(NotificationMapper::toDto)
            .collect(Collectors.toList());

    return ResponseEntity.ok(responseDto);
}

// Service
// 특정 유저 알림 전체 조회
public List<Notification> getNotificationByUser(User user) {
    // User 객체에 해당하는 알림들을 생성일 기준으로 내림차순 정렬하여 반환
    // @AuthenticationPrincipal 어노테이션을 통해 컨트롤러에서 자동으로 User 객체 주입
    return notificationRepository.findByUserOrderByCreatedAtDesc(user);
}

// Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findByUserOrderByCreatedAtDesc(User user);
}