Skip to content

Commit

Permalink
Merge pull request #37 from ssu-student-union/refactor/34-postcomment…
Browse files Browse the repository at this point in the history
…reaction

[refactor] #34 Transaction layer 변경 및 Post updatedAt, lastEditedAt 구분 완료
  • Loading branch information
chahyunsoo authored Jul 31, 2024
2 parents 72eab0d + da62298 commit 3a44fbf
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public PostCommentListResponse getCommentList(String boardCode, Long postId, int
Page<PostComment> commentList = postCommentReader.getPostCommentList(setPageable(page, take), postId);
return PostCommentListResponse.of(commentList, commentList.getTotalElements(), postCommentFormatter::format, type);
}
@Transactional
public PostCommentResponse createComment(Long userId, String boardCode, Long postId, PostCommentCreateRequest postCommentCreateRequest){
PostComment postComment = postCommentAppender.createPostComment(postCommentCreateRequest.toDomain(userId,postId));
return postCommentFormatter.format(postComment.getPostId(), postComment.getUserId(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ussum.homepage.domain.comment.PostComment;

import java.time.LocalDateTime;

public record PostCommentUpdateRequest(
String content
) {
Expand All @@ -11,7 +13,8 @@ public PostComment toDomain(Long commentId, Long postId, Long userId){
content,
postId,
userId,
null
String.valueOf(LocalDateTime.now())
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public Post toDomain(Board board, User user, Category category) {
null,
null,
null,
null,
user.getId(), //이건 채워넣어야 함, user쪽 개발되면
board.getId(),
category.getId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public Post toDomain(Post post, Board board, Category category) {
post.getViewCount(),
thumbnailImage,
LocalDateTime.parse(post.getCreatedAt()),
null,
LocalDateTime.parse(post.getUpdatedAt()),
LocalDateTime.now(),
null,
post.getUserId(),
board.getId(),
category.getId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PostCommentReactionController {
@PostMapping("/boards/posts/comments/{commentId}/reactions")
public ApiResponse<Void> createPostCommentReaction(@PathVariable(name = "commentId") Long commentId,
@RequestBody PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
PostCommentReactionResponse commentReaction = postCommentReactionService.createCommentReaction(commentId, postCommentReactionCreateRequest);
PostCommentReactionResponse commentReaction = postCommentReactionService.createPostCommentReaction(commentId, postCommentReactionCreateRequest);
// return ApiResponse.onSuccess(commentReaction);
return ApiResponse.onSuccess(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@Service
@RequiredArgsConstructor
//@Transactional(readOnly = true)
@Transactional(readOnly = true)
public class PostCommentReactionService {
private final PostCommentReader postCommentReader;
private final PostCommentReactionAppender postCommentReactionAppender;
Expand All @@ -25,7 +25,8 @@ public class PostCommentReactionService {
private final PostCommentReactionFormatter postCommentReactionFormatter;
private final PostCommentReactionReader postCommentReactionReader;

public PostCommentReactionResponse createCommentReaction(Long commentId, PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
@Transactional
public PostCommentReactionResponse createPostCommentReaction(Long commentId, PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
PostComment postComment = postCommentReader.getPostComment(commentId);
Long userId = 1L; //여기에 userId 추출하는 거 추가
postCommentReactionManager.validatePostCommentReactionByCommentIdAndUserId(commentId, userId, postCommentReactionCreateRequest.reaction()); //해당 유저가 해당 댓글에 좋아요를 이미 눌렀는지 안눌렀는지 검증
Expand All @@ -41,9 +42,7 @@ public PostCommentReactionResponse createCommentReaction(Long commentId, PostCom
@Transactional
public void deletePostCommentReaction(Long commentId, PostCommentReactionCreateRequest postCommentReactionCreateRequest) {
Long userId = 1L; //여기에 userId 추출하는 거 추가
// PostCommentReaction postCommentReaction = postCommentReactionReader.getPostCommentReactionWithCommentIdAndUserId(commentId, userId, postCommentReactionCreateRequest.reaction());
postCommentReactionModifier.deletePostCommentReaction(commentId, userId, postCommentReactionCreateRequest.reaction());
// postCommentReactionModifier.deletePostCommentReaction(postCommentReaction.getPostCommentId(), postCommentReaction.getUserId(),
// postCommentReaction.getReactionType());
PostCommentReaction postCommentReaction = postCommentReactionReader.getPostCommentReactionWithCommentIdAndUserId(commentId, userId, postCommentReactionCreateRequest.reaction());
postCommentReactionModifier.deletePostCommentReaction(postCommentReaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void createNewReaction(PostReaction newReaction) {
}

//일단 반환 값 void말고 responseDto로 해놓고 나중에 없애도 될 것 같음.
@Transactional
public PostReactionResponse createPostReaction(Long postId, PostReactionCreateRequest postReactionCreateRequest) {
Long userId = 1L; //여기에 userId 추출하는 거 추가
postReactionManager.validatePostReactionByCommentIdAndUserId(postId, userId, postReactionCreateRequest.reaction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@RequiredArgsConstructor
public class PostCommentAppender {
private final PostCommentRepository postCommentRepository;
@Transactional

public PostComment createPostComment(PostComment postComment){
return postCommentRepository.save(postComment);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ussum/homepage/domain/post/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Post {
private String createdAt;
private String updatedAt;
private String lastEditedAt;
private String deletedAt;
// private String deletedAt;
private Long userId;
private Long boardId;
private Long categoryId;
Expand All @@ -38,12 +38,12 @@ public static Post of(Long id,
LocalDateTime createdAt,
LocalDateTime updatedAt,
LocalDateTime lastEditedAt,
LocalDateTime deletedAt,
// LocalDateTime deletedAt,
Long userId,
Long boardId,
Long categoryId) {
return new Post(id, title, content, viewCount, thumbnailImage, String.valueOf(createdAt),
String.valueOf(updatedAt), String.valueOf(lastEditedAt), String.valueOf(deletedAt),
String.valueOf(updatedAt), String.valueOf(lastEditedAt),
userId, boardId, categoryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
public class PostCommentReactionAppender {
private final PostCommentReactionRepository postCommentReactionRepository;

@Transactional
public PostCommentReaction createPostCommentReaction(PostCommentReaction postCommentReaction) {
return postCommentReactionRepository.save(postCommentReaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ussum.homepage.domain.reaction.PostCommentReaction;
import ussum.homepage.domain.reaction.PostCommentReactionRepository;

@Service
@RequiredArgsConstructor
public class PostCommentReactionModifier {
private final PostCommentReactionRepository postCommentReactionRepository;
private final PostCommentReactionReader postCommentReactionReader;

public void deletePostCommentReaction(Long commentId, Long userId, String reaction) {
postCommentReactionRepository.delete(
postCommentReactionReader.getPostCommentReactionWithCommentIdAndUserId(commentId, userId, reaction)
);
public void deletePostCommentReaction(PostCommentReaction postCommentReaction) {
postCommentReactionRepository.delete(postCommentReaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public PostComment toDomain(PostCommentEntity postCommentEntity){
);
}
public PostCommentEntity toEntity(PostComment postComment){
LocalDateTime lastEditedAt = null;
if (postComment.getLastEditedAt() != null) {
lastEditedAt = LocalDateTime.parse(postComment.getLastEditedAt());
}
return PostCommentEntity.of(
postComment.getId(),
postComment.getContent(),
PostEntity.from(postComment.getPostId()),
UserEntity.from(postComment.getUserId()),
LocalDateTime.parse(postComment.getLastEditedAt())
lastEditedAt
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ussum.homepage.infra.jpa.comment;

import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
Expand All @@ -9,6 +10,9 @@
import ussum.homepage.global.error.exception.GeneralException;
import ussum.homepage.infra.jpa.comment.repository.PostCommentJpaRepository;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static ussum.homepage.global.error.status.ErrorStatus.POST_COMMENT_NOT_FOUND;

@Repository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ussum.homepage.infra.jpa.comment.entity;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.LastModifiedDate;
import ussum.homepage.domain.comment.PostComment;
import ussum.homepage.infra.jpa.BaseEntity;
import ussum.homepage.infra.jpa.post.entity.PostEntity;
import ussum.homepage.infra.jpa.user.entity.UserEntity;

Expand All @@ -24,6 +26,8 @@ public class PostCommentEntity {
private UserEntity userEntity;
@Enumerated(EnumType.STRING)
private CommentType type;

@LastModifiedDate
private LocalDateTime lastEditedAt;

public PostCommentEntity(Long id, String content, PostEntity postEntity, UserEntity userEntity, LocalDateTime lastEditedAt) {
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/ussum/homepage/infra/jpa/post/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,22 @@ public Post toDomain(PostEntity postEntity){
postEntity.getCreatedAt(),
postEntity.getUpdatedAt(),
postEntity.getLastEditedAt(),
postEntity.getDeletedAt(),
postEntity.getUserEntity().getId(),
postEntity.getBoardEntity().getId(),
postEntity.getCategoryEntity().getId()
);
}

public PostEntity toEntity(Post post, UserEntity user, BoardEntity board, CategoryEntity category) {
// PostEntity from = PostEntity.from(post.getId());
// LocalDateTime lastEditedAt = post.getLastEditedAt() != null ? LocalDateTime.parse(post.getLastEditedAt()) : null;
// LocalDateTime deletedAt = post.getDeletedAt() != null ? LocalDateTime.parse(post.getDeletedAt()) : null;
LocalDateTime lastEditedAt = null;
LocalDateTime deletedAt = null;
// LocalDateTime deletedAt = null;

if (post.getLastEditedAt() != null && !"null".equals(post.getLastEditedAt())) {
lastEditedAt = LocalDateTime.parse(post.getLastEditedAt());
}

if (post.getDeletedAt() != null && !"null".equals(post.getDeletedAt())) {
deletedAt = LocalDateTime.parse(post.getDeletedAt());
}
// if (post.getDeletedAt() != null && !"null".equals(post.getDeletedAt())) {
// deletedAt = LocalDateTime.parse(post.getDeletedAt());
// }

return PostEntity.of(
post.getId(),
Expand All @@ -54,7 +49,6 @@ public PostEntity toEntity(Post post, UserEntity user, BoardEntity board, Catego
post.getViewCount(),
post.getThumbnailImage(),
lastEditedAt,
deletedAt,
user,
board,
category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import ussum.homepage.infra.jpa.user.entity.UserEntity;
import ussum.homepage.infra.jpa.user.repository.UserJpaRepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static ussum.homepage.global.error.status.ErrorStatus.*;
import static ussum.homepage.infra.jpa.post.entity.PostEntity.increaseViewCount;
import static ussum.homepage.infra.jpa.post.entity.PostEntity.updateLastEditedAt;

@Repository
@RequiredArgsConstructor
Expand Down Expand Up @@ -51,6 +53,7 @@ public Optional<Post> findByBoardIdAndId(Long boardId, Long postId) {
public Optional<Post> findByBoardIdAndIdForEditAndDelete(Long boardId, Long postId) {
BoardEntity boardEntity = boardJpaRepository.findById(boardId).orElseThrow(() -> new GeneralException(BOARD_NOT_FOUND));
Optional<PostEntity> post = postJpaRepository.findByBoardEntityAndId(boardEntity, postId);
updateLastEditedAt(post.get());
return post.map(postMapper::toDomain);
}

Expand Down
19 changes: 9 additions & 10 deletions src/main/java/ussum/homepage/infra/jpa/post/entity/PostEntity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package ussum.homepage.infra.jpa.post.entity;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import org.springframework.cglib.core.Local;
import org.springframework.data.annotation.LastModifiedDate;
import ussum.homepage.infra.jpa.BaseEntity;
import ussum.homepage.infra.jpa.user.entity.UserEntity;

import java.time.LocalDateTime;
import java.util.List;

@Entity
@Table(name = "post")
Expand All @@ -23,8 +20,9 @@ public class PostEntity extends BaseEntity {
private String content;
private Integer viewCount;
private String thumbnailImage;

private LocalDateTime lastEditedAt;
private LocalDateTime deletedAt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity userEntity;
Expand All @@ -36,18 +34,19 @@ public class PostEntity extends BaseEntity {
private CategoryEntity categoryEntity;

public static PostEntity from(Long id){
return new PostEntity(id,null,null,null,null,null,null,null,null,null);
return new PostEntity(id,null,null,null,null,null,null,null,null);
}

public static PostEntity of(Long id, String title, String content, Integer viewCount, String thumbnailImage,
LocalDateTime lastEditedAt, LocalDateTime deletedAt, UserEntity user, BoardEntity board, CategoryEntity category) {
return new PostEntity(id, title, content, viewCount, thumbnailImage, lastEditedAt, deletedAt, user, board, category);
LocalDateTime lastEditedAt, UserEntity user, BoardEntity board, CategoryEntity category) {
return new PostEntity(id, title, content, viewCount, thumbnailImage, lastEditedAt, user, board, category);
}

public static void increaseViewCount(PostEntity post) {
post.viewCount += 1;
LocalDateTime updatedAt = post.getUpdatedAt();
updatedAt = LocalDateTime.now();
}

public static void updateLastEditedAt(PostEntity post) {
post.lastEditedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public interface PostJpaRepository extends JpaRepository<PostEntity,Long> {
WHERE pe.boardEntity = :board
AND (:q IS NULL OR pe.title LIKE %:q% OR pe.content LIKE %:q%)
AND (:categoryCode IS NULL OR pe.categoryEntity.majorCode = :categoryCode)
AND pe.deletedAt IS NULL
ORDER BY pe.id DESC
""")
Page<PostEntity> findBySearchCriteria(Pageable pageable,
Expand Down

0 comments on commit 3a44fbf

Please sign in to comment.