diff --git a/src/main/java/com/jiwon/mylog/domain/image/entity/ProfileImage.java b/src/main/java/com/jiwon/mylog/domain/image/entity/ProfileImage.java index 40053bc..faac0fa 100644 --- a/src/main/java/com/jiwon/mylog/domain/image/entity/ProfileImage.java +++ b/src/main/java/com/jiwon/mylog/domain/image/entity/ProfileImage.java @@ -3,6 +3,7 @@ import com.jiwon.mylog.domain.user.entity.User; import jakarta.persistence.DiscriminatorValue; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToOne; @@ -10,7 +11,7 @@ @DiscriminatorValue("PROFILE") public class ProfileImage extends Image { - @OneToOne + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; diff --git a/src/main/java/com/jiwon/mylog/domain/like/Like.java b/src/main/java/com/jiwon/mylog/domain/like/Like.java index 2d63aba..4d10f53 100644 --- a/src/main/java/com/jiwon/mylog/domain/like/Like.java +++ b/src/main/java/com/jiwon/mylog/domain/like/Like.java @@ -13,12 +13,17 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import jakarta.persistence.UniqueConstraint; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.LocalDateTime; +@Getter @EntityListeners(AuditingEntityListener.class) +@NoArgsConstructor @Entity @Table( name = "likes", @@ -44,4 +49,11 @@ public class Like { @CreatedDate @Column(nullable = false, updatable = false) private LocalDateTime createdAt; + + public static Like toLike(User user, Post post) { + Like like = new Like(); + like.user = user; + like.post = post; + return like; + } } diff --git a/src/main/java/com/jiwon/mylog/domain/like/LikeController.java b/src/main/java/com/jiwon/mylog/domain/like/LikeController.java index ae19949..79f580e 100644 --- a/src/main/java/com/jiwon/mylog/domain/like/LikeController.java +++ b/src/main/java/com/jiwon/mylog/domain/like/LikeController.java @@ -1,5 +1,6 @@ package com.jiwon.mylog.domain.like; +import com.jiwon.mylog.global.common.entity.PageResponse; import com.jiwon.mylog.global.common.entity.SliceResponse; import com.jiwon.mylog.global.security.auth.annotation.LoginUser; import lombok.RequiredArgsConstructor; @@ -47,10 +48,10 @@ public ResponseEntity getLikeStatus(@LoginUser Long userId, @PathVariab } @GetMapping("/users/{userId}/likes") - public ResponseEntity getUserLikes( + public ResponseEntity getUserLikes( @PathVariable Long userId, - @PageableDefault(sort="createdAt", direction = Sort.Direction.DESC) Pageable pageable) { - SliceResponse response = likeService.getUserLikes(userId, pageable); + @PageableDefault(size = 10, sort="createdAt", direction = Sort.Direction.DESC) Pageable pageable) { + PageResponse response = likeService.getUserLikes(userId, pageable); return ResponseEntity.ok(response); } } diff --git a/src/main/java/com/jiwon/mylog/domain/like/LikeRepository.java b/src/main/java/com/jiwon/mylog/domain/like/LikeRepository.java index 06f9217..c85ae3b 100644 --- a/src/main/java/com/jiwon/mylog/domain/like/LikeRepository.java +++ b/src/main/java/com/jiwon/mylog/domain/like/LikeRepository.java @@ -1,7 +1,8 @@ package com.jiwon.mylog.domain.like; +import com.jiwon.mylog.domain.post.entity.Post; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -20,7 +21,16 @@ public interface LikeRepository extends JpaRepository { @Query(value = "delete from likes where user_id = :userId and post_id = :postId", nativeQuery = true) void deleteLike(@Param("userId") Long userId, @Param("postId") Long postId); - Slice findAllByUserId(Long userId, Pageable pageable); + @Query("select p from Like l " + + "join l.post p " + + "left join fetch p.user u " + + "left join fetch u.profileImage " + + "left join fetch p.category " + + "left join fetch p.postTags pt " + + "left join fetch pt.tag " + + "where l.user.id = :userId and p.deletedAt is null " + + "order by l.createdAt desc") + Page findLikedPostByUserId(@Param("userId") Long userId, Pageable pageable); @Query(value = "select count(*) from likes where post_id = :postId", nativeQuery = true) long countByPostId(@Param("postId") Long postId); diff --git a/src/main/java/com/jiwon/mylog/domain/like/LikeService.java b/src/main/java/com/jiwon/mylog/domain/like/LikeService.java index 13ff88b..49fc239 100644 --- a/src/main/java/com/jiwon/mylog/domain/like/LikeService.java +++ b/src/main/java/com/jiwon/mylog/domain/like/LikeService.java @@ -1,22 +1,26 @@ package com.jiwon.mylog.domain.like; import com.jiwon.mylog.domain.event.dto.LikeCreatedEvent; +import com.jiwon.mylog.domain.post.dto.response.PostSummaryResponse; import com.jiwon.mylog.domain.post.entity.Post; import com.jiwon.mylog.domain.post.repository.PostRepository; import com.jiwon.mylog.domain.user.entity.User; import com.jiwon.mylog.domain.user.repository.UserRepository; -import com.jiwon.mylog.global.common.entity.SliceResponse; +import com.jiwon.mylog.global.common.entity.PageResponse; import com.jiwon.mylog.global.common.error.ErrorCode; import com.jiwon.mylog.global.common.error.exception.NotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @RequiredArgsConstructor @Service public class LikeService { @@ -35,7 +39,8 @@ public void createLike(Long userId, Long postId) { .orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_POST)); Long receiverId = post.getUser().getId(); - likeRepository.saveLike(userId, postId); + Like like = Like.toLike(user, post); + likeRepository.save(like); if (!receiverId.equals(userId)) { eventPublisher.publishEvent( @@ -71,15 +76,19 @@ public boolean getLikeStatus(Long userId, Long postId) { } @Transactional(readOnly = true) - public SliceResponse getUserLikes(Long userId, Pageable pageable) { + public PageResponse getUserLikes(Long userId, Pageable pageable) { validateUserExists(userId); - Slice likeSlice = likeRepository.findAllByUserId(userId, pageable); - return SliceResponse.from( - likeSlice.getContent(), - likeSlice.getNumber(), - likeSlice.getSize(), - likeSlice.isFirst(), - likeSlice.isLast() + Page postPage = likeRepository.findLikedPostByUserId(userId, pageable); + List posts = postPage.getContent().stream() + .map(PostSummaryResponse::fromPost) + .toList(); + + return PageResponse.from( + posts, + postPage.getNumber(), + postPage.getSize(), + postPage.getTotalPages(), + postPage.getTotalElements() ); } diff --git a/src/main/java/com/jiwon/mylog/domain/point/entity/Point.java b/src/main/java/com/jiwon/mylog/domain/point/entity/Point.java index eff37dd..95156fe 100644 --- a/src/main/java/com/jiwon/mylog/domain/point/entity/Point.java +++ b/src/main/java/com/jiwon/mylog/domain/point/entity/Point.java @@ -4,6 +4,7 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.EntityListeners; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -27,7 +28,7 @@ public class Point { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToOne + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; diff --git a/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java b/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java index ee2c403..a9f5d7b 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java +++ b/src/main/java/com/jiwon/mylog/domain/post/controller/PostController.java @@ -139,7 +139,7 @@ public ResponseEntity getUserPosts( @GetMapping("/users/{userId}/categories/{categoryId}/posts") @Operation( - summary = "특정 유저의 카테고리별 게시글 조회 (태그 필터링 포함)", + summary = "특정 유저의 카테고리별 게시글 조회 (태그 필터링 및 키워드 검색 포함)", responses = { @ApiResponse(responseCode = "200", description = "조회 성공"), @ApiResponse(responseCode = "404", description = "존재하지 않는 사용자 혹은 카테고리") @@ -149,9 +149,15 @@ public ResponseEntity getPostsByCategoryAndTags( @PathVariable("userId") Long userId, @PathVariable("categoryId") Long categoryId, @RequestParam(value = "tags", required = false) List tags, + @RequestParam(value = "keyword", required = false) String keyword, @PageableDefault(size = 10, page = 0, sort = "createdAt", direction = Direction.DESC) Pageable pageable) { - PageResponse response = postService.getPostsByCategoryAndTags(userId, categoryId, tags, pageable); - return new ResponseEntity<>(response, HttpStatus.OK); + if (keyword == null || keyword.isEmpty()) { + return ResponseEntity.ok(postService.getPostsByCategoryAndTags( + userId, categoryId, tags, "", pageable)); + } else { + return ResponseEntity.ok(postService.searchPosts( + userId, categoryId, tags, keyword, pageable)); + } } } diff --git a/src/main/java/com/jiwon/mylog/domain/post/dto/response/PostSummaryResponse.java b/src/main/java/com/jiwon/mylog/domain/post/dto/response/PostSummaryResponse.java index 5ab26dc..460fc7a 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/dto/response/PostSummaryResponse.java +++ b/src/main/java/com/jiwon/mylog/domain/post/dto/response/PostSummaryResponse.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.jiwon.mylog.domain.post.entity.Post; +import com.jiwon.mylog.domain.user.dto.response.UserResponse; +import com.jiwon.mylog.domain.user.dto.response.UserSummaryResponse; import com.jiwon.mylog.global.common.enums.Visibility; import com.jiwon.mylog.domain.category.dto.response.CategoryResponse; import com.jiwon.mylog.domain.post.entity.PostStatus; @@ -23,6 +25,7 @@ public class PostSummaryResponse { private final Visibility visibility; private final CategoryResponse category; private final List tags; + private final UserSummaryResponse user; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private final LocalDateTime createdAt; @@ -37,6 +40,7 @@ public static PostSummaryResponse fromPost(Post post) { .tags(post.getPostTags().stream() .map(postTag -> TagResponse.fromTag(postTag.getTag())) .toList()) + .user(UserSummaryResponse.fromUser(post.getUser())) .createdAt(post.getCreatedAt()) .build(); } diff --git a/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryCustom.java b/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryCustom.java index fc77b1a..b0e2133 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryCustom.java +++ b/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryCustom.java @@ -11,8 +11,8 @@ import java.util.Optional; public interface PostRepositoryCustom { - Page findByCategoryAndTags(Long userId, Long categoryId, List tagIds, Pageable pageable); - Page findByTags(Long userId, List tagIds, Pageable pageable); + Page findByCategoryAndTags(Long userId, Long categoryId, List tagIds, String keyword, Pageable pageable); + Page findByTags(Long userId, List tagIds, String keyword, Pageable pageable); Optional findPostDetail(Long postId); List findUserActivities(Long userId, LocalDate start, LocalDate end); diff --git a/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryImpl.java b/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryImpl.java index 4f7c507..f750b80 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryImpl.java +++ b/src/main/java/com/jiwon/mylog/domain/post/repository/PostRepositoryImpl.java @@ -5,6 +5,7 @@ import com.jiwon.mylog.domain.comment.dto.response.CommentResponse; import com.jiwon.mylog.domain.comment.entity.QComment; import com.jiwon.mylog.domain.image.entity.QProfileImage; +import com.jiwon.mylog.domain.point.entity.QPoint; import com.jiwon.mylog.domain.post.dto.response.PostDetailResponse; import com.jiwon.mylog.domain.post.entity.Post; import com.jiwon.mylog.domain.post.entity.QPost; @@ -51,14 +52,14 @@ public class PostRepositoryImpl implements PostRepositoryCustom{ private static final QComment COMMENT = QComment.comment; @Override - public Page findByCategoryAndTags(Long userId, Long categoryId, List tagIds, Pageable pageable) { - BooleanBuilder builder = buildConditions(userId, categoryId,tagIds); + public Page findByCategoryAndTags(Long userId, Long categoryId, List tagIds, String keyword, Pageable pageable) { + BooleanBuilder builder = buildConditions(userId, categoryId, tagIds, keyword); return createResult(pageable, builder); } @Override - public Page findByTags(Long userId, List tagIds, Pageable pageable) { - BooleanBuilder builder = buildConditions(userId, null, tagIds); + public Page findByTags(Long userId, List tagIds, String keyword, Pageable pageable) { + BooleanBuilder builder = buildConditions(userId, null, tagIds, keyword); return createResult(pageable, builder); } @@ -183,6 +184,8 @@ private PageImpl createResult(Pageable pageable, BooleanBuilder builder) { private List createPostsQuery(BooleanBuilder builder, Pageable pageable) { return jpaQueryFactory .selectFrom(POST) + .leftJoin(POST.user, USER).fetchJoin() + .leftJoin(POST.user.profileImage, PROFILE_IMAGE).fetchJoin() .leftJoin(POST.category, CATEGORY).fetchJoin() .leftJoin(POST.postTags, POST_TAG).fetchJoin() .leftJoin(POST_TAG.tag, TAG).fetchJoin() @@ -201,7 +204,7 @@ private Long createCountQuery(BooleanBuilder builder) { .fetchOne(); } - private BooleanBuilder buildConditions(Long userId, Long categoryId, List tagIds) { + private BooleanBuilder buildConditions(Long userId, Long categoryId, List tagIds, String keyword) { BooleanBuilder builder = new BooleanBuilder() .and(POST.user.id.eq(userId)) .and(POST.deletedAt.isNull()); @@ -221,6 +224,10 @@ private BooleanBuilder buildConditions(Long userId, Long categoryId, List builder.and(createTagExistsCondition(tagIds)); } + if (keyword != null && !keyword.isEmpty()) { + builder.and(POST.title.contains(keyword)); + } + return builder; } diff --git a/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java b/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java index 76425e0..b8890d4 100644 --- a/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java +++ b/src/main/java/com/jiwon/mylog/domain/post/service/PostService.java @@ -216,14 +216,43 @@ public PageResponse getUserPosts(Long userId, Pageable pageable) { @Cacheable(value = "post::filter", keyGenerator = "postCacheKeyGenerator") @Transactional(readOnly = true) - public PageResponse getPostsByCategoryAndTags(Long userId, Long categoryId, List tagIds, Pageable pageable) { + public PageResponse getPostsByCategoryAndTags( + Long userId, + Long categoryId, List tagIds, String keyword, + Pageable pageable) { Page postPage; if (categoryId == null || categoryId.equals(0L)) { - postPage = postRepository.findByTags(userId, tagIds, pageable); + postPage = postRepository.findByTags(userId, tagIds, keyword, pageable); } else { - postPage = postRepository.findByCategoryAndTags(userId, categoryId, tagIds, pageable); + postPage = postRepository.findByCategoryAndTags(userId, categoryId, tagIds, keyword, pageable); + } + + List posts = postPage.stream() + .map(PostSummaryResponse::fromPost) + .toList(); + + return PageResponse.from( + posts, + postPage.getNumber(), + postPage.getSize(), + postPage.getTotalPages(), + (int) postPage.getTotalElements()); + } + + @Transactional(readOnly = true) + public PageResponse searchPosts( + Long userId, + Long categoryId, List tagIds, String keyword, + Pageable pageable) { + + Page postPage; + + if (categoryId == null || categoryId.equals(0L)) { + postPage = postRepository.findByTags(userId, tagIds, keyword, pageable); + } else { + postPage = postRepository.findByCategoryAndTags(userId, categoryId, tagIds, keyword, pageable); } List posts = postPage.stream() diff --git a/src/main/java/com/jiwon/mylog/domain/user/dto/response/UserSummaryResponse.java b/src/main/java/com/jiwon/mylog/domain/user/dto/response/UserSummaryResponse.java new file mode 100644 index 0000000..eeaed20 --- /dev/null +++ b/src/main/java/com/jiwon/mylog/domain/user/dto/response/UserSummaryResponse.java @@ -0,0 +1,21 @@ +package com.jiwon.mylog.domain.user.dto.response; + +import com.jiwon.mylog.domain.user.entity.User; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@AllArgsConstructor +@Builder +@Getter +public class UserSummaryResponse { + private final String username; + private final String imageKey; + + public static UserSummaryResponse fromUser(User user) { + return UserSummaryResponse.builder() + .username(user.getUsername()) + .imageKey(user.getProfileImage() == null ? "" : user.getProfileImage().getFileKey()) + .build(); + } +} diff --git a/src/main/java/com/jiwon/mylog/domain/user/entity/User.java b/src/main/java/com/jiwon/mylog/domain/user/entity/User.java index 0913fe5..f7a7b6b 100644 --- a/src/main/java/com/jiwon/mylog/domain/user/entity/User.java +++ b/src/main/java/com/jiwon/mylog/domain/user/entity/User.java @@ -14,6 +14,7 @@ import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -70,8 +71,8 @@ public class User extends BaseEntity { @Column(nullable = false) private Role role = Role.ROLE_USER; - @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) - private Point point; +// @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) +// private Point point; @Builder.Default @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) @@ -101,11 +102,6 @@ public class User extends BaseEntity { @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List items = new ArrayList<>(); - public void initUserPoint(Point point) { - this.point = point; - point.setUser(this); - } - public void updateProfile(String username, String bio) { this.username = username; this.bio = bio; diff --git a/src/main/java/com/jiwon/mylog/global/common/cache/PostKeyGenerator.java b/src/main/java/com/jiwon/mylog/global/common/cache/PostKeyGenerator.java index a360eba..6f33686 100644 --- a/src/main/java/com/jiwon/mylog/global/common/cache/PostKeyGenerator.java +++ b/src/main/java/com/jiwon/mylog/global/common/cache/PostKeyGenerator.java @@ -15,7 +15,7 @@ public Object generate(Object target, Method method, Object... params) { Long userId = (Long) params[0]; Long categoryId = (Long) params[1]; List tagIds = (List) params[2]; - Pageable pageable = (Pageable) params[3]; + Pageable pageable = (Pageable) params[4]; String tagKey = (tagIds == null || tagIds.isEmpty()) ? "none" diff --git a/src/main/java/com/jiwon/mylog/global/oauth/CustomOAuth2UserService.java b/src/main/java/com/jiwon/mylog/global/oauth/CustomOAuth2UserService.java index 7bd3fe6..2bb4f32 100644 --- a/src/main/java/com/jiwon/mylog/global/oauth/CustomOAuth2UserService.java +++ b/src/main/java/com/jiwon/mylog/global/oauth/CustomOAuth2UserService.java @@ -1,6 +1,7 @@ package com.jiwon.mylog.global.oauth; import com.jiwon.mylog.domain.point.entity.Point; +import com.jiwon.mylog.domain.point.repository.PointRepository; import com.jiwon.mylog.domain.user.entity.User; import com.jiwon.mylog.domain.user.repository.UserRepository; import com.jiwon.mylog.global.common.error.ErrorCode; @@ -19,6 +20,7 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService { private final UserRepository userRepository; + private final PointRepository pointRepository; @Transactional @Override @@ -42,9 +44,15 @@ private User getOrSave(OAuth2UserInfo oAuth2UserInfo) { }) .orElseGet(() -> { User user = oAuth2UserInfo.toEntity(); - Point point = new Point(); - user.initUserPoint(point); - return userRepository.save(user); + User savedUser = userRepository.save(user); + initUserPoint(savedUser); + return savedUser; }); } + + private void initUserPoint(User user) { + Point point = new Point(); + point.setUser(user); + pointRepository.save(point); + } } diff --git a/src/main/java/com/jiwon/mylog/global/security/auth/service/AuthService.java b/src/main/java/com/jiwon/mylog/global/security/auth/service/AuthService.java index e718def..8c6b5b5 100644 --- a/src/main/java/com/jiwon/mylog/global/security/auth/service/AuthService.java +++ b/src/main/java/com/jiwon/mylog/global/security/auth/service/AuthService.java @@ -2,6 +2,7 @@ import com.jiwon.mylog.domain.point.entity.Point; import com.jiwon.mylog.domain.event.dto.DailyLoginEvent; +import com.jiwon.mylog.domain.point.repository.PointRepository; import com.jiwon.mylog.domain.user.dto.request.auth.PasswordResetRequest; import com.jiwon.mylog.domain.user.dto.request.auth.UserSaveRequest; import com.jiwon.mylog.domain.user.dto.response.auth.FindIdResponse; @@ -41,6 +42,7 @@ public class AuthService { private final ApplicationEventPublisher eventPublisher; private final UserRepository userRepository; + private final PointRepository pointRepository; private final MailService mailService; private final JwtService jwtService; private final TokenService tokenService; @@ -57,10 +59,8 @@ public Long save(UserSaveRequest userSaveRequest) { String encodedPassword = bCryptPasswordEncoder.encode(userSaveRequest.getPassword()); User user = userSaveRequest.toEntity(encodedPassword); - Point point = new Point(); - user.initUserPoint(point); - User savedUser = userRepository.save(user); + initUserPoint(savedUser); return savedUser.getId(); } @@ -171,4 +171,10 @@ private void validateDuplicateEmail(String email) { throw new DuplicateException(ErrorCode.DUPLICATE_EMAIL); } } + + private void initUserPoint(User savedUser) { + Point point = new Point(); + point.setUser(savedUser); + pointRepository.save(point); + } } diff --git a/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java b/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java index 8b56e45..91b7205 100644 --- a/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java +++ b/src/test/java/com/jiwon/mylog/domain/post/service/PostServiceCacheTest.java @@ -55,7 +55,7 @@ void createPost() { PostRequest postRequest = new PostRequest("title", "content", "preview", "공개", 1L, List.of(), true, PostType.NORMAL.getStatus()); postService.getUserPosts(userId, pageable); - postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), pageable); + postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), "", pageable); // when PostDetailResponse post = postService.createPost(userId, postRequest); @@ -63,14 +63,14 @@ void createPost() { postService.getPost(postId); postService.getUserPosts(userId, pageable); // 캐시 다시 생성 - postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), pageable); // 캐시 다시 생성 + postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), "", pageable); // 캐시 다시 생성 // then Assertions.assertThat(post).isNotNull(); Assertions.assertThat(post.getPostId()).isEqualTo(postId); verify(postRepository, times(2)).findAllByUser(userId, pageable); - verify(postRepository, times(2)).findByCategoryAndTags(userId, categoryId, List.of(), pageable); + verify(postRepository, times(2)).findByCategoryAndTags(userId, categoryId, List.of(), "", pageable); verify(postRepository, times(0)).findPostDetail(postId); } @@ -85,20 +85,20 @@ void updatePost() { PostRequest postRequest = new PostRequest("title", "content", "preview", "공개", 1L, List.of(), true, PostType.NORMAL.getStatus()); postService.getUserPosts(userId, pageable); // 캐시 생성 - postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), pageable); // 캐시 생성 + postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), "", pageable); // 캐시 생성 // when PostDetailResponse post = postService.updatePost(userId, postId, postRequest); postService.getPost(postId); postService.getUserPosts(userId, pageable); // 캐시 다시 생성 - postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), pageable); // 캐시 다시 생성 + postService.getPostsByCategoryAndTags(userId, categoryId, List.of(), "", pageable); // 캐시 다시 생성 // then Assertions.assertThat(post).isNotNull(); Assertions.assertThat(post.getPostId()).isEqualTo(postId); verify(postRepository, times(2)).findAllByUser(userId, pageable); - verify(postRepository, times(2)).findByCategoryAndTags(userId, categoryId, List.of(), pageable); + verify(postRepository, times(2)).findByCategoryAndTags(userId, categoryId, List.of(), "", pageable); verify(postRepository, times(0)).findPostDetail(postId); } @@ -175,14 +175,14 @@ void getPostsByCategoryAndTags() { Pageable pageable = PageRequest.of(0, 10); // when - postService.getPostsByCategoryAndTags(userId, categoryId, tagIds, pageable); - postService.getPostsByCategoryAndTags(userId, categoryId, tagIds, pageable); + postService.getPostsByCategoryAndTags(userId, categoryId, tagIds, "", pageable); + postService.getPostsByCategoryAndTags(userId, categoryId, tagIds, "", pageable); - postService.getPostsByCategoryAndTags(userId, 0L, tagIds, pageable); - postService.getPostsByCategoryAndTags(userId, 0L, tagIds, pageable); + postService.getPostsByCategoryAndTags(userId, 0L, tagIds, "", pageable); + postService.getPostsByCategoryAndTags(userId, 0L, tagIds, "", pageable); // then - verify(postRepository, times(1)).findByCategoryAndTags(userId, categoryId, tagIds, pageable); - verify(postRepository, times(1)).findByTags(userId, tagIds, pageable); + verify(postRepository, times(1)).findByCategoryAndTags(userId, categoryId, tagIds, "", pageable); + verify(postRepository, times(1)).findByTags(userId, tagIds, "", pageable); } } \ No newline at end of file