From f5c6c1ba00d37721f2c32aadccb484d0897a25e9 Mon Sep 17 00:00:00 2001 From: leeuihyun Date: Mon, 2 Jun 2025 20:07:36 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20=EA=B8=B0=EA=B0=84=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80=20(?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EB=AC=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 9 +++++++-- .../post/repository/PostRepository.java | 5 +++++ .../domain/post/service/PostService.java | 4 +++- .../domain/post/service/PostServiceImpl.java | 19 +++++++++++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/feeda/domain/post/controller/PostController.java b/src/main/java/com/example/feeda/domain/post/controller/PostController.java index 565dec6..d3b858c 100644 --- a/src/main/java/com/example/feeda/domain/post/controller/PostController.java +++ b/src/main/java/com/example/feeda/domain/post/controller/PostController.java @@ -6,11 +6,13 @@ import com.example.feeda.security.jwt.JwtPayload; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; +import java.time.LocalDateTime; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -51,12 +53,15 @@ public ResponseEntity findPostById(@PathVariable @NotNull Long public ResponseEntity> findAllPost( @RequestParam(defaultValue = "1") @Min(1) int page, @RequestParam(defaultValue = "10") @Min(1) int size, - @RequestParam(defaultValue = "") String keyword + @RequestParam(defaultValue = "") String keyword, + @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime startUpdatedAt, + @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime endUpdatedAt ) { Pageable pageable = PageRequest.of(page - 1, size, Sort.Direction.DESC, "updatedAt"); - return new ResponseEntity<>(postService.findAll(pageable, keyword), HttpStatus.OK); + return new ResponseEntity<>( + postService.findAll(pageable, keyword, startUpdatedAt, endUpdatedAt), HttpStatus.OK); } @GetMapping("/followings") diff --git a/src/main/java/com/example/feeda/domain/post/repository/PostRepository.java b/src/main/java/com/example/feeda/domain/post/repository/PostRepository.java index 4d0ec21..ff944dc 100644 --- a/src/main/java/com/example/feeda/domain/post/repository/PostRepository.java +++ b/src/main/java/com/example/feeda/domain/post/repository/PostRepository.java @@ -1,6 +1,7 @@ package com.example.feeda.domain.post.repository; import com.example.feeda.domain.post.entity.Post; +import java.time.LocalDateTime; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -11,4 +12,8 @@ public interface PostRepository extends JpaRepository { Page findAllByTitleContaining(String title, Pageable pageable); Page findAllByProfile_IdIn(List followingProfileIds, Pageable pageable); + + Page findAllByTitleContainingAndUpdatedAtBetween(String title, + LocalDateTime startUpdatedAt, LocalDateTime endUpdatedAt, + Pageable pageable); } diff --git a/src/main/java/com/example/feeda/domain/post/service/PostService.java b/src/main/java/com/example/feeda/domain/post/service/PostService.java index 40c1f9e..81bfd97 100644 --- a/src/main/java/com/example/feeda/domain/post/service/PostService.java +++ b/src/main/java/com/example/feeda/domain/post/service/PostService.java @@ -3,6 +3,7 @@ import com.example.feeda.domain.post.dto.PostRequestDto; import com.example.feeda.domain.post.dto.PostResponseDto; import com.example.feeda.security.jwt.JwtPayload; +import java.time.LocalDateTime; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -14,7 +15,8 @@ PostResponseDto createPost(PostRequestDto postRequestDto, PostResponseDto findPostById(Long id); - Page findAll(Pageable pageable, String keyword); + Page findAll(Pageable pageable, String keyword, LocalDateTime startUpdatedAt, + LocalDateTime endUpdatedAt); Page findFollowingAllPost(Pageable pageable, JwtPayload jwtPayload); diff --git a/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java b/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java index 7fa17a7..f9e511d 100644 --- a/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java +++ b/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java @@ -9,6 +9,7 @@ import com.example.feeda.domain.profile.entity.Profile; import com.example.feeda.domain.profile.repository.ProfileRepository; import com.example.feeda.security.jwt.JwtPayload; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; @@ -38,7 +39,7 @@ public PostResponseDto createPost(PostRequestDto postRequestDto, JwtPayload jwtP Post post = new Post(postRequestDto.getTitle(), postRequestDto.getContent(), postRequestDto.getCategory(), profile); - + Post savedPost = postRepository.save(post); return new PostResponseDto(savedPost.getId(), @@ -64,7 +65,21 @@ public PostResponseDto findPostById(Long id) { @Override @Transactional(readOnly = true) - public Page findAll(Pageable pageable, String keyword) { + public Page findAll(Pageable pageable, String keyword, + LocalDateTime startUpdatedAt, LocalDateTime endUpdatedAt) { + + if ((startUpdatedAt == null && endUpdatedAt != null) || (startUpdatedAt != null + && endUpdatedAt == null)) { + throw new ResponseStatusException( + HttpStatus.BAD_REQUEST, + "startUpdatedAt과 endUpdatedAt은 둘 다 있어야 하거나 둘 다 없어야 합니다."); + } + + if (startUpdatedAt != null) { + return postRepository.findAllByTitleContainingAndUpdatedAtBetween( + keyword, startUpdatedAt, endUpdatedAt, pageable) + .map(PostResponseDto::toDto); + } return postRepository.findAllByTitleContaining(keyword, pageable) .map(PostResponseDto::toDto); From 43811c39197c6347e2794eae76b0cc8716a3a219 Mon Sep 17 00:00:00 2001 From: leeuihyun Date: Mon, 2 Jun 2025 21:06:09 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor=20:=20=EA=B8=B0=EA=B0=84=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?LocalDateTime=EA=B3=BC=20String=20=ED=83=80=EC=9E=85=EC=9D=98?= =?UTF-8?q?=20=EC=9D=B4=EC=8A=88=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20LocalDate=20=ED=83=80=EC=9E=85=EC=9D=84=20=EC=9D=B4?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=97=AC=20=EA=B8=B0=EA=B0=84=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/post/controller/PostController.java | 14 +++++++------- .../feeda/domain/post/service/PostService.java | 6 +++--- .../feeda/domain/post/service/PostServiceImpl.java | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/feeda/domain/post/controller/PostController.java b/src/main/java/com/example/feeda/domain/post/controller/PostController.java index daff324..3a2943e 100644 --- a/src/main/java/com/example/feeda/domain/post/controller/PostController.java +++ b/src/main/java/com/example/feeda/domain/post/controller/PostController.java @@ -7,7 +7,7 @@ import com.example.feeda.security.jwt.JwtPayload; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; -import java.time.LocalDateTime; +import java.time.LocalDate; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -48,8 +48,8 @@ public ResponseEntity createPost(@RequestBody PostRequestDto re @PostMapping("/{id}/likes") public ResponseEntity makeLikes( - @PathVariable Long id, - @AuthenticationPrincipal JwtPayload jwtPayload) { + @PathVariable Long id, + @AuthenticationPrincipal JwtPayload jwtPayload) { Long profileId = jwtPayload.getProfileId(); return new ResponseEntity<>(postService.makeLikes(id, jwtPayload), HttpStatus.OK); @@ -57,8 +57,8 @@ public ResponseEntity makeLikes( @DeleteMapping("/{id}/likes") public ResponseEntity deleteLikes( - @PathVariable Long id, - @AuthenticationPrincipal JwtPayload jwtPayload + @PathVariable Long id, + @AuthenticationPrincipal JwtPayload jwtPayload ) { Long profileId = jwtPayload.getProfileId(); postService.deleteLikes(id, profileId); @@ -76,8 +76,8 @@ public ResponseEntity> findAllPost( @RequestParam(defaultValue = "1") @Min(1) int page, @RequestParam(defaultValue = "10") @Min(1) int size, @RequestParam(defaultValue = "") String keyword, - @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime startUpdatedAt, - @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime endUpdatedAt + @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startUpdatedAt, + @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endUpdatedAt ) { Pageable pageable = PageRequest.of(page - 1, size, Sort.Direction.DESC, "updatedAt"); diff --git a/src/main/java/com/example/feeda/domain/post/service/PostService.java b/src/main/java/com/example/feeda/domain/post/service/PostService.java index 30d8768..463d697 100644 --- a/src/main/java/com/example/feeda/domain/post/service/PostService.java +++ b/src/main/java/com/example/feeda/domain/post/service/PostService.java @@ -4,7 +4,7 @@ import com.example.feeda.domain.post.dto.PostRequestDto; import com.example.feeda.domain.post.dto.PostResponseDto; import com.example.feeda.security.jwt.JwtPayload; -import java.time.LocalDateTime; +import java.time.LocalDate; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -15,8 +15,8 @@ PostResponseDto createPost(PostRequestDto postRequestDto, PostResponseDto findPostById(Long id); - Page findAll(Pageable pageable, String keyword, LocalDateTime startUpdatedAt, - LocalDateTime endUpdatedAt); + Page findAll(Pageable pageable, String keyword, LocalDate startUpdatedAt, + LocalDate endUpdatedAt); Page findFollowingAllPost(Pageable pageable, JwtPayload jwtPayload); diff --git a/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java b/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java index 5d39f77..83fab0b 100644 --- a/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java +++ b/src/main/java/com/example/feeda/domain/post/service/PostServiceImpl.java @@ -13,7 +13,7 @@ import com.example.feeda.domain.profile.entity.Profile; import com.example.feeda.domain.profile.repository.ProfileRepository; import com.example.feeda.security.jwt.JwtPayload; -import java.time.LocalDateTime; +import java.time.LocalDate; import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; @@ -107,7 +107,7 @@ public PostResponseDto findPostById(Long id) { @Override @Transactional(readOnly = true) public Page findAll(Pageable pageable, String keyword, - LocalDateTime startUpdatedAt, LocalDateTime endUpdatedAt) { + LocalDate startUpdatedAt, LocalDate endUpdatedAt) { if ((startUpdatedAt == null && endUpdatedAt != null) || (startUpdatedAt != null && endUpdatedAt == null)) { @@ -118,7 +118,7 @@ public Page findAll(Pageable pageable, String keyword, if (startUpdatedAt != null) { return postRepository.findAllByTitleContainingAndUpdatedAtBetween( - keyword, startUpdatedAt, endUpdatedAt, pageable) + keyword, startUpdatedAt.atStartOfDay(), endUpdatedAt.atTime(23, 59, 59), pageable) .map(post -> PostResponseDto.toDto(post, postLikeRepository.countByPost(post), commentRepository.countByPost(post))); }