Skip to content
Open
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 @@ -3,6 +3,7 @@
import com.berry.common.exceptionhandler.CustomApiException;
import com.berry.common.response.ResErrorCode;
import com.berry.post.domain.model.Like;
import com.berry.post.domain.model.Post;
import com.berry.post.domain.repository.LikeRepository;
import com.berry.post.domain.repository.PostRepository;
import com.berry.post.presentation.request.like.CreatePostLikeRequest;
Expand All @@ -28,11 +29,18 @@ public class LikeServiceImpl implements LikeService {
@CacheEvict(cacheNames = "posts", allEntries = true)
public void createPostLike(CreatePostLikeRequest request, Long userId) {
getPostById(request.postId());

Like like = Like.builder()
.userId(userId)
.postId(request.postId())
.createdAt(LocalDateTime.now())
.build();

Post post = postRepository.findByIdAndDeletedYNFalse(request.postId()).orElseThrow(
() -> new CustomApiException(ResErrorCode.NOT_FOUND, "해당 게시글을 찾을 수 없습니다.")
);
post.plusLikeCount();

likeRepository.save(like);
}

Expand All @@ -46,6 +54,12 @@ public void deletePostLike(Long headerUserId, Long postId) {
if (!Objects.equals(like.getUserId(), headerUserId)) {
throw new CustomApiException(ResErrorCode.FORBIDDEN, "접근 권한이 없습니다.");
}

Post post = postRepository.findByIdAndDeletedYNFalse(postId).orElseThrow(
() -> new CustomApiException(ResErrorCode.NOT_FOUND, "해당 게시글을 찾을 수 없습니다.")
);
post.minusLikeCount();

likeRepository.delete(like);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void createPost(PostCreateRequest postCreateRequest,

@Override
@Transactional
@Cacheable(cacheNames = "posts", key = "#keyword + '-' + #type + '-' + #postCategoryId + '-' + #writerId + '-' + #sort + '-' + #userId")
@Cacheable(cacheNames = "posts", key = "#keyword + '-' + #type + '-' + #postCategoryId + '-' + #writerId + '-' + #sort + '-' + #pageable + '-' + #userId")
public Page<PostListResponse> getPosts(String keyword, String type, Long postCategoryId, Long writerId,
String sort, Pageable pageable, Long userId) {

Expand Down