From 4aec3cba38ab6a2a17f1d020c062bb87ad9d7da7 Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 24 May 2023 18:51:41 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[fix]=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=EC=8B=9C=20grid=20=EA=B0=99=EC=9D=B4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infrastructure/AlbumRepositoryImpl.java | 149 +++++++++--------- .../handler/GridUpdateHandler.java | 10 +- .../application/service/StampGetUseCase.java | 8 +- .../domain/grid/domain/entity/Grid.java | 17 +- .../handler/UserSignUpHandler.java | 16 +- .../user/domain/service/UserSaveService.java | 4 + .../handler/UserSignUpHandlerTest.java | 15 +- 7 files changed, 107 insertions(+), 112 deletions(-) diff --git a/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java b/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java index 818eaf5..38be7ba 100644 --- a/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java +++ b/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java @@ -1,98 +1,101 @@ package com.kusitms.samsion.domain.album.infrastructure; -import java.util.List; - -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.springframework.data.domain.SliceImpl; -import org.springframework.stereotype.Repository; - import com.kusitms.samsion.common.querydsl.OrderByNull; -import com.kusitms.samsion.domain.album.domain.entity.SortType; -import com.kusitms.samsion.domain.album.domain.entity.Album; -import com.kusitms.samsion.domain.album.domain.entity.EmotionTag; -import com.kusitms.samsion.domain.album.domain.entity.QAlbum; -import com.kusitms.samsion.domain.album.domain.entity.QTag; -import com.kusitms.samsion.domain.album.domain.entity.Tag; -import com.kusitms.samsion.domain.album.domain.entity.Visibility; +import com.kusitms.samsion.domain.album.domain.entity.*; import com.kusitms.samsion.domain.album.domain.repository.AlbumRepositoryCustom; +import com.kusitms.samsion.domain.comment.domain.entity.QComment; +import com.querydsl.core.types.Expression; +import com.querydsl.core.types.Order; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.JPQLQuery; import com.querydsl.jpa.impl.JPAQueryFactory; - import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.SliceImpl; +import org.springframework.stereotype.Repository; + +import java.util.List; @Repository @RequiredArgsConstructor public class AlbumRepositoryImpl implements AlbumRepositoryCustom { - private final JPAQueryFactory jpaQueryFactory; + private final JPAQueryFactory jpaQueryFactory; + + @Override + public Slice findAlbumList(Pageable pageable, List emotionTagList, SortType sortType) { + List albumList = jpaQueryFactory + .select(QAlbum.album).distinct() + .from(QAlbum.album) + .where(QAlbum.album.visibility.eq(Visibility.PUBLIC), + getTagsInQuery(emotionTagList)) + .orderBy(getSortedColumn(sortType), + QAlbum.album.createdDate.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize() + 1) + .fetch(); + + return getSliceImpl(albumList, pageable); + } - @Override - public Slice findAlbumList(Pageable pageable, List emotionTagList, SortType sortType) { - List albumList = jpaQueryFactory - .select(QAlbum.album).distinct() - .from(QAlbum.album) - .where(QAlbum.album.visibility.eq(Visibility.PUBLIC), - getTagsInQuery(emotionTagList)) - .orderBy(getSortedColumn(sortType), - QAlbum.album.createdDate.desc()) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize() + 1) - .fetch(); + @Override + public Slice findMyAlbumList(Pageable pageable, List emotionTagList, SortType sortType, Long userId) { + List albumList = jpaQueryFactory + .select(QAlbum.album).distinct() + .from(QAlbum.album) + .where(QAlbum.album.writer.id.eq(userId), + getTagsInQuery(emotionTagList)) + .orderBy(getSortedColumn(sortType), + QAlbum.album.createdDate.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize() + 1) + .fetch(); + return getSliceImpl(albumList, pageable); + } - return getSliceImpl(albumList, pageable); - } + private Slice getSliceImpl(List list, Pageable pageable) { + boolean hasNext = false; + if (list.size() > pageable.getPageSize()) { + hasNext = true; + list.remove(list.size() - 1); + } - @Override - public Slice findMyAlbumList(Pageable pageable, List emotionTagList, SortType sortType, Long userId) { - List albumList = jpaQueryFactory - .select(QAlbum.album).distinct() - .from(QAlbum.album) - .where(QAlbum.album.writer.id.eq(userId), - getTagsInQuery(emotionTagList)) - .orderBy(getSortedColumn(sortType), - QAlbum.album.createdDate.desc()) - .offset(pageable.getOffset()) - .limit(pageable.getPageSize() + 1) - .fetch(); - return getSliceImpl(albumList, pageable); - } + return new SliceImpl<>(list, pageable, hasNext); + } - private Slice getSliceImpl(List list, Pageable pageable){ - boolean hasNext = false; - if (list.size() > pageable.getPageSize()) { - hasNext = true; - list.remove(list.size() - 1); - } + private BooleanExpression getTagsInQuery(List emotionTagList) { + if (emotionTagList == null || emotionTagList.isEmpty()) { + return null; + } + return QAlbum.album.tags.any().in( + getTagsInSubQuery(emotionTagList)); + } - return new SliceImpl<>(list, pageable, hasNext); - } + private JPQLQuery getTagsInSubQuery(List emotionTagList) { + return JPAExpressions.select(QTag.tag) + .from(QTag.tag) + .where(QTag.tag.emotionTag.in(emotionTagList)); + } - private BooleanExpression getTagsInQuery(List emotionTagList) { - if(emotionTagList == null || emotionTagList.isEmpty()) { - return null; - } - return QAlbum.album.tags.any().in( - getTagsInSubQuery(emotionTagList)); - } + private OrderSpecifier getSortedColumn(SortType sortType) { + switch (sortType) { + case EMPATHY: + return QAlbum.album.empathies.size().desc(); + case COMMENT: + return new OrderSpecifier<>(Order.DESC, getCommentSubQuery()); + } + return OrderByNull.getDefault(); + } - private JPQLQuery getTagsInSubQuery(List emotionTagList) { - return JPAExpressions.select(QTag.tag) - .from(QTag.tag) - .where(QTag.tag.emotionTag.in(emotionTagList)); - } + private Expression getCommentSubQuery() { + return JPAExpressions.select(QComment.comment.id.count()) + .from(QComment.comment) + .where(QComment.comment.deleted.isFalse(), + QComment.comment.album.id.eq(QAlbum.album.id)); + } - private OrderSpecifier getSortedColumn(SortType sortType) { - switch (sortType) { - case EMPATHY: - return QAlbum.album.empathies.size().desc(); - case COMMENT: - return QAlbum.album.comments.size().desc(); - } - return OrderByNull.getDefault(); - } } diff --git a/src/main/java/com/kusitms/samsion/domain/grid/application/handler/GridUpdateHandler.java b/src/main/java/com/kusitms/samsion/domain/grid/application/handler/GridUpdateHandler.java index ab2ffc7..1eb9160 100644 --- a/src/main/java/com/kusitms/samsion/domain/grid/application/handler/GridUpdateHandler.java +++ b/src/main/java/com/kusitms/samsion/domain/grid/application/handler/GridUpdateHandler.java @@ -1,23 +1,19 @@ package com.kusitms.samsion.domain.grid.application.handler; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.transaction.event.TransactionalEventListener; - import com.kusitms.samsion.common.annotation.UseCase; -import com.kusitms.samsion.common.util.UserUtils; import com.kusitms.samsion.domain.grid.application.dto.request.GridUpdateRequest; import com.kusitms.samsion.domain.grid.domain.entity.Grid; import com.kusitms.samsion.domain.grid.domain.service.GridQueryService; - import lombok.RequiredArgsConstructor; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.event.TransactionalEventListener; @UseCase @RequiredArgsConstructor @Transactional(propagation = Propagation.REQUIRES_NEW) public class GridUpdateHandler { - private final UserUtils userUtils; private final GridQueryService gridQueryService; @TransactionalEventListener diff --git a/src/main/java/com/kusitms/samsion/domain/grid/application/service/StampGetUseCase.java b/src/main/java/com/kusitms/samsion/domain/grid/application/service/StampGetUseCase.java index eaccd38..281e135 100644 --- a/src/main/java/com/kusitms/samsion/domain/grid/application/service/StampGetUseCase.java +++ b/src/main/java/com/kusitms/samsion/domain/grid/application/service/StampGetUseCase.java @@ -1,9 +1,5 @@ package com.kusitms.samsion.domain.grid.application.service; -import java.util.List; - -import org.springframework.transaction.annotation.Transactional; - import com.kusitms.samsion.common.annotation.UseCase; import com.kusitms.samsion.common.slice.ListResponse; import com.kusitms.samsion.common.util.UserUtils; @@ -11,8 +7,10 @@ import com.kusitms.samsion.domain.grid.application.mapper.GridMapper; import com.kusitms.samsion.domain.grid.domain.entity.Grid; import com.kusitms.samsion.domain.grid.domain.service.GridQueryService; - import lombok.RequiredArgsConstructor; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; @UseCase @Transactional(readOnly = true) diff --git a/src/main/java/com/kusitms/samsion/domain/grid/domain/entity/Grid.java b/src/main/java/com/kusitms/samsion/domain/grid/domain/entity/Grid.java index bd68b71..32957bc 100644 --- a/src/main/java/com/kusitms/samsion/domain/grid/domain/entity/Grid.java +++ b/src/main/java/com/kusitms/samsion/domain/grid/domain/entity/Grid.java @@ -1,25 +1,14 @@ package com.kusitms.samsion.domain.grid.domain.entity; -import java.util.Objects; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; - import com.kusitms.samsion.common.domain.BaseEntity; import com.kusitms.samsion.domain.user.domain.entity.User; - import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import javax.persistence.*; +import java.util.Objects; + @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandler.java b/src/main/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandler.java index 3f511f6..c011003 100644 --- a/src/main/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandler.java +++ b/src/main/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandler.java @@ -1,16 +1,16 @@ package com.kusitms.samsion.domain.user.application.handler; -import org.springframework.context.event.EventListener; -import org.springframework.transaction.annotation.Transactional; - +import com.kusitms.samsion.common.annotation.UseCase; +import com.kusitms.samsion.domain.grid.application.dto.request.GridCreateRequest; import com.kusitms.samsion.domain.user.application.dto.request.UserSignUpRequest; import com.kusitms.samsion.domain.user.application.mapper.UserMapper; -import com.kusitms.samsion.common.annotation.UseCase; import com.kusitms.samsion.domain.user.domain.entity.User; import com.kusitms.samsion.domain.user.domain.service.UserSaveService; - import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.event.EventListener; +import org.springframework.transaction.annotation.Transactional; @Slf4j @UseCase @@ -19,11 +19,15 @@ public class UserSignUpHandler { private final UserSaveService userSaveService; + private final ApplicationEventPublisher applicationEventPublisher; + @Transactional @EventListener public void signUp(UserSignUpRequest request){ - log.info("request {}", request); final User user = UserMapper.toEntity(request); userSaveService.saveUser(user); + userSaveService.flush(); + applicationEventPublisher.publishEvent(new GridCreateRequest(user.getId())); } + } diff --git a/src/main/java/com/kusitms/samsion/domain/user/domain/service/UserSaveService.java b/src/main/java/com/kusitms/samsion/domain/user/domain/service/UserSaveService.java index 98d1a86..a61186d 100644 --- a/src/main/java/com/kusitms/samsion/domain/user/domain/service/UserSaveService.java +++ b/src/main/java/com/kusitms/samsion/domain/user/domain/service/UserSaveService.java @@ -21,4 +21,8 @@ public void saveUser(User user) { } } + public void flush(){ + userRepository.flush(); + } + } diff --git a/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java b/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java index 252ba83..7cc7b2f 100644 --- a/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java +++ b/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java @@ -1,7 +1,9 @@ package com.kusitms.samsion.domain.user.application.handler; -import static org.mockito.BDDMockito.*; - +import com.kusitms.samsion.common.consts.TestConst; +import com.kusitms.samsion.domain.grid.domain.service.GridSaveService; +import com.kusitms.samsion.domain.user.application.dto.request.UserSignUpRequest; +import com.kusitms.samsion.domain.user.domain.service.UserSaveService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -9,10 +11,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.kusitms.samsion.domain.user.application.dto.request.UserSignUpRequest; -import com.kusitms.samsion.common.consts.TestConst; -import com.kusitms.samsion.domain.user.application.handler.UserSignUpHandler; -import com.kusitms.samsion.domain.user.domain.service.UserSaveService; +import static org.mockito.BDDMockito.*; @ExtendWith(MockitoExtension.class) @DisplayName("UserSignUpHandler 테스트") @@ -20,12 +19,14 @@ class UserSignUpHandlerTest { @Mock UserSaveService userSaveService; + @Mock + GridSaveService gridSaveService; UserSignUpHandler userSignUpHandler; @BeforeEach void setUp() { - userSignUpHandler = new UserSignUpHandler(userSaveService); + userSignUpHandler = new UserSignUpHandler(userSaveService, gridSaveService); } @Test From 40d12d1d16e42408853787930e4437f8a2d955f6 Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 24 May 2023 18:52:46 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[test]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/application/handler/UserSignUpHandlerTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java b/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java index 7cc7b2f..1f90fb6 100644 --- a/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java +++ b/src/test/java/com/kusitms/samsion/domain/user/application/handler/UserSignUpHandlerTest.java @@ -1,7 +1,6 @@ package com.kusitms.samsion.domain.user.application.handler; import com.kusitms.samsion.common.consts.TestConst; -import com.kusitms.samsion.domain.grid.domain.service.GridSaveService; import com.kusitms.samsion.domain.user.application.dto.request.UserSignUpRequest; import com.kusitms.samsion.domain.user.domain.service.UserSaveService; import org.junit.jupiter.api.BeforeEach; @@ -10,6 +9,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.context.ApplicationEventPublisher; import static org.mockito.BDDMockito.*; @@ -20,13 +20,13 @@ class UserSignUpHandlerTest { @Mock UserSaveService userSaveService; @Mock - GridSaveService gridSaveService; + ApplicationEventPublisher applicationEventPublisher; UserSignUpHandler userSignUpHandler; @BeforeEach void setUp() { - userSignUpHandler = new UserSignUpHandler(userSaveService, gridSaveService); + userSignUpHandler = new UserSignUpHandler(userSaveService, applicationEventPublisher); } @Test From e6873d1bf3e13cc5ff1e107e3b99cce2e3618a35 Mon Sep 17 00:00:00 2001 From: tlarbals824 Date: Wed, 24 May 2023 19:34:53 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[fix]=20@Where=EC=9D=84=20=ED=86=B5?= =?UTF-8?q?=ED=95=B4=20select=20=EC=BF=BC=EB=A6=AC=20=EC=A1=B0=EA=B1=B4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EA=B3=A0=EC=95=84=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../album/infrastructure/AlbumRepositoryImpl.java | 12 +----------- .../domain/comment/domain/entity/Comment.java | 6 ++++-- .../comment/presentation/CommentController.java | 2 +- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java b/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java index 38be7ba..8825cac 100644 --- a/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java +++ b/src/main/java/com/kusitms/samsion/domain/album/infrastructure/AlbumRepositoryImpl.java @@ -3,9 +3,6 @@ import com.kusitms.samsion.common.querydsl.OrderByNull; import com.kusitms.samsion.domain.album.domain.entity.*; import com.kusitms.samsion.domain.album.domain.repository.AlbumRepositoryCustom; -import com.kusitms.samsion.domain.comment.domain.entity.QComment; -import com.querydsl.core.types.Expression; -import com.querydsl.core.types.Order; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.JPAExpressions; @@ -85,17 +82,10 @@ private OrderSpecifier getSortedColumn(SortType sortType) { case EMPATHY: return QAlbum.album.empathies.size().desc(); case COMMENT: - return new OrderSpecifier<>(Order.DESC, getCommentSubQuery()); + return QAlbum.album.comments.size().desc(); } return OrderByNull.getDefault(); } - private Expression getCommentSubQuery() { - return JPAExpressions.select(QComment.comment.id.count()) - .from(QComment.comment) - .where(QComment.comment.deleted.isFalse(), - QComment.comment.album.id.eq(QAlbum.album.id)); - } - } diff --git a/src/main/java/com/kusitms/samsion/domain/comment/domain/entity/Comment.java b/src/main/java/com/kusitms/samsion/domain/comment/domain/entity/Comment.java index 6e79c7b..3b324b2 100644 --- a/src/main/java/com/kusitms/samsion/domain/comment/domain/entity/Comment.java +++ b/src/main/java/com/kusitms/samsion/domain/comment/domain/entity/Comment.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; import javax.persistence.*; import java.util.ArrayList; @@ -15,7 +16,8 @@ @Getter @Entity @NoArgsConstructor(access = lombok.AccessLevel.PROTECTED) -@SQLDelete(sql = "UPDATE comment SET deleted = true WHERE id = ?") +@SQLDelete(sql = "UPDATE comment SET deleted = true WHERE comment_id = ?") +@Where(clause = "deleted = false") public class Comment extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -40,7 +42,7 @@ public class Comment extends BaseEntity { private Comment parent; //자식 정의 (대댓글) - @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) private List childList = new ArrayList<>(); public void addChild(Comment child){ diff --git a/src/main/java/com/kusitms/samsion/domain/comment/presentation/CommentController.java b/src/main/java/com/kusitms/samsion/domain/comment/presentation/CommentController.java index 72ac0b9..8abbfb4 100644 --- a/src/main/java/com/kusitms/samsion/domain/comment/presentation/CommentController.java +++ b/src/main/java/com/kusitms/samsion/domain/comment/presentation/CommentController.java @@ -43,7 +43,7 @@ public CommentInfoResponse update(@PathVariable Long commentId, @RequestBody Com return commentUpdateUseCase.updateComment(commentId, commentUpdateRequest); } - @CacheEvict(value = CachingStoreConst.COMMENT_COUNT_CACHE_NAME, key = "#albumId") + @CacheEvict(value = CachingStoreConst.COMMENT_COUNT_CACHE_NAME, key = "#albumId",condition="#albumId!=null") @DeleteMapping("/{albumId}/comment/{commentId}") public void delete(@PathVariable Long commentId){ commentDeleteUseCase.deleteComment(commentId);