diff --git a/src/main/java/com/seoultech/synergybe/domain/comment/dto/response/GetCommentResponse.java b/src/main/java/com/seoultech/synergybe/domain/comment/dto/response/GetCommentResponse.java index a82fe1d2..20af2ccc 100644 --- a/src/main/java/com/seoultech/synergybe/domain/comment/dto/response/GetCommentResponse.java +++ b/src/main/java/com/seoultech/synergybe/domain/comment/dto/response/GetCommentResponse.java @@ -6,9 +6,9 @@ @Builder public record GetCommentResponse( - String commentId, - String userId, - String postId, + String commentToken, + String userToken, + String postToken, String comment, LocalDateTime updateAt ) { diff --git a/src/main/java/com/seoultech/synergybe/domain/comment/service/CommentService.java b/src/main/java/com/seoultech/synergybe/domain/comment/service/CommentService.java index 227c9cdc..6049ef37 100644 --- a/src/main/java/com/seoultech/synergybe/domain/comment/service/CommentService.java +++ b/src/main/java/com/seoultech/synergybe/domain/comment/service/CommentService.java @@ -11,7 +11,7 @@ import com.seoultech.synergybe.domain.common.paging.ListResponse; import com.seoultech.synergybe.domain.notification.service.NotificationService; import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.implement.PostReader; +import com.seoultech.synergybe.domain.post.repository.PostRepository; import com.seoultech.synergybe.domain.user.User; import com.seoultech.synergybe.domain.user.service.UserServiceImpl; import lombok.RequiredArgsConstructor; @@ -25,15 +25,15 @@ @Transactional @RequiredArgsConstructor public class CommentService { + private final PostRepository postRepository; private final CommentRepository commentRepository; - private final PostReader postReader; private final UserServiceImpl userService; private final NotificationService notificationService; private final IdGenerator idGenerator; private final TokenGenerator tokenGenerator; public GetCommentResponse createComment(String userId, CreateCommentRequest request) { - Post post = postReader.read(request.postId()); + Post post = postRepository.findByPostToken(request.postId()); User user = userService.getUserByToken(userId); Long commentId = idGenerator.generateId(); String commentToken = tokenGenerator.generateToken(IdPrefix.COMMENT); @@ -47,10 +47,9 @@ public GetCommentResponse createComment(String userId, CreateCommentRequest requ Comment savedComment = commentRepository.save(comment); savedComment.addPost(post); -// User postUser = post.getUser(); // notificationService.send(postUser, NotificationType.COMMENT, "댓글이 생성되었습니다", post.getId()); return GetCommentResponse.builder() - .commentId(savedComment.getCommentToken()) + .commentToken(savedComment.getCommentToken()) .build(); } diff --git a/src/main/java/com/seoultech/synergybe/domain/follow/controller/FollowController.java b/src/main/java/com/seoultech/synergybe/domain/follow/controller/FollowController.java index 71f56de9..38b80bb8 100644 --- a/src/main/java/com/seoultech/synergybe/domain/follow/controller/FollowController.java +++ b/src/main/java/com/seoultech/synergybe/domain/follow/controller/FollowController.java @@ -45,6 +45,4 @@ public ResponseEntity> getFollowingIds(@LoginUser String us return ResponseEntity.status(HttpStatus.OK).body(followService.getFollowingIds(userId)); } - - } diff --git a/src/main/java/com/seoultech/synergybe/domain/follow/implement/FollowReader.java b/src/main/java/com/seoultech/synergybe/domain/follow/implement/FollowReader.java index a293f364..ba21b0ad 100644 --- a/src/main/java/com/seoultech/synergybe/domain/follow/implement/FollowReader.java +++ b/src/main/java/com/seoultech/synergybe/domain/follow/implement/FollowReader.java @@ -11,7 +11,7 @@ public class FollowReader { private final FollowRepository followRepository; - public List readFollowingIds(String userId) { - return followRepository.findFollowingIdsByFollowerId(userId); + public List readFollowingIds(String userToken) { + return followRepository.findFollowingIdsByFollowerToken(userToken); } } diff --git a/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepository.java b/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepository.java index e6f1d936..ff34bfd5 100644 --- a/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepository.java +++ b/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepository.java @@ -10,14 +10,6 @@ import java.util.Optional; @Repository -public interface FollowRepository extends JpaRepository { +public interface FollowRepository extends JpaRepository, FollowRepositoryCustom { - @Query(value = "SELECT following_id FROM follow WHERE follower_id = :userId AND status = 'FOLLOW'", nativeQuery = true) - List findFollowingIdsByFollowerId(@Param("userId") String userId); - - @Query(value = "SELECT follower_id FROM follow WHERE following_id = :userId AND status = 'FOLLOW'", nativeQuery = true) - List findFollowerIdsByFollowingId(@Param("userId") String userId); - - @Query(value = "SELECT * FROM follow WHERE follower_id = :userId AND following_id = :followingId FOR UPDATE", nativeQuery = true) - Optional findByFollowerIdAndFollowingId(@Param("userId") String userId, @Param("followingId") String followingId); } diff --git a/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepositoryCustom.java b/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepositoryCustom.java new file mode 100644 index 00000000..481fb66d --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepositoryCustom.java @@ -0,0 +1,11 @@ +package com.seoultech.synergybe.domain.follow.repository; + +import com.seoultech.synergybe.domain.follow.Follow; + +import java.util.List; + +public interface FollowRepositoryCustom { + List findFollowingIdsByFollowerToken(String followerToken); + List findFollowerIdsByFollowingToken(String followingToken); + Follow findByFollowerTokenAndFollowingToken(String userToken, String followingToken); +} diff --git a/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepositoryCustomImpl.java b/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepositoryCustomImpl.java new file mode 100644 index 00000000..f838ca8c --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/follow/repository/FollowRepositoryCustomImpl.java @@ -0,0 +1,45 @@ +package com.seoultech.synergybe.domain.follow.repository; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import com.seoultech.synergybe.domain.follow.Follow; +import com.seoultech.synergybe.domain.follow.FollowStatus; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.util.List; + +import static com.seoultech.synergybe.domain.follow.QFollow.follow; + +@RequiredArgsConstructor +@Repository +public class FollowRepositoryCustomImpl implements FollowRepositoryCustom { + private final JPAQueryFactory queryFactory; + + @Override + public List findFollowingIdsByFollowerToken(String followerToken) { + return queryFactory + .select(follow.follower.id) + .where( + follow.follower.userToken.eq(followerToken).and(follow.status.eq(FollowStatus.FOLLOW))) + .fetch(); + } + + @Override + public List findFollowerIdsByFollowingToken(String followingToken) { + return queryFactory + .select(follow.follower.id) + .where( + follow.following.userToken.eq(followingToken).and(follow.status.eq(FollowStatus.FOLLOW))) + .fetch(); + } + + @Override + public Follow findByFollowerTokenAndFollowingToken(String userToken, String followingToken) { + return queryFactory + .selectFrom(follow) + .where( + follow.follower.userToken.eq(userToken).and(follow.following.userToken.eq(followingToken)) + ) + .fetchOne(); + } +} diff --git a/src/main/java/com/seoultech/synergybe/domain/follow/service/FollowService.java b/src/main/java/com/seoultech/synergybe/domain/follow/service/FollowService.java index 2642b77d..47dd6e4c 100644 --- a/src/main/java/com/seoultech/synergybe/domain/follow/service/FollowService.java +++ b/src/main/java/com/seoultech/synergybe/domain/follow/service/FollowService.java @@ -30,10 +30,6 @@ public class FollowService { private final IdGenerator idGenerator; private final TokenGenerator tokenGenerator; - public List findFollowingIdsByUserId(String userId) { - return followRepository.findFollowingIdsByFollowerId(userId); - } - /** * * @param user 신청한 유저 @@ -64,14 +60,14 @@ public GetFollowResponse updateFollow(User user, String followingToken, CreateFo } public synchronized Follow update(User user, String followingId, FollowStatus status) { - Optional followOptional = followRepository.findByFollowerIdAndFollowingId(user.getUserToken(), followingId); + Follow followOptional = followRepository.findByFollowerTokenAndFollowingToken(user.getUserToken(), followingId); - if (followOptional.isPresent()) { - followOptional.get().updateStatus(status); + if (followOptional != null) { + followOptional.updateStatus(status); User following = userService.getUserByToken(followingId); // notificationService.send(following, NotificationType.FOLLOW, "팔로우 신청이 완료되었습니다.", Long.valueOf(followingId)); - return followOptional.get(); + return followOptional; } else { User following = userService.getUserByToken(followingId); Long followId = idGenerator.generateId(); @@ -88,27 +84,23 @@ public synchronized Follow update(User user, String followingId, FollowStatus st } } - public List getFollowerIdList(String userId) { - return followRepository.findFollowerIdsByFollowingId(userId); + public List getFollowerIdList(String userToken) { + return followRepository.findFollowerIdsByFollowingToken(userToken); } - public List getFollowingIdList(String userId) { - return followRepository.findFollowingIdsByFollowerId(userId); + public List getFollowingIdList(String userToken) { + return followRepository.findFollowingIdsByFollowerToken(userToken); } - public ListResponse getFollowerIds(String userId) { - List getFollowerIdList = getFollowerIdList(userId); + public ListResponse getFollowerIds(String userToken) { + List getFollowerIdList = getFollowerIdList(userToken); - ListResponse getUserIdListResponses = new ListResponse(getFollowerIdList); - - return getUserIdListResponses; + return new ListResponse(getFollowerIdList); } - public ListResponse getFollowingIds(String userId) { - List getFollowingIdList = getFollowingIdList(userId); - - ListResponse getUserIdListResponses = new ListResponse(getFollowingIdList); + public ListResponse getFollowingIds(String userToken) { + List getFollowingIdList = getFollowingIdList(userToken); - return getUserIdListResponses; + return new ListResponse(getFollowingIdList); } } diff --git a/src/main/java/com/seoultech/synergybe/domain/post/business/PostService.java b/src/main/java/com/seoultech/synergybe/domain/post/business/PostService.java deleted file mode 100644 index 9c2a8bdf..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/business/PostService.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.seoultech.synergybe.domain.post.business; - -import com.seoultech.synergybe.domain.common.paging.ListResponse; -import com.seoultech.synergybe.domain.follow.implement.FollowReader; -import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.business.dto.UpdatePostDto; -import com.seoultech.synergybe.domain.post.implement.*; -import com.seoultech.synergybe.domain.post.business.dto.CreatePostDto; -import com.seoultech.synergybe.domain.post.presentation.dto.response.GetPostResponse; -import com.seoultech.synergybe.domain.user.User; -import com.seoultech.synergybe.domain.user.repository.UserReader; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -@RequiredArgsConstructor -public class PostService { - private final UserReader userReader; - private final FollowReader followReader; - private final PostReader postReader; - private final PostFactory postFactory; - private final PostManager postManager; - private final PostValidator postValidator; - - public GetPostResponse createPost(String userId, CreatePostDto postData) { - User user = userReader.read(userId); - Post post = postFactory.createPost(user, postData.title(), postData.content(), postData.files()); - postManager.save(post); - - return GetPostResponse.builder() - .postToken(post.getPostToken()) - .build(); - } - - public GetPostResponse updatePost(String userId, UpdatePostDto updatePostDto) { - User user = userReader.read(userId); - Post post = postReader.read(updatePostDto.postId()); - - postValidator.validateUser(user, post); - postManager.update(post, updatePostDto); - - return GetPostResponse.builder() - .postToken(post.getPostToken()) - .build(); - } - - public void deletePost(String userId, String postId) { - User user = userReader.read(userId); - Post post = postReader.read(postId); - - postValidator.validateUser(user, post); - postManager.delete(post); - } - - public ListResponse getRecentList(Long offset) { - - return postReader.readRecentList(offset); - } - - public GetPostResponse getPost(String postId) { - Post post = postReader.read(postId); - System.out.println(postId); - - return PostMapperEntityToDto.postToResponse(post); - } - - public ListResponse getFeed(Long end, String userId) { - List followingIds = followReader.readFollowingIds(userId); - System.out.println(followingIds.size()); - - return postReader.readFeed(followingIds); - } - - public ListResponse getList(String userId) { - return postReader.readListByUser(userId); - } - -// Page searchAllPosts(String keyword, Pageable pageable); -// -// Specification search(String keyword); - - public ListResponse getWeekBestPostList() { - return postReader.readWeekBest(); - } -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/PostController.java b/src/main/java/com/seoultech/synergybe/domain/post/controller/PostController.java similarity index 51% rename from src/main/java/com/seoultech/synergybe/domain/post/presentation/PostController.java rename to src/main/java/com/seoultech/synergybe/domain/post/controller/PostController.java index c2519d38..dda234ab 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/PostController.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/controller/PostController.java @@ -1,12 +1,13 @@ -package com.seoultech.synergybe.domain.post.presentation; +package com.seoultech.synergybe.domain.post.controller; import com.seoultech.synergybe.domain.common.paging.ListResponse; -import com.seoultech.synergybe.domain.post.business.dto.CreatePostDto; -import com.seoultech.synergybe.domain.post.business.dto.UpdatePostDto; -import com.seoultech.synergybe.domain.post.presentation.dto.request.CreatePostRequest; -import com.seoultech.synergybe.domain.post.presentation.dto.request.UpdatePostRequest; -import com.seoultech.synergybe.domain.post.presentation.dto.response.GetPostResponse; -import com.seoultech.synergybe.domain.post.business.PostService; +import com.seoultech.synergybe.domain.post.Post; +import com.seoultech.synergybe.domain.post.service.dto.CreatePostDto; +import com.seoultech.synergybe.domain.post.service.dto.UpdatePostDto; +import com.seoultech.synergybe.domain.post.controller.dto.request.CreatePostRequest; +import com.seoultech.synergybe.domain.post.controller.dto.request.UpdatePostRequest; +import com.seoultech.synergybe.domain.post.controller.dto.response.GetPostResponse; +import com.seoultech.synergybe.domain.post.service.PostService; import com.seoultech.synergybe.system.config.login.LoginUser; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -14,6 +15,9 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -28,91 +32,77 @@ public class PostController { private final PostService postService; - - @Operation(summary = "post 생성", description = "PostResponse가 반환되며 이미지가 함께 저장됩니다.") @PostMapping - public ResponseEntity createPost(@ModelAttribute @Valid CreatePostRequest request, @LoginUser String userId) { - // service layer에 맞게 변경 필요 - CreatePostDto createPostDto = new CreatePostDto( - request.title(), - request.content(), - request.files() == null ? List.of() : request.files() - ); - GetPostResponse response = postService.createPost(userId, createPostDto); - + public ResponseEntity createPost(@ModelAttribute @Valid CreatePostRequest request, @LoginUser String userToken) { + String response = postService.createPost(userToken, request); return ResponseEntity.status(HttpStatus.CREATED).body(response); } @Operation(summary = "post 수정", description = "PostResponse가 반환되며 UpdatePostRequest에 담긴 내용으로 수정됩니다.") @PutMapping - public ResponseEntity updatePost(@ModelAttribute @Valid UpdatePostRequest request, @LoginUser String userId) { - UpdatePostDto updatePostDto = new UpdatePostDto( - request.postId(), - request.title(), - request.content(), - request.files() == null ? List.of() : request.files() - ); - GetPostResponse response = postService.updatePost(userId, updatePostDto); + public ResponseEntity updatePost(@ModelAttribute @Valid UpdatePostRequest request, @LoginUser String userToken) { + String response = postService.updatePost(userToken, request); + return ResponseEntity.status(HttpStatus.OK).body(response); } @Operation(summary = "post 삭제", description = "DeletePostResponse가 반환되며, post의 isDelete = true 로 값이 변경됩니다") @DeleteMapping(value = "/{postId}") - public ResponseEntity deletePost(@PathVariable("postId") String postId, @LoginUser String userId) { - postService.deletePost(userId, postId); + public ResponseEntity deletePost(@PathVariable("postId") String postToken, @LoginUser String userToken) { + postService.deletePost(userToken, postToken); return ResponseEntity.noContent().build(); } @Operation(summary = "단건 Post Get", description = "요청된 1개의 Post가 반환됩니다") @GetMapping(value = "/{postId}") - public ResponseEntity getPost(@PathVariable("postId") String postId) { - GetPostResponse response = postService.getPost(postId); + public ResponseEntity getPost(@PathVariable("postId") String postToken) { + GetPostResponse response = postService.getPost(postToken); return ResponseEntity.status(HttpStatus.OK).body(response); } @Operation(summary = "최근 Post 리스트", description = "ListPostResponse가 반환되며, end값(default로는 long 최대값)으로 마지막 조회된 postId를 전달받으며 이후 10개의 post만 반환합니다") @GetMapping(value = "/recent") - public ResponseEntity> getPosts(@Parameter(description = "offset") @RequestParam(value = "offset", required = false, defaultValue = "9223372036854775807") Long offset) { - ListResponse response = postService.getRecentList(offset); + public ResponseEntity> getPosts(@RequestParam(value = "offsetToken", required = false) String offsetToken) { + ListResponse response = postService.getRecentPostList(offsetToken); return ResponseEntity.status(HttpStatus.OK).body(response); } @Operation(summary = "팔로워의 Post", description = "팔로워의 Post가 반환됩니다") @GetMapping(value = "/feed") - public ResponseEntity> getFeed(@Parameter(description = "마지막 조회 Id") @RequestParam(value = "end", required = false, defaultValue = "9223372036854775807") Long end, @LoginUser String userId) { - ListResponse response = postService.getFeed(end, userId); + public ResponseEntity> getFeed(@RequestParam(value = "offsetToken", required = false) String offsetToken, @LoginUser String userToken) { + ListResponse response = postService.getFeed(offsetToken, userToken); return ResponseEntity.status(HttpStatus.OK).body(response); } -// @Operation(summary = "검색어를 포함하는 Post", description = "검색어를 포함하는 Post가 반환됩니다") -// @GetMapping -// public ResponseEntity> searchAllPosts(@Parameter(description = "검색어") @RequestParam("search") String search, @PageableDefault(size = 15) Pageable pageable) { -// log.info(">> keyword : {}", search); -// -// return ResponseEntity.status(HttpStatus.OK).body(postService.searchAllPosts(search, pageable)); -// } + @Operation(summary = "검색어를 포함하는 Post", description = "검색어를 포함하는 Post가 반환됩니다") + @GetMapping + public ResponseEntity> searchAllPosts(@Parameter(description = "검색어") @RequestParam("search") String search, @PageableDefault(size = 15) Pageable pageable) { + Page response = postService.searchAllPosts(search, pageable); + + return ResponseEntity.status(HttpStatus.OK).body(response); + } @Operation(summary = "사용자가 작성한 Post", description = "사용자의 Post가 반환됩니다") @GetMapping(value = "/user") - public ResponseEntity> getPostsByUser(@Parameter(description = "사용자의 Id") @RequestParam("userId") String userId) { - ListResponse list = postService.getList(userId); + public ResponseEntity> getPostsByUser(@Parameter(description = "사용자의 userToken") @RequestParam("userToken") String userToken) { + ListResponse response = postService.getPostListByUser(userToken); - return ResponseEntity.status(HttpStatus.OK).body(list); + return ResponseEntity.status(HttpStatus.OK).body(response); } + @Operation(summary = "내가 좋아요한 Post", description = "좋아요한 Post가 반환됩니다") + @GetMapping(value = "/me/likes") + public ResponseEntity> getLikedPosts(@LoginUser String userToken) { + ListResponse response = postService.getMyLikedPostList(userToken); -// @Operation(summary = "내가 좋아요한 Post", description = "좋아요한 Post가 반환됩니다") -// @GetMapping(value = "/me/likes") -// public ResponseEntity<> getLikedPosts(@LoginUser String userId) { -// -// return ResponseEntity.status(HttpStatus.OK).body(postService.getLikedPostList(userId)); -// } + return ResponseEntity.status(HttpStatus.OK).body(response); + } @Operation(summary = "이주의 베스트 Post", description = "좋아요 상위 랭킹 순으로 Post가 반환됩니다") @GetMapping(value = "/week") diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/request/CreatePostRequest.java b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/request/CreatePostRequest.java similarity index 74% rename from src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/request/CreatePostRequest.java rename to src/main/java/com/seoultech/synergybe/domain/post/controller/dto/request/CreatePostRequest.java index 24d2df3a..67c3ecb0 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/request/CreatePostRequest.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/request/CreatePostRequest.java @@ -1,15 +1,8 @@ -package com.seoultech.synergybe.domain.post.presentation.dto.request; +package com.seoultech.synergybe.domain.post.controller.dto.request; -import com.seoultech.synergybe.domain.image.Image; -import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.postlike.PostLike; -import com.seoultech.synergybe.domain.user.User; import jakarta.validation.constraints.NotBlank; -import lombok.AllArgsConstructor; -import lombok.Getter; import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; import java.util.List; public record CreatePostRequest( diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/request/UpdatePostRequest.java b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/request/UpdatePostRequest.java similarity index 70% rename from src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/request/UpdatePostRequest.java rename to src/main/java/com/seoultech/synergybe/domain/post/controller/dto/request/UpdatePostRequest.java index feaf0117..ecee5d1b 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/request/UpdatePostRequest.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/request/UpdatePostRequest.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.presentation.dto.request; +package com.seoultech.synergybe.domain.post.controller.dto.request; import jakarta.validation.constraints.NotBlank; import org.springframework.web.multipart.MultipartFile; @@ -10,8 +10,8 @@ public record UpdatePostRequest( String title, @NotBlank(message = "게시글 내용은 필수항목입니다.") String content, - @NotBlank(message = "게시글ID는 필수항목입니다.") - String postId, + @NotBlank(message = "게시글Token은 필수항목입니다.") + String postToken, List files ) { } diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/GetListPostResponse.java b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/response/GetListPostResponse.java similarity index 73% rename from src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/GetListPostResponse.java rename to src/main/java/com/seoultech/synergybe/domain/post/controller/dto/response/GetListPostResponse.java index 48ca9342..6aeb74a7 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/GetListPostResponse.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/response/GetListPostResponse.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.presentation.dto.response; +package com.seoultech.synergybe.domain.post.controller.dto.response; import com.seoultech.synergybe.domain.common.PageInfo; diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/GetPostResponse.java b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/response/GetPostResponse.java similarity index 97% rename from src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/GetPostResponse.java rename to src/main/java/com/seoultech/synergybe/domain/post/controller/dto/response/GetPostResponse.java index 52c5378e..76f752b7 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/GetPostResponse.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/controller/dto/response/GetPostResponse.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.presentation.dto.response; +package com.seoultech.synergybe.domain.post.controller.dto.response; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; @@ -15,7 +15,7 @@ public record GetPostResponse( String postToken, String title, String content, - String userId, + String userToken, String authorName, List commentList, diff --git a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostManager.java b/src/main/java/com/seoultech/synergybe/domain/post/implement/PostManager.java deleted file mode 100644 index aa2110a3..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostManager.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.seoultech.synergybe.domain.post.implement; - -import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.business.dto.UpdatePostDto; -import com.seoultech.synergybe.domain.post.infrastructure.PostJpaRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - -@Transactional -@Component -@RequiredArgsConstructor -public class PostManager { - private final PostJpaRepository postRepository; - - public void save(Post post) { - postRepository.save(post); - } - - public void update(Post post, UpdatePostDto updatePostDto) { - post.updatePost(updatePostDto.title(), updatePostDto.content()); - } - - public void delete(Post post) { - postRepository.delete(post); - } - - public void saveAll(List postList) { - postRepository.saveAll(postList); - } -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostReader.java b/src/main/java/com/seoultech/synergybe/domain/post/implement/PostReader.java deleted file mode 100644 index 7826871b..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostReader.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.seoultech.synergybe.domain.post.implement; - -import com.seoultech.synergybe.domain.common.paging.ListResponse; -import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.infrastructure.PostJpaRepository; -import com.seoultech.synergybe.domain.post.presentation.dto.response.GetPostResponse; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; - -import java.util.List; - -@Component -@RequiredArgsConstructor -public class PostReader { - private final PostJpaRepository postJpaRepository; - - public Post read(String postId) { - return postJpaRepository.findByToken(postId); - } - - public ListResponse readRecentList(Long offset) { - List postList = postJpaRepository.findAllRecentByCount(offset); - System.out.println("postList.size()" + postList.size()); - Long totalCount = postJpaRepository.countSize(); - - boolean hasNext; - int pageSize = 10; - - if (totalCount > pageSize + offset) { - hasNext = true; - } else { - hasNext = false; - } - - List getPostResponses = PostMapperEntityToDto.postListToResponse(postList); - - return new ListResponse<>(getPostResponses); - } - - public ListResponse readFeed(List followingIds) { - List postList = postJpaRepository.findAllByFollowerIds(followingIds); - - return postListToListResponse(postList); - } - - public ListResponse readWeekBest() { - List postList = postJpaRepository.WeekBest(); - - return postListToListResponse(postList); - } - - public ListResponse readListByUser(String userId) { - List postList = postJpaRepository.findAllByUserId(userId); - - return postListToListResponse(postList); - } - - private ListResponse postListToListResponse(List postList) { - List getPostResponses = PostMapperEntityToDto.postListToResponse(postList); - - return new ListResponse<>(getPostResponses); - } -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostJpaRepository.java b/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostJpaRepository.java deleted file mode 100644 index a4446ebf..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostJpaRepository.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.seoultech.synergybe.domain.post.infrastructure; - -import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.exception.PostNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; - -import java.util.List; - -@Component -@RequiredArgsConstructor -public class PostJpaRepository { - private final PostRepository postRepository; - - public void save(Post post) { - postRepository.save(post); - } - - public void saveAll(List postList) { - postRepository.saveAll(postList); - } - - public void delete(Post post) { - postRepository.delete(post); - } - - public Post findByToken(String token) { - return postRepository.findByPostToken(token); - } - - public Post findById(Long postId) { - return postRepository.findById(postId) - .orElseThrow(() -> new PostNotFoundException("존재하지 않는 게시글입니다.")); - } - - public List findAllByEndId(String postId) { - return postRepository.findAllByEndId(postId); - } - - public List findAllRecentByCount(Long offset) { - return postRepository.findAllRecentByCount(offset); - } - - public Long countSize() { - return postRepository.countSize(); - } - - public Long countPostSizeByUserId(String userId) { - return postRepository.totalSizeUser(userId); - } - - public List findAllByFollowerIds(List followingIds) { - return postRepository.findAllByFollowerIds(followingIds); - } - - public List WeekBest() { - return postRepository.WeekBest(); - } - - public List findAllByUserId(String userId) { - return postRepository.findAllByUserId(userId); - } -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepository.java b/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepository.java deleted file mode 100644 index a6dbdd6f..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepository.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.seoultech.synergybe.domain.post.infrastructure; - -import com.seoultech.synergybe.domain.post.Post; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; - -import java.util.List; - -public interface PostRepository extends JpaRepository, PostRepositoryCustom { - - @Query(value = "SELECT * FROM post WHERE post_id < :postId AND is_deleted = 0 ORDER BY post_id DESC LIMIT 10", nativeQuery = true) - List findAllByEndId(@Param("postId") String postId); - - Page findAll(Specification spec, Pageable pageable); - - @Query(value = "SELECT * FROM (SELECT * FROM post WHERE user_id = :userId) p WHERE post_id < :end AND is_deleted = 0 ORDER BY post_id DESC LIMIT 10", nativeQuery = true) - List findAllByIdsAndEndId(@Param("userId") String userId, @Param("end") Long end); - - @Query(value = "SELECT count(*) FROM post WHERE post_id < :end AND is_deleted = 0", nativeQuery = true) - int countPostList(@Param("end") String end); - - @Query(value = "SELECT count(*) FROM (SELECT * FROM post WHERE user_id = :userId AND is_deleted = 0) p WHERE post_id < :end AND is_deleted = 0", nativeQuery = true) - int countFeed(@Param("end") Long end); - - @Query(value = - "SELECT p.post_id, p.create_at, p.update_at, p.author_name, p.content, p.is_deleted, p.title, p.user_id, p.thumbnail_image_id, COUNT(pl.post_like_id) AS like_count " + - "FROM post p " + - "LEFT JOIN post_like pl ON p.post_id = pl.post_id " + - "WHERE p.create_at >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND p.is_deleted = 0 " + - "GROUP BY p.post_id " + - "ORDER BY like_count DESC " + - "LIMIT 5" - , nativeQuery = true) - List findAllByLikeAndDate(); - - @Query(value = "SELECT * FROM post WHERE post_id IN :ids ORDER BY FIELD(post_id, :ids)", nativeQuery = true) - List findAllByIdInOrderByListOrder(@Param("ids") List ids); - - Post findByPostToken(String token); -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepositoryCustom.java b/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepositoryCustom.java deleted file mode 100644 index 2b0c8714..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepositoryCustom.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.seoultech.synergybe.domain.post.infrastructure; - -import com.seoultech.synergybe.domain.post.Post; - -import java.util.List; - -public interface PostRepositoryCustom { -// List findAllByCreateAtAndLimit(Long offset); - - List findAllRecentByCount(Long offset); - - List findAllByUserId(String userId); - -// List findAllByFollowingIdAndEndSeq(String followingId, Long end); - - Long countSize(); - - Long totalSizeUser(String userId); - - List WeekBest(); - - List findAllByFollowerIds(List userIds); -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepositoryImpl.java b/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepositoryImpl.java deleted file mode 100644 index f8e05617..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/infrastructure/PostRepositoryImpl.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.seoultech.synergybe.domain.post.infrastructure; - -import com.querydsl.jpa.impl.JPAQueryFactory; -import com.seoultech.synergybe.domain.post.Post; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Repository; - -import java.time.LocalDateTime; -import java.util.List; - -import static com.seoultech.synergybe.domain.post.QPost.post; -import static com.seoultech.synergybe.domain.postlike.QPostLike.postLike; - -@Repository -@RequiredArgsConstructor -public class PostRepositoryImpl implements PostRepositoryCustom { - private final JPAQueryFactory queryFactory; - - @Override - public Long totalSizeUser(String userId) { - return queryFactory - .select(post.count()) - .from(post) - .where(post.user.userToken.eq(userId)) - .fetchOne(); - } - - @Override - public List findAllRecentByCount(Long offset) { - return queryFactory - .selectFrom(post) - .orderBy(post.createAt.desc()) - .limit(offset) - .fetch(); - } - - @Override - public List findAllByUserId(String userId) { - return queryFactory - .selectFrom(post) - .where(post.user.userToken.eq(userId)) - .fetch(); - } - - @Override - public Long countSize() { - return queryFactory - .select(post.count()) - .from(post) - .fetchOne(); - } - - @Override - public List WeekBest() { - LocalDateTime oneWeekAgo = LocalDateTime.now().minusWeeks(1); - return queryFactory - .select(post -// constructor(GetPostResponse.class, -// post.id, -// post.createAt, -// post.updateAt, -// post.authorName, -// post.content.content, -// post.title.title, -// post.user.id, -// postLike.id.count().as("likes") -// ) - ) - .from(post) - .leftJoin(postLike).on(post.id.eq(postLike.post.id)) - .where(post.createAt.goe(oneWeekAgo)) - .groupBy(post.id) - .orderBy(postLike.id.count().desc()) - .limit(10) - .fetch(); - } - - @Override - public List findAllByFollowerIds(List userIds) { - LocalDateTime oneWeekAgo = LocalDateTime.now().minusWeeks(1); - - return queryFactory - .selectFrom(post) - .where(post.user.userToken.in(userIds) - .and(post.createAt.goe(oneWeekAgo))) - .orderBy(post.createAt.desc()) - .limit(10) - .fetch(); - } -} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/DeletePostResponse.java b/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/DeletePostResponse.java deleted file mode 100644 index 21a501f6..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/DeletePostResponse.java +++ /dev/null @@ -1,17 +0,0 @@ -//package com.seoultech.synergybe.domain.post.dto.response; -// -//import com.seoultech.synergybe.domain.post.Post; -//import lombok.AllArgsConstructor; -//import lombok.Builder; -//import lombok.Getter; -// -//@AllArgsConstructor -//@Getter -//@Builder -//public class DeletePostResponse { -// private Long postId; -// -// public static DeletePostResponse from(Post post) { -// return DeletePostResponse.builder().postId(post.getId()).build(); -// } -//} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/ListPostResponse.java b/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/ListPostResponse.java deleted file mode 100644 index 85708283..00000000 --- a/src/main/java/com/seoultech/synergybe/domain/post/presentation/dto/response/ListPostResponse.java +++ /dev/null @@ -1,32 +0,0 @@ -//package com.seoultech.synergybe.domain.post.dto.response; -// -//import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -//import com.fasterxml.jackson.annotation.JsonProperty; -//import lombok.AllArgsConstructor; -//import lombok.Getter; -//import lombok.NoArgsConstructor; -// -//import java.util.List; -// -//@AllArgsConstructor -//@NoArgsConstructor -//@Getter -//@JsonIgnoreProperties(ignoreUnknown = true) -//public class ListPostResponse { -// private List content; -// -// @JsonProperty("hasNext") -// private boolean hasNext; -// -// public static ListPostResponse from(List getPostRespons, boolean isNext) { -// return new ListPostResponse(getPostRespons, isNext); -// } -// -// public static ListPostResponse from(List getPostRespons) { -// return new ListPostResponse(getPostRespons); -// } -// -// public ListPostResponse(List getPostRespons) { -// this.content = getPostRespons; -// } -//} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepository.java b/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepository.java new file mode 100644 index 00000000..43b1b967 --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepository.java @@ -0,0 +1,21 @@ +package com.seoultech.synergybe.domain.post.repository; + +import com.seoultech.synergybe.domain.post.Post; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface PostRepository extends JpaRepository, PostRepositoryCustom { + + Page findAll(Specification spec, Pageable pageable); + + @Query(value = "SELECT * FROM post WHERE post_id IN :ids ORDER BY FIELD(post_id, :ids)", nativeQuery = true) + List findAllByIdInOrderByListOrder(@Param("ids") List ids); + + Post findByPostToken(String token); +} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepositoryCustom.java b/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepositoryCustom.java new file mode 100644 index 00000000..7b92f588 --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepositoryCustom.java @@ -0,0 +1,25 @@ +package com.seoultech.synergybe.domain.post.repository; + +import com.seoultech.synergybe.domain.post.Post; + +import java.util.List; + +public interface PostRepositoryCustom { + List findAllByCreateAtAndLimit(Long offset); + + List findAllRecentByCount(Long offset); + + List findAllByUserId(Long userId); + + List findMyLikedPostList(Long userId); + + Long countSize(); + + Long totalSizeUser(String userId); + + List findAllByFollowerIds(List userIds); + + List findAllByIdsAndEndId(Long userId, String offsetToken); + + List findAllByLikeAndDate(); +} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepositoryImpl.java b/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepositoryImpl.java new file mode 100644 index 00000000..85c8b9e4 --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/post/repository/PostRepositoryImpl.java @@ -0,0 +1,113 @@ +package com.seoultech.synergybe.domain.post.repository; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import com.seoultech.synergybe.domain.post.Post; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +import java.time.LocalDateTime; +import java.util.List; + +import static com.seoultech.synergybe.domain.post.QPost.post; +import static com.seoultech.synergybe.domain.postlike.QPostLike.postLike; + +@Repository +@RequiredArgsConstructor +public class PostRepositoryImpl implements PostRepositoryCustom { + private final JPAQueryFactory queryFactory; + + @Override + public Long totalSizeUser(String userId) { + return queryFactory + .select(post.count()) + .from(post) + .where(post.user.userToken.eq(userId)) + .fetchOne(); + } + + @Override + public List findAllByCreateAtAndLimit(Long offset) { + return queryFactory + .selectFrom(post) + .where(post.id.lt(offset) + .and(post.isDeleted.isDeleted.eq(false))) + .orderBy(post.createAt.desc()) + .limit(20) + .fetch(); + } + + @Override + public List findAllRecentByCount(Long offset) { + return queryFactory + .selectFrom(post) + .orderBy(post.createAt.desc()) + .limit(offset) + .fetch(); + } + + @Override + public List findAllByUserId(Long userId) { + return queryFactory + .selectFrom(post) + .where(post.user.id.eq(userId)) + .fetch(); + } + + @Override + public List findMyLikedPostList(Long userId) { + return queryFactory + .selectFrom(post) + .leftJoin(postLike).on(post.id.eq(postLike.post.id)) // post와 postLike를 조인 + .where(postLike.user.id.eq(userId) // 해당 사용자가 좋아요한 게시글 + .and(post.isDeleted.isDeleted.eq(false))) // 삭제되지 않은 게시글 + .groupBy(post.id) // 게시글별 그룹화 + .orderBy(post.createAt.desc()) // 최신순으로 정렬 + .limit(5) // 상위 5개의 게시글만 조회 + .fetch(); + } + + @Override + public Long countSize() { + return queryFactory + .select(post.count()) + .from(post) + .fetchOne(); + } + + @Override + public List findAllByFollowerIds(List userIds) { + LocalDateTime oneWeekAgo = LocalDateTime.now().minusWeeks(1); + + return queryFactory + .selectFrom(post) + .where(post.user.id.in(userIds) + .and(post.createAt.goe(oneWeekAgo))) + .orderBy(post.createAt.desc()) + .limit(10) + .fetch(); + } + + @Override + public List findAllByIdsAndEndId(Long userId, String offsetToken) { + return queryFactory + .selectFrom(post) + .where(post.user.id.eq(userId) // userId로 필터링 + .and(post.id.lt(Long.valueOf(offsetToken))) // offsetToken보다 작은 post_id 조회 + .and(post.isDeleted.isDeleted.eq(false))) // is_deleted가 false인 게시물만 조회 + .orderBy(post.id.desc()) // post_id 기준 내림차순 정렬 + .limit(10) // 상위 10개 게시물만 조회 + .fetch(); + } + + @Override + public List findAllByLikeAndDate() { + return queryFactory + .selectFrom(post) + .leftJoin(postLike).on(post.id.eq(postLike.post.id)) // post와 postLike를 조인 + .where(post.isDeleted.isDeleted.eq(false)) // 삭제되지 않은 게시글 + .groupBy(post.id) // 게시글별 그룹화 + .orderBy(postLike.id.count().desc()) // 좋아요 수 기준으로 정렬 + .limit(20) // 상위 20개의 게시글만 조회 + .fetch(); + } +} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostFactory.java b/src/main/java/com/seoultech/synergybe/domain/post/service/PostFactory.java similarity index 95% rename from src/main/java/com/seoultech/synergybe/domain/post/implement/PostFactory.java rename to src/main/java/com/seoultech/synergybe/domain/post/service/PostFactory.java index 33c6ddaa..062f2c9a 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostFactory.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/service/PostFactory.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.implement; +package com.seoultech.synergybe.domain.post.service; import com.seoultech.synergybe.domain.common.generator.IdGenerator; import com.seoultech.synergybe.domain.common.generator.IdPrefix; diff --git a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostMapperEntityToDto.java b/src/main/java/com/seoultech/synergybe/domain/post/service/PostMapperEntityToDto.java similarity index 56% rename from src/main/java/com/seoultech/synergybe/domain/post/implement/PostMapperEntityToDto.java rename to src/main/java/com/seoultech/synergybe/domain/post/service/PostMapperEntityToDto.java index e7c2a62b..c42d66e1 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostMapperEntityToDto.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/service/PostMapperEntityToDto.java @@ -1,8 +1,9 @@ -package com.seoultech.synergybe.domain.post.implement; +package com.seoultech.synergybe.domain.post.service; import com.seoultech.synergybe.domain.comment.dto.response.GetCommentResponse; +import com.seoultech.synergybe.domain.common.paging.ListResponse; import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.presentation.dto.response.GetPostResponse; +import com.seoultech.synergybe.domain.post.controller.dto.response.GetPostResponse; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -12,7 +13,7 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class PostMapperEntityToDto { - public static List postListToResponse(List postList) { + public static ListResponse postListToResponse(List postList, boolean hasNext) { List getPostResponses = postList.stream() .map( result -> new GetPostResponse( @@ -38,9 +39,40 @@ public static List postListToResponse(List postList) { ) ) .toList(); -// PageInfo pageInfo = PageInfo.of(getPostResponses.size(), hasNext); + ListResponse response = new ListResponse<>(getPostResponses, hasNext); - return getPostResponses; + return response; + } + + public static ListResponse postListToResponse(List postList) { + List getPostResponses = postList.stream() + .map( + result -> new GetPostResponse( + result.getPostToken(), + result.getTitle().getTitle(), + result.getContent().getContent(), + result.getUser().getUserToken(), + result.getAuthorName().getAuthorName(), + result.getComments().stream().map( + comment -> new GetCommentResponse( + comment.getCommentToken(), + comment.getUser().getUserToken(), + comment.getPost().getPostToken(), + comment.getComment().getContent(), + comment.getUpdateAt() + ) + ).collect(Collectors.toList()), // 댓글 목록을 포함 + result.getCreateAt(), + result.getUpdateAt(), + "", + List.of(""), + result.getLikes().size() // 좋아요 수 + ) + ) + .toList(); + ListResponse response = new ListResponse<>(getPostResponses); + + return response; } public static GetPostResponse postToResponse(Post post) { diff --git a/src/main/java/com/seoultech/synergybe/domain/post/service/PostService.java b/src/main/java/com/seoultech/synergybe/domain/post/service/PostService.java new file mode 100644 index 00000000..3a5e6c50 --- /dev/null +++ b/src/main/java/com/seoultech/synergybe/domain/post/service/PostService.java @@ -0,0 +1,31 @@ +package com.seoultech.synergybe.domain.post.service; + +import com.seoultech.synergybe.domain.common.paging.ListResponse; +import com.seoultech.synergybe.domain.post.Post; +import com.seoultech.synergybe.domain.post.controller.dto.request.CreatePostRequest; +import com.seoultech.synergybe.domain.post.controller.dto.request.UpdatePostRequest; +import com.seoultech.synergybe.domain.post.controller.dto.response.GetPostResponse; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +public interface PostService { + String createPost(String userToken, CreatePostRequest request); + + String updatePost(String userToken, UpdatePostRequest request); + + void deletePost(String userToken, String postToken); + + GetPostResponse getPost(String postToken); + + ListResponse getRecentPostList(String offsetToken); + + ListResponse getFeed(String offsetToken, String userToken); + + Page searchAllPosts(String keyword, Pageable pageable); + + ListResponse getPostListByUser(String userToken); + + ListResponse getMyLikedPostList(String userToken); + + ListResponse getWeekBestPostList(); +} diff --git a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostValidator.java b/src/main/java/com/seoultech/synergybe/domain/post/service/PostValidator.java similarity index 89% rename from src/main/java/com/seoultech/synergybe/domain/post/implement/PostValidator.java rename to src/main/java/com/seoultech/synergybe/domain/post/service/PostValidator.java index 12e25275..8e3ebdc3 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/implement/PostValidator.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/service/PostValidator.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.implement; +package com.seoultech.synergybe.domain.post.service; import com.seoultech.synergybe.domain.post.Post; import com.seoultech.synergybe.domain.post.exception.PostBadRequestException; diff --git a/src/main/java/com/seoultech/synergybe/domain/post/business/dto/CreatePostDto.java b/src/main/java/com/seoultech/synergybe/domain/post/service/dto/CreatePostDto.java similarity index 77% rename from src/main/java/com/seoultech/synergybe/domain/post/business/dto/CreatePostDto.java rename to src/main/java/com/seoultech/synergybe/domain/post/service/dto/CreatePostDto.java index ec58f98c..e88ce446 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/business/dto/CreatePostDto.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/service/dto/CreatePostDto.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.business.dto; +package com.seoultech.synergybe.domain.post.service.dto; import org.springframework.web.multipart.MultipartFile; diff --git a/src/main/java/com/seoultech/synergybe/domain/post/business/dto/UpdatePostDto.java b/src/main/java/com/seoultech/synergybe/domain/post/service/dto/UpdatePostDto.java similarity index 79% rename from src/main/java/com/seoultech/synergybe/domain/post/business/dto/UpdatePostDto.java rename to src/main/java/com/seoultech/synergybe/domain/post/service/dto/UpdatePostDto.java index f6dd35d9..e952cdbb 100644 --- a/src/main/java/com/seoultech/synergybe/domain/post/business/dto/UpdatePostDto.java +++ b/src/main/java/com/seoultech/synergybe/domain/post/service/dto/UpdatePostDto.java @@ -1,4 +1,4 @@ -package com.seoultech.synergybe.domain.post.business.dto; +package com.seoultech.synergybe.domain.post.service.dto; import org.springframework.web.multipart.MultipartFile; diff --git a/src/main/java/com/seoultech/synergybe/domain/postlike/service/PostLikeService.java b/src/main/java/com/seoultech/synergybe/domain/postlike/service/PostLikeService.java index 92f74b6d..6a68f5df 100644 --- a/src/main/java/com/seoultech/synergybe/domain/postlike/service/PostLikeService.java +++ b/src/main/java/com/seoultech/synergybe/domain/postlike/service/PostLikeService.java @@ -5,7 +5,7 @@ import com.seoultech.synergybe.domain.common.generator.IdPrefix; import com.seoultech.synergybe.domain.common.generator.TokenGenerator; import com.seoultech.synergybe.domain.post.Post; -import com.seoultech.synergybe.domain.post.infrastructure.PostJpaRepository; +import com.seoultech.synergybe.domain.post.repository.PostRepository; import com.seoultech.synergybe.domain.postlike.PostLike; import com.seoultech.synergybe.domain.postlike.PostLikeType; import com.seoultech.synergybe.domain.postlike.dto.response.GetPostLikeResponse; @@ -27,14 +27,11 @@ public class PostLikeService { private final PostLikeRepository postLikeRepository; private final IdGenerator idGenerator; private final TokenGenerator tokenGenerator; - private final PostJpaRepository postJpaRepository; + private final PostRepository postRepository; @Transactional public GetPostLikeResponse updatePostLike(User user, String postToken, PostLikeType type) { LikeStatus status; - log.info("before find"); - Post post = postJpaRepository.findByToken(postToken); - log.info("after find"); if (type.getLikeType().equals("like")) { status = LikeStatus.LIKE; @@ -42,9 +39,7 @@ public GetPostLikeResponse updatePostLike(User user, String postToken, PostLikeT status = LikeStatus.UN_LIKE; } try { - log.info("updatePostLike update before"); - PostLike updatedPostLike = this.update(user, postToken, status); - log.info("updatePostLike update after"); + update(user, postToken, status); return GetPostLikeResponse.builder().build(); } catch (Exception e) { throw new PostLikeNotFoundException("존재하지 않는 좋아요입니다."); @@ -68,7 +63,7 @@ public synchronized PostLike update(User user, String postToken, LikeStatus like Optional postLikeOptional = postLikeRepository.findByUserIdAndPostId(user.getUserToken(), postToken); log.info("option"); - Post post = postJpaRepository.findByToken(postToken); + Post post = postRepository.findByPostToken(postToken); log.info("post"); if (postLikeOptional.isPresent()) { diff --git a/src/main/resources/post-setup.sql b/src/main/resources/post-setup.sql new file mode 100644 index 00000000..9c0b1c85 --- /dev/null +++ b/src/main/resources/post-setup.sql @@ -0,0 +1,26 @@ +// /src/test/resources/payment-setup.sql +INSERT INTO post (is_deleted, create_at, post_id, update_at, user_id, author_name, content, post_token, thumbnail_image_id, title) +VALUES + (0, NOW(6), 12345, NOW(6), 67890, 'Author 1', 'Content for post 1', 'token1', 'thumbnail1', 'Post Title 1'), + (0, NOW(6), 12346, NOW(6), 67891, 'Author 2', 'Content for post 2', 'token2', 'thumbnail2', 'Post Title 2'), + (0, NOW(6), 12347, NOW(6), 67892, 'Author 3', 'Content for post 3', 'token3', 'thumbnail3', 'Post Title 3'), + (0, NOW(6), 12348, NOW(6), 67893, 'Author 4', 'Content for post 4', 'token4', 'thumbnail4', 'Post Title 4'), + (0, NOW(6), 12349, NOW(6), 67894, 'Author 5', 'Content for post 5', 'token5', 'thumbnail5', 'Post Title 5'), + (0, NOW(6), 12350, NOW(6), 67895, 'Author 6', 'Content for post 6', 'token6', 'thumbnail6', 'Post Title 6'), + (0, NOW(6), 12351, NOW(6), 67896, 'Author 7', 'Content for post 7', 'token7', 'thumbnail7', 'Post Title 7'), + (0, NOW(6), 12352, NOW(6), 67897, 'Author 8', 'Content for post 8', 'token8', 'thumbnail8', 'Post Title 8'), + (0, NOW(6), 12353, NOW(6), 67898, 'Author 9', 'Content for post 9', 'token9', 'thumbnail9', 'Post Title 9'), + (0, NOW(6), 12354, NOW(6), 67899, 'Author 10', 'Content for post 10', 'token10', 'thumbnail10', 'Post Title 10'), + (0, NOW(6), 12355, NOW(6), 67900, 'Author 11', 'Content for post 11', 'token11', 'thumbnail11', 'Post Title 11'), + (0, NOW(6), 12356, NOW(6), 67901, 'Author 12', 'Content for post 12', 'token12', 'thumbnail12', 'Post Title 12'), + (0, NOW(6), 12357, NOW(6), 67902, 'Author 13', 'Content for post 13', 'token13', 'thumbnail13', 'Post Title 13'), + (0, NOW(6), 12358, NOW(6), 67903, 'Author 14', 'Content for post 14', 'token14', 'thumbnail14', 'Post Title 14'), + (0, NOW(6), 12359, NOW(6), 67904, 'Author 15', 'Content for post 15', 'token15', 'thumbnail15', 'Post Title 15'), + (0, NOW(6), 12360, NOW(6), 67905, 'Author 16', 'Content for post 16', 'token16', 'thumbnail16', 'Post Title 16'), + (0, NOW(6), 12361, NOW(6), 67906, 'Author 17', 'Content for post 17', 'token17', 'thumbnail17', 'Post Title 17'), + (0, NOW(6), 12362, NOW(6), 67907, 'Author 18', 'Content for post 18', 'token18', 'thumbnail18', 'Post Title 18'), + (0, NOW(6), 12363, NOW(6), 67908, 'Author 19', 'Content for post 19', 'token19', 'thumbnail19', 'Post Title 19'), + (0, NOW(6), 12364, NOW(6), 67909, 'Author 20', 'Content for post 20', 'token20', 'thumbnail20', 'Post Title 20'); + + + diff --git a/src/test/java/com/seoultech/synergybe/domain/user/JwtAuthenticationFilterTest.java b/src/test/java/com/seoultech/synergybe/domain/user/JwtAuthenticationFilterTest.java index 9fb1a79c..5c29c0f0 100644 --- a/src/test/java/com/seoultech/synergybe/domain/user/JwtAuthenticationFilterTest.java +++ b/src/test/java/com/seoultech/synergybe/domain/user/JwtAuthenticationFilterTest.java @@ -12,38 +12,10 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.security.test.context.support.WithMockUser; -import static org.hamcrest.Matchers.not; - import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; - class JwtAuthenticationFilterTest extends ControllerTestSupport { -// @DisplayName("email, password로 로그인 한다") -// @WithMockUser(username = "as@gmail.com", password = "whdgns0000", roles = "USER") -// @Test -// void login() throws Exception { -// // given -// String email = "as@gmail.com"; -// String password = "whdgns0000"; -// LoginRequest loginRequest = new LoginRequest(email, password); -// -// // login시 -//// Mockito.when(userService.) -// -// mockMvc.perform( -// post("/api/v1/users/login") -// .contentType(MediaType.APPLICATION_JSON) -// .content(objectMapper.writeValueAsString(loginRequest)) -// .with(csrf()) -// ) -// .andDo(print()) -// .andExpect(status().isOk()) -// ; -// } - @DisplayName("email, password로 로그인시 인증필터를 거친다.") @WithMockUser(username = "as@gmail.com", password = "whdgns0000", roles = "USER") @Test @@ -65,30 +37,4 @@ void testJwtAuthenticationFilter() throws Exception { verify(filterChain, times(1)).doFilter(request, response); } - -// @DisplayName("RefreshToken으로 accessToken을 재발행한다") -// void generateAccessTokenByRefreshToken() throws Exception { -// @Test -// // given -// String email = "jonghuncu@gmail.com"; -// String password = "password"; -// String name = "name"; -// String major = "major"; -// String userToken = userService.createUser(new CreateUserRequest(email, password, name, major, "123456")); -// User user = userRepository.findByUserToken(userToken).orElseThrow(); -// -// UserRefreshToken refreshToken = new UserRefreshToken(1L); -// userRefreshTokenFactory.save(refreshToken); -// -// // when -// mockMvc.perform( -// post("/api/v1/users/refresh-token") -// .cookie(new Cookie("refreshToken", refreshToken.getRefreshToken().getRefreshToken())) -// .contentType(MediaType.APPLICATION_JSON)) -// .andDo(print()) -// .andExpect(status().isNoContent()) -// .andExpect(cookie().exists("refreshToken")) -// .andExpect(cookie().value("refreshToken", not(equalTo(refreshToken)))) -// ; -// } }