Skip to content

Commit 095b7e0

Browse files
authored
Feat#131 포스트 다건조회시 조회수 추가 (#133)
* Practice Post 생성 api path 분리 * 게시글 좋아요 싫어요 api 호출 시 count 리턴 * 게시글 상세페이지 url 수정 * post 필드 명 수정 career -> job * validation 메시지 수정 * Practice Post API 통합 * 조회수 추가
1 parent 19fc1c5 commit 095b7e0

File tree

7 files changed

+72
-78
lines changed

7 files changed

+72
-78
lines changed

back/src/main/java/com/back/domain/post/post/controller/PostController.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public RsData<PostPagingResponse> getPostWithPage(
4040
return new RsData<>("200", "게시글이 조회 되었습니다.", resDto);
4141
}
4242

43-
@Operation(summary = "게시글 생성")
43+
@Operation(summary = "게시글 생성 ")
4444
@PostMapping
4545
public RsData<PostCreateResponse> createPost(
4646
@Valid @RequestBody PostCreateRequest postCreateRequest
@@ -93,41 +93,29 @@ public RsData<Void> updatePost(@PathVariable Long post_id
9393

9494
@Operation(summary = "게시글 좋아요")
9595
@PostMapping("/{post_id}/liked")
96-
public RsData<Void> likePost(@PathVariable Long post_id) {
96+
public RsData<PostLikedResponse> likePost(@PathVariable Long post_id) {
9797
postLikeService.likePost(post_id);
9898

99-
return new RsData<>("200", "게시글 좋아요 성공", null);
100-
}
101-
102-
@Operation(summary = "게시글 좋아요 (Show)")
103-
@GetMapping("/{post_id}/liked")
104-
public RsData<PostLikedResponse> getLike(@PathVariable Long post_id) {
10599
int likeCount = postLikeService.getLikeCount(post_id);
106100
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);
107101

108-
return new RsData<>("200", "게시글 좋아요 조회 성공", postLikedResponse);
109-
}
110-
111-
@Operation(summary = "게시글 싫어요 (Show)")
112-
@GetMapping("/{post_id}/disliked")
113-
public RsData<PostLikedResponse> getDisLike(@PathVariable Long post_id) {
114-
int likeCount = postLikeService.getDisLikeCount(post_id);
115-
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);
116-
117-
return new RsData<>("200", "게시글 싫어요 조회 성공", postLikedResponse);
102+
return new RsData<>("200", "게시글 좋아요 성공", postLikedResponse);
118103
}
119104

120105
@Operation(summary = "게시글 싫어요")
121106
@PostMapping("/{post_id}/disliked")
122107
public RsData<PostLikedResponse> disLikePost(@PathVariable Long post_id) {
123108
postLikeService.disLikePost(post_id);
124109

125-
return new RsData<>("200", "게시글 싫어요 성공", null);
110+
int likeCount = postLikeService.getDisLikeCount(post_id);
111+
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);
112+
113+
return new RsData<>("200", "게시글 싫어요 성공", postLikedResponse);
126114
}
127115

128116

129117
@Operation(summary = "게시글 상세페이지")
130-
@GetMapping("/Detail/{post_id}")
118+
@GetMapping("/detail/{post_id}")
131119
public RsData<PostDetailResponse> getPostDetail(@PathVariable Long post_id) {
132120

133121
PostDetailResponse response = postDetailFacade.getDetailWithViewIncrement(post_id);

back/src/main/java/com/back/domain/post/post/dto/PostCreateRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ public record PostCreateRequest(
88
String postType,
99
@NotBlank(message = "제목은 null 혹은 공백일 수 없습니다.")
1010
String title,
11-
@NotBlank(message = "제목은 null 혹은 공백일 수 없습니다.")
12-
String content
11+
@NotBlank(message = "내용은 null 혹은 공백일 수 없습니다.")
12+
String content,
13+
String job
1314
) {
1415

1516

back/src/main/java/com/back/domain/post/post/dto/PostDto.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
public record PostDto(
99
Long postId,
1010
String title,
11-
String content
11+
String content,
12+
int viewCount
1213
) {
1314

1415

1516
public static PostDto from(Post post) {
1617
return new PostDto(
1718
post.getId(),
1819
post.getTitle(),
19-
post.getContent()
20+
post.getContent(),
21+
post.getViewCount()
2022
);
2123
}
2224
}

back/src/main/java/com/back/domain/post/post/entity/Post.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public enum PostType {
4242
private int viewCount;
4343

4444
private Boolean isMento;
45-
private String career;
45+
private String job;
4646

4747
private Boolean isResolve;
4848

@@ -107,4 +107,11 @@ public void increaseViewCount() {
107107
public void updateResolveStatus(Boolean isResolve) {
108108
this.isResolve = isResolve;
109109
}
110+
111+
public void updateJob(String job) {
112+
if (job == null || job.isBlank()) {
113+
throw new ServiceException("400", "직업은 null이거나 공백일 수 없습니다.");
114+
}
115+
this.job = job;
116+
}
110117
}

back/src/main/java/com/back/domain/post/post/service/PostService.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.back.domain.post.post.service;
22

33
import com.back.domain.member.member.entity.Member;
4-
import com.back.domain.post.post.dto.PostAllResponse;
5-
import com.back.domain.post.post.dto.PostCreateRequest;
6-
import com.back.domain.post.post.dto.PostDto;
7-
import com.back.domain.post.post.dto.PostSingleResponse;
4+
import com.back.domain.post.post.dto.*;
85
import com.back.domain.post.post.entity.Post;
96
import com.back.domain.post.post.repository.PostRepository;
107
import com.back.global.exception.ServiceException;
@@ -43,12 +40,6 @@ public Post createPost(PostCreateRequest postCreateRequest, Member member) {
4340
throw new ServiceException("400", "실무 경험 공유 게시글은 멘토만 작성할 수 있습니다.");
4441
}
4542

46-
// if( postType == Post.PostType.PRACTICEPOST ) {
47-
// if(member.getCareer() == null || member.getCareer().isEmpty()) {
48-
// throw new ServiceException("400", "멘토는 경력을 입력해야 실무 경험 공유 게시글을 작성할 수 있습니다.");
49-
// }
50-
// }
51-
5243
Post post = Post.builder()
5344
.title(postCreateRequest.title())
5445
.content(postCreateRequest.content())
@@ -57,6 +48,10 @@ public Post createPost(PostCreateRequest postCreateRequest, Member member) {
5748
.build();
5849

5950

51+
if(postType == Post.PostType.PRACTICEPOST) {
52+
post.updateJob(postCreateRequest.job());
53+
}
54+
6055
// PostType이 QUESTIONPOST인 경우 isResolve를 false로 초기화
6156
if(postType == Post.PostType.QUESTIONPOST) {
6257
post.updateResolveStatus(false);
@@ -67,6 +62,7 @@ public Post createPost(PostCreateRequest postCreateRequest, Member member) {
6762
return post;
6863
}
6964

65+
7066
@Transactional
7167
public void removePost(Long postId, Member member) {
7268
Post post = findById(postId);

back/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -438,22 +438,22 @@ void t12() throws Exception {
438438
.andExpect(jsonPath("$.msg").value("게시글 좋아요 성공"));
439439
}
440440

441-
@Test
442-
@DisplayName("게시글 좋아요 조회")
443-
void t13() throws Exception {
444-
ResultActions resultActions = mvc
445-
.perform(
446-
get("/post/{post_id}/liked", 1L)
447-
)
448-
.andDo(print());
449-
450-
resultActions
451-
.andExpect(handler().handlerType(PostController.class))
452-
.andExpect(handler().methodName("getLike"))
453-
.andExpect(jsonPath("$.resultCode").value("200"))
454-
.andExpect(jsonPath("$.msg").value("게시글 좋아요 조회 성공"))
455-
.andExpect(jsonPath("$.data.likeCount").exists());
456-
}
441+
// @Test
442+
// @DisplayName("게시글 좋아요 조회")
443+
// void t13() throws Exception {
444+
// ResultActions resultActions = mvc
445+
// .perform(
446+
// get("/post/{post_id}/liked", 1L)
447+
// )
448+
// .andDo(print());
449+
//
450+
// resultActions
451+
// .andExpect(handler().handlerType(PostController.class))
452+
// .andExpect(handler().methodName("getLike"))
453+
// .andExpect(jsonPath("$.resultCode").value("200"))
454+
// .andExpect(jsonPath("$.msg").value("게시글 좋아요 조회 성공"))
455+
// .andExpect(jsonPath("$.data.likeCount").exists());
456+
// }
457457

458458
@Test
459459
@DisplayName("게시글 싫어요 성공")
@@ -471,22 +471,22 @@ void t14() throws Exception {
471471
.andExpect(jsonPath("$.msg").value("게시글 싫어요 성공"));
472472
}
473473

474-
@Test
475-
@DisplayName("게시글 싫어요 조회")
476-
void t15() throws Exception {
477-
ResultActions resultActions = mvc
478-
.perform(
479-
get("/post/{post_id}/disliked", 1L)
480-
)
481-
.andDo(print());
482-
483-
resultActions
484-
.andExpect(handler().handlerType(PostController.class))
485-
.andExpect(handler().methodName("getDisLike"))
486-
.andExpect(jsonPath("$.resultCode").value("200"))
487-
.andExpect(jsonPath("$.msg").value("게시글 싫어요 조회 성공"))
488-
.andExpect(jsonPath("$.data.likeCount").exists());
489-
}
474+
// @Test
475+
// @DisplayName("게시글 싫어요 조회")
476+
// void t15() throws Exception {
477+
// ResultActions resultActions = mvc
478+
// .perform(
479+
// get("/post/{post_id}/disliked", 1L)
480+
// )
481+
// .andDo(print());
482+
//
483+
// resultActions
484+
// .andExpect(handler().handlerType(PostController.class))
485+
// .andExpect(handler().methodName("getDisLike"))
486+
// .andExpect(jsonPath("$.resultCode").value("200"))
487+
// .andExpect(jsonPath("$.msg").value("게시글 싫어요 조회 성공"))
488+
// .andExpect(jsonPath("$.data.likeCount").exists());
489+
// }
490490

491491
@Test
492492
@DisplayName("좋아요 -> 싫어요 토글 테스트")
@@ -539,7 +539,7 @@ void t18() throws Exception {
539539

540540
ResultActions resultActions = mvc
541541
.perform(
542-
get("/post/Detail/{post_id}", 1L)
542+
get("/post/detail/{post_id}", 1L)
543543
)
544544
.andDo(print());
545545

@@ -566,7 +566,7 @@ void t18() throws Exception {
566566
void t19() throws Exception {
567567
ResultActions resultActions = mvc
568568
.perform(
569-
get("/post/Detail/{post_id}", 999L)
569+
get("/post/detail/{post_id}", 999L)
570570
)
571571
.andDo(print());
572572

back/src/test/java/com/back/domain/post/post/service/PostServiceTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CreatePostTest {
4545
void createPost_informationPost_success() {
4646
// given
4747
Member member = MemberFixture.create(1L, "[email protected]", "Test User", "password", Member.Role.MENTEE);
48-
PostCreateRequest request = new PostCreateRequest("INFORMATIONPOST","제목","내용");
48+
PostCreateRequest request = new PostCreateRequest("INFORMATIONPOST","제목","내용","");
4949

5050

5151
Post expectedPost = createPost("제목", "내용", member, Post.PostType.INFORMATIONPOST);
@@ -68,7 +68,7 @@ void createPost_informationPost_success() {
6868
void createPost_practicePost_mentor_success() {
6969
// given
7070
Member mentor = MemberFixture.create(1L, "[email protected]", "Mentor", "password", Member.Role.MENTOR);
71-
PostCreateRequest request = new PostCreateRequest("PRACTICEPOST","실무경험","실무내용");
71+
PostCreateRequest request = new PostCreateRequest("PRACTICEPOST","실무경험","실무내용","123");
7272
Post expectedPost = createPost("실무 경험", "실무 내용", mentor, Post.PostType.PRACTICEPOST);
7373

7474
when(postRepository.save(any(Post.class))).thenReturn(expectedPost);
@@ -86,7 +86,7 @@ void createPost_practicePost_mentor_success() {
8686
void createPost_practicePost_mentee_failure() {
8787
// given
8888
Member mentee = MemberFixture.create(1L, "[email protected]", "Mentee", "password", Member.Role.MENTEE);
89-
PostCreateRequest request = new PostCreateRequest("PRACTICEPOST","실무경험","실무내용");
89+
PostCreateRequest request = new PostCreateRequest("PRACTICEPOST","실무경험","실무내용","");
9090

9191
// when & then
9292
assertThatThrownBy(() -> postService.createPost(request, mentee))
@@ -101,7 +101,7 @@ void createPost_practicePost_mentee_failure() {
101101
void createPost_questionPost_initializeIsResolve() {
102102
// given
103103
Member member = MemberFixture.create(1L, "[email protected]", "Test User", "password", Member.Role.MENTEE);
104-
PostCreateRequest request = new PostCreateRequest("QUESTIONPOST","질문경험","질문내용");
104+
PostCreateRequest request = new PostCreateRequest("QUESTIONPOST","질문경험","질문내용","");
105105

106106
Post expectedPost = createPost("질문", "질문 내용", member, Post.PostType.QUESTIONPOST);
107107
expectedPost.updateResolveStatus(false);
@@ -122,7 +122,7 @@ void createPost_questionPost_initializeIsResolve() {
122122
void createPost_invalidPostType_failure() {
123123
// given
124124
Member member = MemberFixture.createDefault();
125-
PostCreateRequest request = new PostCreateRequest("INVALIDPOST","질문경험","질문내용");
125+
PostCreateRequest request = new PostCreateRequest("INVALIDPOST","질문경험","질문내용","");
126126

127127
// when & then
128128
assertThatThrownBy(() -> postService.createPost(request, member))
@@ -202,7 +202,7 @@ void updatePost_author_success() {
202202
Member author = MemberFixture.create(1L, "[email protected]", "Author", "password", Member.Role.MENTEE);
203203
Post post = createPost("기존 제목", "기존 내용", author, Post.PostType.INFORMATIONPOST);
204204
Long postId = 1L;
205-
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","새 제목","새 내용");
205+
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","새 제목","새 내용","");
206206

207207
when(postRepository.findById(postId)).thenReturn(Optional.of(post));
208208
when(postRepository.save(any(Post.class))).thenReturn(post);
@@ -224,7 +224,7 @@ void updatePost_notAuthor_failure() {
224224
Member otherUser = MemberFixture.create(2L, "[email protected]", "Other", "password", Member.Role.MENTEE);
225225
Post post = createPost("제목", "내용", author, Post.PostType.INFORMATIONPOST);
226226
Long postId = 1L;
227-
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","새 내용","새 제목");
227+
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","새 내용","새 제목","");
228228

229229

230230
when(postRepository.findById(postId)).thenReturn(Optional.of(post));
@@ -244,7 +244,7 @@ void updatePost_nullOrBlankTitle_failure() {
244244
Member author = MemberFixture.create(1L, "[email protected]", "Author", "password", Member.Role.MENTEE);
245245
Post post = createPost("제목", "내용", author, Post.PostType.INFORMATIONPOST);
246246
Long postId = 1L;
247-
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","","새 내용");
247+
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","","새 내용","");
248248

249249
when(postRepository.findById(postId)).thenReturn(Optional.of(post));
250250

@@ -263,7 +263,7 @@ void updatePost_nullOrBlankContent_failure() {
263263
Member author = MemberFixture.create(1L, "[email protected]", "Author", "password", Member.Role.MENTEE);
264264
Post post = createPost("제목", "내용", author, Post.PostType.INFORMATIONPOST);
265265
Long postId = 1L;
266-
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","새 제목","");
266+
PostCreateRequest updateRequest = new PostCreateRequest("INFORMATIONPOST","새 제목","","");
267267

268268
when(postRepository.findById(postId)).thenReturn(Optional.of(post));
269269

0 commit comments

Comments
 (0)