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 @@ -41,4 +41,16 @@ public RsData<PostResponseDto> createPost(
public RsData<List<PostResponseDto>> getAllPosts() {
return RsData.successOf(postService.getAllPosts()); // code=200, message="success"
}

/**
* 게시글 단건 조회 API
* @param postId 조회할 게시글 ID
* @return 해당 ID의 게시글 정보
*/
@GetMapping("/{postId}")
public RsData<PostResponseDto> getPost(
@PathVariable Long postId
) {
return RsData.successOf(postService.getPost(postId)); // code=200, message="success"
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/back/domain/post/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.back.domain.user.entity.User;
import com.back.global.rq.Rq;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -69,4 +70,13 @@ public List<PostResponseDto> getAllPosts() {
.map(PostResponseDto::new)
.collect(Collectors.toList());
}

// 게시글 단건 조회 로직
@Transactional(readOnly = true)
public PostResponseDto getPost(Long postId) {
return new PostResponseDto(
postRepository.findById(postId)
.orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId))
);
}
}