Skip to content

Commit

Permalink
feat: 기존 PostId 대신 Blog별로 PostId를 갖도록 복합키로 설정 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-mallang authored Nov 27, 2023
1 parent 111fa71 commit 9e63c12
Show file tree
Hide file tree
Showing 109 changed files with 1,078 additions and 780 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class AuthCommentService {
private final CommentDeleteService commentDeleteService;

public Long write(WriteAuthCommentCommand command) {
Post post = postRepository.getById(command.postId());
Post post = postRepository.getByIdAndBlogName(command.postId(), command.blogName());
Member writer = memberRepository.getById(command.memberId());
Comment parent = commentRepository.getParentByIdAndPostId(command.parentCommentId(), command.postId());
Comment parent = commentRepository.getParentByIdAndPost(command.parentCommentId(), post);
AuthComment comment = command.toComment(post, writer, parent);
comment.write(command.postPassword());
return commentRepository.save(comment).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class UnAuthCommentService {
private final CommentDeleteService commentDeleteService;

public Long write(WriteUnAuthCommentCommand command) {
Post post = postRepository.getById(command.postId());
Comment parent = commentRepository.getParentByIdAndPostId(command.parentCommentId(), command.postId());
Post post = postRepository.getByIdAndBlogName(command.postId(), command.blogName());
Comment parent = commentRepository.getParentByIdAndPost(command.parentCommentId(), post);
UnAuthComment comment = command.toCommand(post, parent);
comment.write(command.postPassword());
return commentRepository.save(comment).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Builder
public record WriteAuthCommentCommand(
Long postId,
String blogName,
String content,
boolean secret,
Long memberId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@Builder
public record WriteUnAuthCommentCommand(
Long postId,
String blogName,
String content,
String nickname,
String password,
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/mallang/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.util.ArrayList;
Expand All @@ -35,7 +36,10 @@ public abstract class Comment extends CommonDomainModel {
protected String content;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "post_id", nullable = false)
@JoinColumns({
@JoinColumn(name = "post_id", referencedColumnName = "post_id"),
@JoinColumn(name = "blog_id", referencedColumnName = "blog_id"),
})
protected Post post;

protected boolean deleted;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/mallang/comment/domain/CommentRepository.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.mallang.comment.domain;

import com.mallang.comment.exception.NotFoundCommentException;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostId;
import jakarta.annotation.Nullable;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CommentRepository extends JpaRepository<Comment, Long> {

Expand All @@ -27,17 +30,17 @@ default Comment getById(Long id) {
@EntityGraph(attributePaths = {"parent"})
Optional<Comment> findById(Long id);

default Comment getParentByIdAndPostId(@Nullable Long parentCommentId, Long postId) {
default Comment getParentByIdAndPost(@Nullable Long parentCommentId, Post post) {
if (parentCommentId == null) {
return null;
}
return findByIdAndPostId(parentCommentId, postId)
return findByIdAndPost(parentCommentId, post)
.orElseThrow(NotFoundCommentException::new);
}

Optional<Comment> findByIdAndPostId(Long id, Long postId);
Optional<Comment> findByIdAndPost(Long id, Post post);

@Modifying
@Query("DELETE FROM Comment c WHERE c.post.id = :postId")
void deleteAllByPostId(Long postId);
@Query("DELETE FROM Comment c WHERE c.post.postId = :postId")
void deleteAllByPostId(@Param("postId") PostId postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public ResponseEntity<Void> delete(
public ResponseEntity<List<CommentResponse>> findAll(
@CookieValue(name = POST_PASSWORD_COOKIE, required = false) String postPassword,
@Nullable @OptionalAuth Long memberId,
@RequestParam(value = "postId", required = true) Long postId
@RequestParam(value = "postId", required = true) Long postId,
@RequestParam(value = "blogName", required = true) String blogName
) {
List<CommentResponse> result = commentQueryService.findAllByPostId(postId, memberId, postPassword);
List<CommentResponse> result = commentQueryService.findAllByPost(postId, blogName, memberId, postPassword);
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public record WriteAuthCommentRequest(
Long postId,
String blogName,
String content,
boolean secret,
@Nullable Long parentCommentId
Expand All @@ -13,6 +14,7 @@ public record WriteAuthCommentRequest(
public WriteAuthCommentCommand toCommand(Long memberId, @Nullable String postPassword) {
return WriteAuthCommentCommand.builder()
.postId(postId)
.blogName(blogName)
.postPassword(postPassword)
.content(content)
.secret(secret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public record WriteUnAuthCommentRequest(
Long postId,
String blogName,
String content,
String nickname,
String password,
Expand All @@ -14,6 +15,7 @@ public record WriteUnAuthCommentRequest(
public WriteUnAuthCommentCommand toCommand(@Nullable String postPassword) {
return WriteUnAuthCommentCommand.builder()
.postId(postId)
.blogName(blogName)
.postPassword(postPassword)
.content(content)
.nickname(nickname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ private CommentResponse processDeleted(CommentResponse data) {
.build();
}

public List<CommentResponse> processSecret(List<CommentResponse> datas, Long postId, @Nullable Long memberId) {
if (isPostWriter(postId, memberId)) {
public List<CommentResponse> processSecret(List<CommentResponse> datas,
Long postId,
String blogName,
@Nullable Long memberId) {
if (isPostWriter(postId, blogName, memberId)) {
return datas;
}
return datas.stream()
.map(it -> processSecret(it, memberId))
.toList();
}

private boolean isPostWriter(Long postId, @Nullable Long memberId) {
Post post = postRepository.getById(postId);
private boolean isPostWriter(Long postId, String blogName, @Nullable Long memberId) {
Post post = postRepository.getByIdAndBlogName(postId, blogName);
return Objects.equals(post.getWriter().getId(), memberId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class CommentDataValidator {
private final PostRepository postRepository;

public void validateAccessPost(Long postId,
String blogName,
@Nullable Long memberId,
@Nullable String postPassword) {
Post post = postRepository.getById(postId);
Post post = postRepository.getByIdAndBlogName(postId, blogName);
Member member = null;
if (memberId != null) {
member = memberRepository.getById(memberId);
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/mallang/comment/query/CommentQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ public class CommentQueryService {
private final CommentDataValidator commentDataValidator;
private final CommentDataPostProcessor commentDataPostProcessor;

public List<CommentResponse> findAllByPostId(Long postId,
@Nullable Long memberId,
@Nullable String postPassword) {
commentDataValidator.validateAccessPost(postId, memberId, postPassword);
List<CommentResponse> result = commentDao.findAllByPostId(postId);
public List<CommentResponse> findAllByPost(Long postId,
String blogName,
@Nullable Long memberId,
@Nullable String postPassword) {
commentDataValidator.validateAccessPost(postId, blogName, memberId, postPassword);
List<CommentResponse> result = commentDao.findAllByPost(postId, blogName);
List<CommentResponse> deletedProcessedResult = commentDataPostProcessor.processDeleted(result);
return commentDataPostProcessor.processSecret(deletedProcessedResult, postId, memberId);
return commentDataPostProcessor.processSecret(deletedProcessedResult, postId, blogName, memberId);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/mallang/comment/query/dao/CommentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class CommentDao {

private final CommentQuerySupport commentQuerySupport;

public List<CommentResponse> findAllByPostId(Long postId) {
return commentQuerySupport.findAllByPostId(postId).stream()
public List<CommentResponse> findAllByPost(Long postId, String blogName) {
return commentQuerySupport.findAllByPost(postId, blogName)
.stream()
.filter(it -> Objects.isNull(it.getParent()))
.map(CommentResponse::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.mallang.comment.domain.Comment;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CommentQuerySupport extends JpaRepository<Comment, Long> {

List<Comment> findAllByPostId(Long postId);
@Query("SELECT c FROM Comment c WHERE c.post.postId.id = :postId AND c.post.blog.name.value = :blogName")
List<Comment> findAllByPost(@Param("postId") Long postId, @Param("blogName") String blogName);
}
2 changes: 1 addition & 1 deletion src/main/java/com/mallang/common/domain/DomainEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface DomainEvent {

Long id();
Object id();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ public class PostLikeService {
private final PostLikeValidator postLikeValidator;

public void like(ClickPostLikeCommand command) {
Post post = postRepository.getById(command.postId());
Post post = postRepository.getByIdAndBlogName(command.postId(), command.blogName());
Member member = memberRepository.getById(command.memberId());
PostLike postLike = new PostLike(post, member);
postLike.like(postLikeValidator, command.postPassword());
postLikeRepository.save(postLike);
}

public void cancel(CancelPostLikeCommand command) {
PostLike postLike = postLikeRepository.getByPostIdAndMemberId(command.postId(), command.memberId());
PostLike postLike = postLikeRepository
.getByPostIdAndMemberId(command.postId(), command.blogName(), command.memberId());
postLike.cancel(command.postPassword());
postLikeRepository.delete(postLike);
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/mallang/post/application/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import com.mallang.post.application.command.DeletePostCommand;
import com.mallang.post.application.command.UpdatePostCommand;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostId;
import com.mallang.post.domain.PostIntro;
import com.mallang.post.domain.PostOrderInBlogGenerator;
import com.mallang.post.domain.PostRepository;
import com.mallang.post.domain.visibility.PostVisibilityPolicy;
import com.mallang.post.domain.PostVisibilityPolicy;
import jakarta.annotation.Nullable;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,17 +32,18 @@ public class PostService {
private final CategoryRepository categoryRepository;
private final PostOrderInBlogGenerator postOrderInBlogGenerator;

public Long create(CreatePostCommand command) {
public PostId create(CreatePostCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByNameAndOwnerId(command.blogName(), command.memberId());
Category category = getCategoryByIdAndOwnerIdIfPresent(command.categoryId(), command.memberId());
Long postId = postOrderInBlogGenerator.generate(blog);
Post post = command.toPost(member, blog, category, postId);
return postRepository.save(post).getId();
PostId postId = postOrderInBlogGenerator.generate(blog.getId());
Post post = command.toPost(member, category, postId);
return postRepository.save(post).getPostId();
}

public void update(UpdatePostCommand command) {
Post post = postRepository.getByIdAndWriterId(command.postId(), command.memberId());
Post post = postRepository
.getByIdAndBlogNameAndWriterId(command.postId(), command.blogName(), command.memberId());
Category category = getCategoryByIdAndOwnerIdIfPresent(command.categoryId(), command.memberId());
post.update(
command.title(),
Expand All @@ -55,7 +57,8 @@ public void update(UpdatePostCommand command) {
}

public void delete(DeletePostCommand command) {
List<Post> posts = postRepository.findAllByIdInAndWriterId(command.postIds(), command.memberId());
List<Post> posts = postRepository
.findAllByIdInAndWriterId(command.postIds(), command.blogName(), command.memberId());
for (Post post : posts) {
post.delete();
postRepository.delete(post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PostStarService {
private final PostStarValidator postStarValidator;

public Long star(StarPostCommand command) {
Post post = postRepository.getById(command.postId());
Post post = postRepository.getByIdAndBlogName(command.postId(), command.blogName());
Member member = memberRepository.getById(command.memberId());
PostStar postStar = new PostStar(post, member);
postStar.star(postStarValidator, command.postPassword());
Expand All @@ -33,7 +33,8 @@ public Long star(StarPostCommand command) {
}

public void cancel(CancelPostStarCommand command) {
PostStar postStar = postStarRepository.getByPostIdAndMemberId(command.postId(), command.memberId());
PostStar postStar = postStarRepository
.getByPostIdAndBlogNameAndMemberId(command.postId(), command.blogName(), command.memberId());
postStarRepository.delete(postStar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public record CancelPostLikeCommand(
Long postId,
String blogName,
Long memberId,
@Nullable String postPassword
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.mallang.post.application.command;

public record CancelPostStarCommand(
Long memberId,
Long postId,
Long memberId
String blogName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public record ClickPostLikeCommand(
Long postId,
String blogName,
Long memberId,
@Nullable String postPassword
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mallang.post.application.command;

import com.mallang.auth.domain.Member;
import com.mallang.blog.domain.Blog;
import com.mallang.category.domain.Category;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostId;
import com.mallang.post.domain.PostIntro;
import com.mallang.post.domain.visibility.PostVisibilityPolicy;
import com.mallang.post.domain.visibility.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.PostVisibilityPolicy;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import jakarta.annotation.Nullable;
import java.util.List;
import lombok.Builder;
Expand All @@ -24,13 +24,12 @@ public record CreatePostCommand(
@Nullable Long categoryId,
List<String> tags
) {
public Post toPost(Member member, Blog blog, Category category, Long postIdInBlog) {
public Post toPost(Member member, Category category, PostId postId) {
return Post.builder()
.blog(blog)
.postId(postId)
.title(title)
.content(content)
.postThumbnailImageName(postThumbnailImageName)
.order(postIdInBlog)
.writer(member)
.visibilityPolish(new PostVisibilityPolicy(visibility, password))
.category(category)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public record DeletePostCommand(
Long memberId,
List<Long> postIds
List<Long> postIds,
String blogName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public record StarPostCommand(
Long postId,
String blogName,
Long memberId,
@Nullable String postPassword
) {
Expand Down
Loading

0 comments on commit 9e63c12

Please sign in to comment.