Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@ public ResponseEntity<?> createPost(
}//희소식 생성

@GetMapping("/{postId}")
public ResponseEntity<?> getPostById(@PathVariable Long postId) {
return postService.getPostById(postId);
}//희소식 상세 조회
public ResponseEntity<?> getPostById(
@PathVariable Long postId,
@AuthenticationPrincipal String email
) {
return postService.getPostById(postId, email);
}
//희소식 상세 조회

@GetMapping
public ResponseEntity<?> getAllPosts(
@RequestParam(defaultValue = "0") int page, // 기본값 0
@RequestParam(defaultValue = "10") int size // 기본값 10
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@AuthenticationPrincipal String email // 로그인 사용자 email 주입
) {
return postService.getAllPosts(page, size);
}//희소식 전체 조회 (페이지네이션)
return postService.getAllPosts(page, size, email);
}
//희소식 전체 조회 (페이지네이션)

@PostMapping("/{postId}/like")
public ResponseEntity<?> togglePostLike(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static class GoodnewsResponseDto {
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private String placeName;

private WriterInfoDto writer; //작성자 정보 추가

public static GoodnewsResponseDto from(Post post, WriterInfoDto writer) {
Expand Down Expand Up @@ -69,6 +70,7 @@ public static class PostDto {
private int likeCount; // 좋아요 개수 추가
private int commentCount; // 댓글 개수 추가
private WriterInfoDto writer;// 작성자 정보 필드 추가
private boolean liked; // 좋아요 여부

}//값 반환 dto

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import java.util.Optional;

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

Optional<PostLike> findByUserIdAndPostId(Long userId, Long postId);

boolean existsByUserIdAndPostId(Long userId, Long postId); // ✅ 추가

int countByPostId(Long postId); // 특정 게시글의 좋아요 개수 조회

void deleteByPostId(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ private CommentDto.CommentResultDto toCommentResultDtoWithReplies(Comment commen
}



//사용자의 댓글 조회
public ResponseEntity<?> getMyComments(String email) {
Member member = memberRepository.findMemberByEmail(email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ public ResponseEntity<?> createPost(GoodnewsDto.GoodnewsCreateDto goodnewsCreate
}
}//게시글 생성

public ResponseEntity<?> getPostById(Long postId) {
public ResponseEntity<?> getPostById(Long postId, String email) {
Member member = memberRepository.findMemberByEmail(email)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

// 1. 게시글 조회 (없으면 예외 발생)
Post post = postRepository.findById(postId)
.orElseThrow(() -> new RuntimeException(ErrorStatus.POST_NOT_FOUND.getMessage()));

boolean liked = postLikeRepository.findByUserIdAndPostId(member.getId(), postId).isPresent();

// 2. 조회된 게시글을 DTO로 변환
GoodnewsDto.PostDto postDto = GoodnewsDto.PostDto.builder()
.postId(post.getId())
Expand All @@ -106,6 +111,7 @@ public ResponseEntity<?> getPostById(Long postId) {
.likeCount(postLikeRepository.countByPostId(post.getId()))
.commentCount(commentRepository.countByPostId(post.getId()))
.writer(mapToWriterDto(post.getUserId())) //작성자 추가
.liked(liked)
.build();

// 3. ApiResponse로 감싸서 반환 (POST_DETAIL_SUCCESS 사용)
Expand All @@ -117,7 +123,11 @@ public ResponseEntity<?> getPostById(Long postId) {
}


public ResponseEntity<?> getAllPosts(int page, int size) {
public ResponseEntity<?> getAllPosts(int page, int size, String email) {
// 1. 사용자 정보 조회
Member user = memberRepository.findMemberByEmail(email)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

// 1. 페이지네이션 객체 생성
Pageable pageable = PageRequest.of(page, size);

Expand All @@ -137,6 +147,7 @@ public ResponseEntity<?> getAllPosts(int page, int size) {
.updatedAt(post.getUpdatedAt())
.likeCount(postLikeRepository.countByPostId(post.getId()))
.commentCount(commentRepository.countByPostId(post.getId()))
.liked(postLikeRepository.findByUserIdAndPostId(user.getId(), post.getId()).isPresent())
.writer(mapToWriterDto(post.getUserId()))//작성자 추가
.build())
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public ResponseEntity<?> getAllPlaces(


@GetMapping("/{placeId}")
public ResponseEntity<?> getPlaceById(@PathVariable Long placeId) {
return placeService.getPlaceById(placeId);
public ResponseEntity<?> getPlaceDetail(
@PathVariable Long id,
@AuthenticationPrincipal String email // JWT 인증 객체 등으로부터 유저 식별
) {
return placeService.getPlaceById(id, email);
}//특정 플레이스 상세 조회

@PatchMapping("/{placeId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,35 @@ public ResponseEntity<?> findAllWithPagination(int page, int size, String email)
}
//플레이스 전체 조회 ( 페이지네이션 )

public ResponseEntity<?> getPlaceById(Long placeId) {
public ResponseEntity<?> getPlaceById(Long placeId, String email) {
// 0. placeId 유효성 검사
if (placeId == null || placeId <= 0) {
throw new GeneralException(ErrorStatus.INVALID_PLACE_ID);
}

// 1. 회원 조회
Member member = memberRepository.findMemberByEmail(email)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

// 1. placeId로 Place 조회 (없으면 예외 발생)
Place place = placeRepository.findById(placeId)
.orElseThrow(() -> new GeneralException(ErrorStatus.PLACE_NOT_FOUND));

// 2. DTO로 변환
// 2. 좋아요 수 및 여부 확인
int likeCount = placeLikeRepository.countByPlaceId(place.getId());
boolean isBookmarked = placeLikeRepository.existsByPlaceIdAndUserId(place.getId(), member.getId());

// 3. DTO로 변환
PlaceDTO placeDTO = PlaceDTO.builder()
.placeId(place.getId()) // 🔹 placeId 추가
.placeId(place.getId()) // placeId 추가
.placeName(place.getPlaceName())
.placeDetails(place.getPlaceDetails())
.placeImg(place.getPlaceImg())
.likeCount(likeCount)
.isBookmark(isBookmarked)
.build();


// 3. 성공 응답 반환
// 4. 성공 응답 반환
return ResponseEntity.ok(ApiResponse.onSuccess(
SuccessStatus._PLACE_DETAIL_SUCCESS.getMessage(),
placeDTO
Expand Down