-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] post 상세보기 api #27
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f54ea4
feat: post 상세보기 api
ssunnykku 3fc8e68
refactor: 테스트용 security 설정
ssunnykku 6dda02c
refactor: review 반영
ssunnykku 9d8b0a0
refactor: repository test code 삭제
ssunnykku 53480f8
refactor: Entity, DTO Colum post -> content 변경
ssunnykku e3d0a76
Merge branch 'dev' into feat/content_detail
ssunnykku 6fb0355
fix: conflict 해결
ssunnykku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
src/main/java/wanted/media/exception/NotFoundException.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package wanted.media.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class NotFoundException extends RuntimeException { | ||
private final ErrorCode errorCode; | ||
} |
18 changes: 15 additions & 3 deletions
18
src/main/java/wanted/media/exception/handler/GlobalExceptionHandler.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,18 +1,30 @@ | ||
package wanted.media.exception.handler; | ||
|
||
import org.apache.coyote.BadRequestException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import wanted.media.exception.BadRequestException; | ||
import wanted.media.exception.ErrorCode; | ||
import wanted.media.exception.ErrorResponse; | ||
import wanted.media.exception.NotFoundException; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(BadRequestException.class) | ||
public ResponseEntity<ErrorResponse> handleBadRequestException(BadRequestException e) { | ||
return ResponseEntity.badRequest() | ||
.body(new ErrorResponse(400, e.getErrorCode().getMessage())); | ||
.body(new ErrorResponse(400, e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(NotFoundException.class) | ||
public ResponseEntity<ErrorResponse> handlePostNotFound(NotFoundException e) { | ||
ErrorCode errorCode = e.getErrorCode(); | ||
ErrorResponse errorResponse = new ErrorResponse( | ||
errorCode.getStatus().value(), | ||
errorCode.getMessage() | ||
); | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); | ||
} | ||
} |
37 changes: 36 additions & 1 deletion
37
src/main/java/wanted/media/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,9 +1,44 @@ | ||
package wanted.media.post.controller; | ||
|
||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import wanted.media.post.domain.Post; | ||
import wanted.media.post.dto.PostDetailResponse; | ||
import wanted.media.post.service.PostService; | ||
|
||
@RestController | ||
@RequestMapping("/posts") | ||
@RequestMapping("/api/posts") | ||
@RequiredArgsConstructor | ||
public class PostController { | ||
|
||
private final PostService posetService; | ||
|
||
/** | ||
* @param postId | ||
* @return PostDetailResponse | ||
*/ | ||
@GetMapping("/{postId}") | ||
public ResponseEntity<PostDetailResponse> getPost(@PathVariable String postId) { | ||
Post post = posetService.getPost(postId); | ||
PostDetailResponse result = PostDetailResponse.builder() | ||
.postId(post.getId()) | ||
.likeCount(post.getLikeCount()) | ||
.type(post.getType()) | ||
.title(post.getTitle()) | ||
.content(post.getContent()) | ||
.hashtags(post.getHashtags()) | ||
.viewCount(post.getViewCount()) | ||
.shareCount(post.getShareCount()) | ||
.updatedAt(post.getUpdatedAt()) | ||
.createdAt(post.getCreatedAt()) | ||
.userId(post.getUser().getUserId()) | ||
.account(post.getUser().getAccount()) | ||
.email(post.getUser().getEmail()) | ||
.build(); | ||
return ResponseEntity.ok(result); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/wanted/media/post/dto/PostDetailResponse.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package wanted.media.post.dto; | ||
|
||
import lombok.Builder; | ||
import wanted.media.post.domain.Type; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
@Builder | ||
public record PostDetailResponse( | ||
String postId, | ||
Type type, | ||
String title, | ||
String content, | ||
String hashtags, | ||
Long likeCount, | ||
Long viewCount, | ||
Long shareCount, | ||
LocalDateTime updatedAt, | ||
LocalDateTime createdAt, | ||
UUID userId, | ||
String account, | ||
String email | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/wanted/media/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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package wanted.media.post.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import wanted.media.post.domain.Post; | ||
|
||
public interface PostRepository extends JpaRepository<Post, String> { | ||
} |
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,7 +1,24 @@ | ||
package wanted.media.post.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import wanted.media.exception.ErrorCode; | ||
import wanted.media.exception.NotFoundException; | ||
import wanted.media.post.domain.Post; | ||
import wanted.media.post.repository.PostRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
private final PostRepository postRepository; | ||
|
||
@Transactional | ||
public Post getPost(String postId) { | ||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND)); | ||
|
||
post.incrementViewCount(); | ||
return post; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# application-test | ||
spring: | ||
datasource: | ||
driver-class-name: org.h2.Driver | ||
url: jdbc:h2:mem:db;MODE=MYSQL | ||
username: sa | ||
password: | ||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
jpa: | ||
show-sql: true | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
hibernate: | ||
ddl-auto: update | ||
|
||
jwt: | ||
secret_key: key |
63 changes: 63 additions & 0 deletions
63
src/test/java/wanted/media/post/service/PostServiceTest.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package wanted.media.post.service; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import wanted.media.post.domain.Post; | ||
import wanted.media.post.domain.Type; | ||
import wanted.media.post.repository.PostRepository; | ||
import wanted.media.user.domain.Grade; | ||
import wanted.media.user.domain.User; | ||
import wanted.media.user.repository.UserRepository; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
class PostServiceTest { | ||
@Autowired | ||
private PostService postService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private PostRepository postRepository; | ||
|
||
@Test | ||
@Transactional | ||
void getPostTest() { | ||
// given | ||
User user = User.builder() | ||
.account("sun") | ||
.email("[email protected]") | ||
.password("1234") | ||
.grade(Grade.NORMAL_USER) | ||
.build(); | ||
|
||
userRepository.save(user); | ||
|
||
Post post = Post.builder() | ||
.id("qwer") | ||
.type(Type.TWITTER) | ||
.title("제목 입력") | ||
.content("내용 입력") | ||
.user(user) | ||
.viewCount(100L) | ||
.build(); | ||
|
||
postRepository.save(post); | ||
|
||
// when | ||
Post getData = postService.getPost(post.getId()); | ||
|
||
// then | ||
assertThat(getData.getTitle()).isEqualTo("제목 입력"); | ||
assertThat(getData.getContent()).isEqualTo("내용 입력"); | ||
assertThat(getData.getViewCount()).isEqualTo(101); | ||
assertThat(getData.getUser().getAccount()).isEqualTo("sun"); | ||
assertThat(getData.getUser().getEmail()).isEqualTo("[email protected]"); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EOL이 안지켜졌네요 ~! 64,66라인 불필요한 공백도 제거해주세요 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
불필요한 공백 제거해주세요~!