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
2 changes: 1 addition & 1 deletion back/.env.default
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CUSTOM__JWT__SECRET_KEY=abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
CUSTOM__JWT__SECRET_KEY=NEED_TO_SET
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.back.domain.post.like.repository.PostLikeRepository;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.service.PostService;
import com.back.domain.post.rq.PostDetailFacade;
import com.back.global.rq.Rq;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Optional;
Expand All @@ -16,8 +18,8 @@
public class PostLikeService {
private final Rq rq;
private final PostLikeRepository postLikeRepository;
private final PostService postService;

//private final PostDetailFacade postDetailFacade;
private final PostService postService; // 리팩토링 대상

@Transactional
public void likePost(long postId) {
Expand Down Expand Up @@ -70,11 +72,25 @@ public void disLikePost(long postId) {
}
}

public int showDisLikeCount(Long postId) {
public int getDisLikeCount(Long postId) {
return postLikeRepository.countDislikesByPostId(postId);
}

public int showLikeCount(Long postId) {
public int getLikeCount(Long postId) {
return postLikeRepository.countLikesByPostId(postId);
}

public String getPresentStatus(Long postId) {
Member member = rq.getActor();
Post post = postService.findById(postId);

Optional<PostLike> existingLike = postLikeRepository.findByMemberAndPost(member, post);

if (existingLike.isPresent()) {
PostLike postLike = existingLike.get();
return postLike.getStatus().name();
}

return "NONE"; // 좋아요/싫어요 안한 상태
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
import com.back.domain.post.post.dto.*;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.service.PostService;
import com.back.domain.post.rq.PostDetailFacade;
import com.back.global.rq.Rq;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -26,6 +23,7 @@ public class InformationPostController {
private final PostLikeService postLikeService;
private final PostService postService;
private final Rq rq;
private final PostDetailFacade postDetailFacade;


@Operation(summary = "게시글 조회 - 페이징 처리")
Expand Down Expand Up @@ -103,7 +101,7 @@ public RsData<Void> likePost(@PathVariable Long post_id) {
@Operation(summary = "게시글 좋아요 (Show)")
@GetMapping("/{post_id}/liked")
public RsData<PostLikedResponse> getLike(@PathVariable Long post_id) {
int likeCount = postLikeService.showLikeCount(post_id);
int likeCount = postLikeService.getLikeCount(post_id);
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);

return new RsData<>("200", "게시글 좋아요 조회 성공", postLikedResponse);
Expand All @@ -112,7 +110,7 @@ public RsData<PostLikedResponse> getLike(@PathVariable Long post_id) {
@Operation(summary = "게시글 싫어요 (Show)")
@GetMapping("/{post_id}/Disliked")
public RsData<PostLikedResponse> getDisLike(@PathVariable Long post_id) {
int likeCount = postLikeService.showDisLikeCount(post_id);
int likeCount = postLikeService.getDisLikeCount(post_id);
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);

return new RsData<>("200", "게시글 싫어요 조회 성공", postLikedResponse);
Expand All @@ -125,4 +123,12 @@ public RsData<PostLikedResponse> disLikePost(@PathVariable Long post_id) {

return new RsData<>("200", "게시글 싫어요 성공", null);
}

@GetMapping("/Detail/{post_id}")
public RsData<PostDetailResponse> getPostDetail(@PathVariable Long post_id) {

PostDetailResponse response = postDetailFacade.getDetailWithViewIncrement(post_id);

return new RsData<>("200", "게시글 상세 조회 성공", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.back.domain.post.post.dto;

import com.back.domain.post.comment.dto.CommentAllResponse;
import com.back.domain.post.post.entity.Post;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
@Data
public class PostDetailResponse {

private Long id;
private String title;
private String content;
private String authorName;
private LocalDateTime createdAt;
private int viewCount;

private int likeCount;
private int dislikeCount;
private String userLikeStatus;

private List<CommentAllResponse> comments;



public static PostDetailResponse from(Post post, List<CommentAllResponse> comments, int likeCount, int dislikeCount, String userLikeStatus) {
PostDetailResponse postDetailResponse = new PostDetailResponse();

postDetailResponse.setId(post.getId());
postDetailResponse.setTitle(post.getTitle());
postDetailResponse.setContent(post.getContent());
postDetailResponse.setAuthorName(post.getAuthorName());
postDetailResponse.setCreatedAt(post.getCreateDate());
postDetailResponse.setViewCount(post.getViewCount());

postDetailResponse.setLikeCount(likeCount);
postDetailResponse.setDislikeCount(dislikeCount);
postDetailResponse.setUserLikeStatus(userLikeStatus);

postDetailResponse.setComments(comments);



return postDetailResponse;
}

}
23 changes: 13 additions & 10 deletions back/src/main/java/com/back/domain/post/post/dto/PostDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@


import com.back.domain.post.post.entity.Post;
import lombok.Data;

@Data
public class PostDto {
private Long postId;
private String title;
private String content;

public record PostDto(
Long postId,
String title,
String content
) {
public static PostDto from(Post post) {
return new PostDto(
post.getId(),
post.getTitle(),
post.getContent()
);
PostDto postDto = new PostDto();
postDto.setPostId(post.getId());
postDto.setTitle(post.getTitle());
postDto.setContent(post.getContent());

return postDto;
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package com.back.domain.post.post.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.domain.Page;
import org.springframework.security.core.parameters.P;

import java.util.List;

public record PostPagingResponse(
@Schema(description = "게시글 목록")
List<PostDto> posts,
@Schema(description = "현재 페이지 (0부터 시작)")
int currentPage,
@Schema(description = "총 페이지")
int totalPage,
@Schema(description = "총 개수")
long totalElements,
@Schema(description = "다음 페이지 존재 여부")
boolean hasNext
) {
@Data
public class PostPagingResponse{

@Schema(description = "게시글 목록")
List<PostDto> posts;
@Schema(description = "현재 페이지 (0부터 시작)")
int currentPage;
@Schema(description = "총 페이지")
int totalPage;
@Schema(description = "총 개수")
long totalElements;
@Schema(description = "다음 페이지 존재 여부")
boolean hasNext;

public static PostPagingResponse from(Page<PostDto> page) {
return new PostPagingResponse(
page.getContent(),
page.getNumber(),
page.getTotalPages(),
page.getTotalElements(),
page.hasNext()
);
PostPagingResponse postPagingResponse = new PostPagingResponse();
postPagingResponse.setPosts(page.getContent());
postPagingResponse.setCurrentPage(page.getNumber());
postPagingResponse.setTotalPage(page.getTotalPages());
postPagingResponse.setTotalElements(page.getTotalElements());
postPagingResponse.setHasNext(page.hasNext());

return postPagingResponse;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> , PostRepositoryCustom{
@Query("SELECT p FROM Post p JOIN FETCH p.member")
List<Post> findAllWithMember();

@Query("SELECT p FROM Post p JOIN FETCH p.member WHERE p.id = :postId")
Optional<Post> findByIdWithMember(@Param("postId") Long postId);

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package com.back.domain.post.post.service;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.like.repository.PostLikeRepository;
import com.back.domain.post.post.dto.PostAllResponse;
import com.back.domain.post.post.dto.PostCreateRequest;
import com.back.domain.post.post.dto.PostDto;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.repository.PostRepository;
import com.back.global.exception.ServiceException;
import com.back.global.rq.Rq;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PostService {

private final PostRepository postRepository;
private final PostRepository postRepository;;

public List<Post> getAllPosts() {
List<Post> posts = postRepository.findAll();
Expand Down Expand Up @@ -67,6 +65,14 @@ public void updatePost(long postId, Member member, @Valid PostCreateRequest post
postRepository.save(post);
}

public Post getPostDetailWithViewIncrement(Long postId) {
Post post = postRepository.findByIdWithMember(postId)
.orElseThrow(()-> new ServiceException("400","해당 Id의 게시글이 없습니다."));
post.setViewCount(post.getViewCount() + 1);

return post;
}


public Page<PostDto> getPosts(String keyword, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
Expand All @@ -86,4 +92,6 @@ public List<PostAllResponse> getAllPostResponse() {
.map(PostAllResponse::new)
.toList();
}


}
41 changes: 41 additions & 0 deletions back/src/main/java/com/back/domain/post/rq/PostDetailFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.back.domain.post.rq;

import com.back.domain.post.comment.dto.CommentAllResponse;
import com.back.domain.post.comment.service.PostCommentService;
import com.back.domain.post.like.service.PostLikeService;
import com.back.domain.post.post.dto.PostDetailResponse;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PostDetailFacade {

private final PostService postService;
private final PostLikeService postLikeService;
private final PostCommentService postCommentService;

@Transactional
public PostDetailResponse getDetailWithViewIncrement(Long postId) {
// 조회수 증가와 상세정보 조회를 한 번에
Post post = postService.getPostDetailWithViewIncrement(postId);

List<CommentAllResponse> comments = postCommentService.getAllPostCommentResponse(postId);
int likeCount = postLikeService.getLikeCount(postId);
int dislikeCount = postLikeService.getDisLikeCount(postId);
String userStatus = postLikeService.getPresentStatus(postId);

return PostDetailResponse.from(post, comments, likeCount, dislikeCount, userStatus);
}

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


}
Loading