Spring/[P] 눈치우기 봉사활동 매칭 플랫폼

게시글 상세 조회하기

alsruds 2024. 2. 13. 21:59

 

게시글 상세 페이지에 필요한 데이터 반환

첨부된 사진이 있을 때 & 없을 때

 

☃️ Controller

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/volunteers")
public class VolunteerController {
    private final VolunteerService volunteerService;

    // 봉사활동 구인글 상세 조회
    @GetMapping("/{volunteerId}")
    public ResponseEntity<VolunteerResponse> getVolunteerById(@PathVariable("volunteerId") Long volunteerId, @RequestParam(value = "userId", required = false, defaultValue = "0") Long userId) {
        VolunteerResponse response = volunteerService.getVolunteerById(volunteerId, userId);
        return ResponseEntity.status(HttpStatus.OK).body(response);
    }
}

 

· PathVariable 로 봉사활동글 id 받아 해당 글 조회하기

· RequestParam 으로 사용자 id 받아 해당 글 작성자 조회하기

 

☃️ Service

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class VolunteerService {
    private final VolunteerRepository volunteerRepository;
    private final ImageService imageService;
    private final LikeService likeService;
    private final CommentVolunteerRepository commentVolunteerRepository;

    // 봉사활동 구인글 상세 조회
    public VolunteerResponse getVolunteerById(Long volunteerId, Long userId) {
        Volunteer volunteer = getVolunteerOrThrow(volunteerId);
        List<CommentVolunteer> commentVolunteers = commentVolunteerRepository.findByUserIdAndVolunteerId(userId, volunteerId);
        List<CommentResponse> commentResponses = commentVolunteers.stream()
                .map(m -> CommentResponse.builder()
                        .commentId(m.getId())
                        .userNickname(m.getUser().getNickname())
                        .content(m.getContent())
                        .createdDate(m.getCreatedDate().toLocalDate())
                        .build())
                .collect(Collectors.toList());
        return VolunteerResponse.builder()
                .userNickname(volunteer.getUser().getNickname())
                .userImage(volunteer.getUser().getProfileImage())
                .volunteerId(volunteer.getId())
                .createdDate(volunteer.getCreatedDate().toLocalDate())
                .title(volunteer.getTitle())
                .content(volunteer.getContent())
                .image(imageService.processImage(volunteer.getImage()))
                .place(volunteer.getPlace())
                .likeCount(volunteer.getLikeCount())
                .userLiked(likeService.checkIfUserLikedVolunteer(userId, volunteer.getId()))
                .comments(commentResponses)
                .build();
    }

    // 예외 처리 - 존재하는 봉사활동 구인글인지
    private Volunteer getVolunteerOrThrow(Long volunteerId) {
        return volunteerRepository.findById(volunteerId)
                .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_VOLUNTEER));
    }
}

 

· 댓글 리스트 따로 세팅한 후 넣어주기

 

☃️ Repository

public interface VolunteerRepository extends JpaRepository<Volunteer, Long> {}
public interface CommentVolunteerRepository extends JpaRepository<CommentVolunteer, Long> {
    List<CommentVolunteer> findByUserIdAndVolunteerId(Long userId, Long volunteerId);
}

 

· UserId 와 VolunteerId 를 모두 가지고 있는 댓글 리스트 조회하기

 

☃️ Dto

· VolunteerResponse

@Getter
@Builder
public class VolunteerResponse {
    private Long volunteerId;
    private String userNickname;
    private String userImage;
    private String title;
    private String content;
    private String image;
    private String place;
    private LocalDate createdDate;
    private int likeCount;
    private Boolean userLiked;
    private List<CommentResponse> comments;
}

 

· CommentResponse

@Getter
@Builder
public class CommentResponse {
    private Long commentId;
    private String userNickname;
    private String content;
    private LocalDate createdDate;
}