-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
84 deletions.
There are no files selected for viewing
24 changes: 13 additions & 11 deletions
24
src/main/java/com/snsIntegrationFeedService/post/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
package com.snsIntegrationFeedService.post.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.snsIntegrationFeedService.post.dto.PostDetailResponseDto; | ||
import com.snsIntegrationFeedService.post.service.PostService; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Post API", description = "Post 관련 API 정보를 담고 있습니다.") | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class PostController { | ||
|
||
private final PostService postService; | ||
private final PostService postService; | ||
|
||
@Operation(summary = "게시글 상세보기", description = "유저가 게시물을 클릭 시 사용되는 API") | ||
@GetMapping("/api/post/{postId}") | ||
public ResponseEntity<PostDetailResponseDto> getPostDetail(@PathVariable String postId) { | ||
PostDetailResponseDto postDetailResponseDto = postService.getPostDetail(postId); | ||
return ResponseEntity.ok().body(postDetailResponseDto); | ||
} | ||
@Operation(summary = "게시글 상세보기", description = "유저가 게시물을 클릭 시 사용되는 API") | ||
@GetMapping("/api/post/{postId}") | ||
public ResponseEntity<PostDetailResponseDto> getPostDetail(@PathVariable String postId) { | ||
PostDetailResponseDto postDetailResponseDto = postService.getPostDetail(postId); | ||
return ResponseEntity.ok().body(postDetailResponseDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
src/main/java/com/snsIntegrationFeedService/post/repository/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package com.snsIntegrationFeedService.post.repository; | ||
|
||
import com.snsIntegrationFeedService.post.entity.Post; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.snsIntegrationFeedService.post.entity.Post; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
|
||
Optional<Post> findByPostId(String postId); | ||
Optional<Post> findByPostId(String postId); | ||
} |
39 changes: 21 additions & 18 deletions
39
src/main/java/com/snsIntegrationFeedService/post/service/PostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
package com.snsIntegrationFeedService.post.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.snsIntegrationFeedService.common.error.CustomErrorCode; | ||
import com.snsIntegrationFeedService.common.exception.CustomException; | ||
import com.snsIntegrationFeedService.post.dto.PostDetailResponseDto; | ||
import com.snsIntegrationFeedService.post.entity.Post; | ||
import com.snsIntegrationFeedService.post.repository.PostRepository; | ||
import java.util.List; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final PostRepository postRepository; | ||
|
||
@Transactional | ||
public PostDetailResponseDto getPostDetail(String postId) { | ||
// 예외 처리 | ||
Post post = postRepository.findByPostId(postId).orElseThrow( | ||
() -> new CustomException(CustomErrorCode.POST_ID_NOT_FOUND) | ||
); | ||
@Transactional | ||
public PostDetailResponseDto getPostDetail(String postId) { | ||
// 예외 처리 | ||
Post post = postRepository.findByPostId(postId).orElseThrow( | ||
() -> new CustomException(CustomErrorCode.POST_ID_NOT_FOUND) | ||
); | ||
|
||
// 해시 태그명 가져오기 | ||
List<String> hashTags = post.getPostHashtagList().stream() | ||
.map(postHashtag -> postHashtag.getHashtag().getName()) | ||
.toList(); | ||
// 해시 태그명 가져오기 | ||
List<String> hashTags = post.getPostHashtagList().stream() | ||
.map(postHashtag -> postHashtag.getHashtag().getName()) | ||
.toList(); | ||
|
||
// 조회수 증가 | ||
post.view(); | ||
return PostDetailResponseDto.from(post, hashTags); | ||
} | ||
// 조회수 증가 | ||
post.view(); | ||
return PostDetailResponseDto.from(post, hashTags); | ||
} | ||
} |