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..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 @@ -1,98 +1,91 @@ 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.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(); - @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); + } - return getSliceImpl(albumList, pageable); - } + @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); + } - @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); - } + private Slice getSliceImpl(List list, Pageable pageable) { + boolean hasNext = false; + if (list.size() > pageable.getPageSize()) { + hasNext = true; + list.remove(list.size() - 1); + } - private Slice getSliceImpl(List list, Pageable pageable){ - boolean hasNext = false; - if (list.size() > pageable.getPageSize()) { - hasNext = true; - list.remove(list.size() - 1); - } + return new SliceImpl<>(list, pageable, hasNext); + } - return new SliceImpl<>(list, pageable, hasNext); - } + private BooleanExpression getTagsInQuery(List emotionTagList) { + if (emotionTagList == null || emotionTagList.isEmpty()) { + return null; + } + return QAlbum.album.tags.any().in( + getTagsInSubQuery(emotionTagList)); + } - private BooleanExpression getTagsInQuery(List emotionTagList) { - if(emotionTagList == null || emotionTagList.isEmpty()) { - return null; - } - return QAlbum.album.tags.any().in( - getTagsInSubQuery(emotionTagList)); - } + private JPQLQuery getTagsInSubQuery(List emotionTagList) { + return JPAExpressions.select(QTag.tag) + .from(QTag.tag) + .where(QTag.tag.emotionTag.in(emotionTagList)); + } - private JPQLQuery getTagsInSubQuery(List emotionTagList) { - return JPAExpressions.select(QTag.tag) - .from(QTag.tag) - .where(QTag.tag.emotionTag.in(emotionTagList)); - } + 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(); + } - 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/comment/domain/entity/Comment.java b/src/main/java/com/kusitms/samsion/domain/comment/domain/entity/Comment.java index eef10af..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; @@ -16,6 +17,7 @@ @Entity @NoArgsConstructor(access = lombok.AccessLevel.PROTECTED) @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 93034f3..2175787 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 albumId,@PathVariable Long commentId){ commentDeleteUseCase.deleteComment(commentId); 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..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,18 +1,17 @@ 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.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; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.context.ApplicationEventPublisher; -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 + ApplicationEventPublisher applicationEventPublisher; UserSignUpHandler userSignUpHandler; @BeforeEach void setUp() { - userSignUpHandler = new UserSignUpHandler(userSaveService); + userSignUpHandler = new UserSignUpHandler(userSaveService, applicationEventPublisher); } @Test