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 @@ -9,6 +9,7 @@
import com.back.global.rq.Rq;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,6 +21,7 @@

@RestController
@RequestMapping("/post/comment")

public class PostCommentController {
@Autowired
private Rq rq;
Expand Down Expand Up @@ -66,5 +68,12 @@ public RsData<Void> updatePostComment(@PathVariable Long post_id

return new RsData<>("200", "댓글 수정 성공", null);
}
@Operation(summary = "댓글 채택")
@PostMapping("isAdopted/{commentId}")
public RsData<Void> adoptComment(@PathVariable Long commentId) {
Member member = rq.getActor();
postCommentService.adoptComment(commentId, member);
return new RsData<>("200", "댓글 채택 성공", null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class PostComment extends BaseEntity {

private String role;

private Boolean isAdopted;

public Boolean isAuthor( Member member) {
return Objects.equals(this.member.getId(), member.getId());
}
Expand All @@ -41,6 +43,7 @@ public PostComment(Post post, String content, Member member, String role) {
this.content = content;
this.member = member;
this.role = role;
this.isAdopted = false;
}


Expand All @@ -55,5 +58,14 @@ public void updatePost(Post post) {
this.post = post;
}

public void adoptComment() {
this.isAdopted = true;
}
/*
* Post 단위 테스트 작성
* isAdopted 테스트
* PracticePost 작성 권한 테스트
* API 명세서 수정
* */

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.back.domain.post.comment.repository;

import com.back.domain.post.comment.entity.PostComment;
import com.back.domain.post.post.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -13,4 +14,7 @@ public interface PostCommentRepository extends JpaRepository<PostComment, Long>

@Query("SELECT c FROM PostComment c JOIN FETCH c.member WHERE c.post.id = :postId")
List<PostComment> findCommentsWithMemberByPostId(@Param("postId") Long postId);


boolean existsByPostAndIsAdoptedTrue(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public void removePostComment(Long postId, CommentDeleteRequest commentDeleteReq
throw new ServiceException("400", "삭제 권한이 없습니다.");
}

// if(postComment.getIsAdopted()) {
// throw new ServiceException("400", "채택된 댓글은 삭제할 수 없습니다.");
// }

postCommentRepository.delete(postComment);

}
Expand All @@ -79,21 +83,56 @@ public void updatePostComment(Long postId, CommentModifyRequest commentModifyReq
throw new ServiceException("400", "수정 권한이 없습니다.");
}

if ( commentModifyRequest.getContent() == null || commentModifyRequest.getContent().isEmpty()) {
throw new ServiceException("400", "댓글은 비어 있을 수 없습니다.");
}

postComment.updateContent(commentModifyRequest.getContent());
}



private void validatePostExists(Long postId) {
if(postId == null || postId <= 0) {
throw new ServiceException("400", "유효하지 않은 게시글 Id입니다.");
}

if (!postRepository.existsById(postId)) {
throw new ServiceException("400", "해당 Id의 게시글이 없습니다.");
}


}

private PostComment getPostCommentById(Long commentId) {
return postCommentRepository.findById(commentId).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
}


public void adoptComment(Long commentId, Member member) {
PostComment postComment = postCommentRepository.findById(commentId)
.orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));

Post post = postComment.getPost();

if (!post.isAuthor(member)) {
throw new ServiceException("400", "채택 권한이 없습니다.");
}

if (post.getPostType() != Post.PostType.QUESTIONPOST) {
throw new ServiceException("400", "질문 게시글에만 댓글 채택이 가능합니다.");
}

if (postComment.getIsAdopted()) {
throw new ServiceException("400", "이미 채택된 댓글입니다.");
}

// 이미 채택된 댓글이 있는지 확인
boolean alreadyAdopted = postCommentRepository.existsByPostAndIsAdoptedTrue(post);
if (alreadyAdopted) {
throw new ServiceException("400", "이미 채택된 댓글이 있습니다.");
}

postComment.adoptComment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@
import java.util.List;

@RestController
@RequestMapping("post/infor")
@RequestMapping("/post")
@RequiredArgsConstructor
public class InformationPostController {
public class PostController {
private final PostLikeService postLikeService;
private final PostService postService;
private final Rq rq;
private final PostDetailFacade postDetailFacade;


@Operation(summary = "게시글 조회 - 페이징 처리")
@GetMapping
@GetMapping("/page/{postType}")
public RsData<PostPagingResponse> getPostWithPage(
@PathVariable Post.PostType postType,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String keyword
) {
Page<PostDto> postPage = postService.getPosts(keyword, page,size);
Page<PostDto> postPage = postService.getPosts(keyword, page,size, postType);
PostPagingResponse resDto = PostPagingResponse.from(postPage);

return new RsData<>("200", "게시글이 조회 되었습니다.", resDto);
Expand Down Expand Up @@ -108,7 +109,7 @@ public RsData<PostLikedResponse> getLike(@PathVariable Long post_id) {
}

@Operation(summary = "게시글 싫어요 (Show)")
@GetMapping("/{post_id}/Disliked")
@GetMapping("/{post_id}/disliked")
public RsData<PostLikedResponse> getDisLike(@PathVariable Long post_id) {
int likeCount = postLikeService.getDisLikeCount(post_id);
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);
Expand All @@ -124,6 +125,8 @@ public RsData<PostLikedResponse> disLikePost(@PathVariable Long post_id) {
return new RsData<>("200", "게시글 싫어요 성공", null);
}


@Operation(summary = "게시글 상세페이지")
@GetMapping("/Detail/{post_id}")
public RsData<PostDetailResponse> getPostDetail(@PathVariable Long post_id) {

Expand Down
10 changes: 7 additions & 3 deletions back/src/main/java/com/back/domain/post/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,29 @@ public static void validPostType(String postTypeStr) {
try {
Post.PostType.valueOf(postTypeStr);
} catch (IllegalArgumentException e) {
throw new ServiceException("400-2", "유효하지 않은 PostType입니다.");
throw new ServiceException("400", "유효하지 않은 PostType입니다.");
}
}

public void updateTitle(String title) {
if(title == null || title.isBlank()) {
throw new ServiceException("400-3", "제목은 null이거나 공백일 수 없습니다.");
throw new ServiceException("400", "제목은 null이거나 공백일 수 없습니다.");
}
this.title = title;
}

public void updateContent(String content) {
if(content == null || content.isBlank()) {
throw new ServiceException("400-4", "내용은 null이거나 공백일 수 없습니다.");
throw new ServiceException("400", "내용은 null이거나 공백일 수 없습니다.");
}
this.content = content;
}

public void increaseViewCount() {
this.viewCount ++;
}

public void updateResolveStatus(Boolean isResolve) {
this.isResolve = isResolve;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import org.springframework.data.domain.Pageable;

public interface PostRepositoryCustom {
Page<Post> searchPosts(String keyword, Pageable pageable);
Page<Post> searchPosts(String keyword, Pageable pageable, Post.PostType postType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ public class PostRepositoryImpl implements PostRepositoryCustom{
private final JPAQueryFactory queryFactory;

@Override
public Page<Post> searchPosts(String keyword, Pageable pageable) {
public Page<Post> searchPosts(String keyword, Pageable pageable, Post.PostType postType) {
QPost post = QPost.post;
QMember member = QMember.member;

BooleanBuilder builder = new BooleanBuilder();

if(postType != null) {
builder.and(post.postType.eq(postType));
}

if(keyword != null && !keyword.isBlank()) {
builder.and(
post.title.containsIgnoreCase(keyword)
Expand All @@ -34,7 +37,7 @@ public Page<Post> searchPosts(String keyword, Pageable pageable) {
.selectFrom(post)
.where(builder)
.orderBy(post.createDate.desc())
.offset(pageable.getOffset())// 이거뭐임
.offset(pageable.getOffset()) // 시작점
.limit(pageable.getPageSize())
.fetch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.repository.PostRepository;
import com.back.global.exception.ServiceException;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -19,6 +20,7 @@

@Service
@RequiredArgsConstructor
@Tag(name = "PostController", description = "커뮤니티(게시글) API")
public class PostService {

private final PostRepository postRepository;;
Expand All @@ -35,17 +37,29 @@ public Post createPost(PostCreateRequest postCreateRequest, Member member) {
Post.validPostType(postTypeStr);
Post.PostType postType = Post.PostType.valueOf(postTypeStr);

// PostType이 PracticePost인 경우 멘토인지 확인
if (postType == Post.PostType.PRACTICEPOST && member.getRole() != Member.Role.MENTOR) {
throw new ServiceException("400", "실무 경험 공유 게시글은 멘토만 작성할 수 있습니다.");
}

// if( postType == Post.PostType.PRACTICEPOST ) {
// if(member.getCareer() == null || member.getCareer().isEmpty()) {
// throw new ServiceException("400", "멘토는 경력을 입력해야 실무 경험 공유 게시글을 작성할 수 있습니다.");
// }
// }

Post post = Post.builder()
.title(postCreateRequest.getTitle())
.content(postCreateRequest.getContent())
.member(member)
.postType(postType)
.build();

// post.setTitle(postCreateRequest.getTitle());
// post.setContent(postCreateRequest.getContent());
// post.setMember(member);
// post.setPostType(postType);

// PostType이 QUESTIONPOST인 경우 isResolve를 false로 초기화
if(postType == Post.PostType.QUESTIONPOST) {
post.updateResolveStatus(false);
}

postRepository.save(post);

Expand All @@ -65,6 +79,14 @@ public void updatePost(long postId, Member member, @Valid PostCreateRequest post
Post post = findById(postId);
if (!post.isAuthor(member)) throw new ServiceException("400", "수정 권한이 없습니다.");

if ( postCreateRequest.getTitle() == null || postCreateRequest.getTitle().isBlank()) {
throw new ServiceException("400", "제목을 입력해주세요.");
}

if ( postCreateRequest.getContent() == null || postCreateRequest.getContent().isBlank()) {
throw new ServiceException("400", "내용을 입력해주세요.");
}

post.updateTitle(postCreateRequest.getTitle());
post.updateContent(postCreateRequest.getContent());

Expand All @@ -79,25 +101,23 @@ public Post getPostDetailWithViewIncrement(Long postId) {
return post;
}


public Page<PostDto> getPosts(String keyword, int page, int size) {
public Page<PostDto> getPosts(String keyword, int page, int size ,Post.PostType postType) {
Pageable pageable = PageRequest.of(page, size);

return postRepository.searchPosts(keyword, pageable).map(PostDto::from);
return postRepository.searchPosts(keyword, pageable, postType).map(PostDto::from);
}

public Post findById(Long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
return post;
}



public List<PostAllResponse> getAllPostResponse() {
return postRepository.findAllWithMember().stream()
.map(PostAllResponse::new)
.toList();
}

//채택된 comment 받아오기

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ public PostDetailResponse getDetailWithViewIncrement(Long postId) {
return PostDetailResponse.from(post, comments, likeCount, dislikeCount, userStatus);
}

// public Post findById(Long postId) {
// return postService.findById(postId);
// }


}
Loading