Skip to content

Commit

Permalink
제목 검색 -> 내용 검색으로 변경 & Pagination 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
3un0ia committed Mar 16, 2024
1 parent 58e239a commit 32d24b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
17 changes: 15 additions & 2 deletions JWT/src/main/java/JWTLogIn/JWT/post/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import JWTLogIn.JWT.post.service.PostService;
import JWTLogIn.JWT.user.entity.UserEntity;
import JWTLogIn.JWT.user.repository.UserRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
Expand Down Expand Up @@ -119,6 +123,15 @@ public ResponseEntity<Void> delete(@PathVariable("id") Long id)
}




@CrossOrigin
@GetMapping("/notice")
@Transactional
public ResponseEntity<Page> getPostsInPage(@RequestParam int page,
@RequestParam(required = false, defaultValue = "15") int size) {
System.out.println("-- Get Posts in Page --");
// 첫 번째 페이지 page = 0이므로, page-1로 전달 -> 1부터 요청할 수 있도록
Page<PostDto> postsInPage = postService.findPostsInPage(page-1, size);

return ResponseEntity.ok(postsInPage);
}
}
1 change: 1 addition & 0 deletions JWT/src/main/java/JWTLogIn/JWT/post/entity/PostEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import JWTLogIn.JWT.post.dto.PostDto;
import JWTLogIn.JWT.user.entity.UserEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.UpdateTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import JWTLogIn.JWT.post.dto.PostDto;
import JWTLogIn.JWT.post.entity.PostEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Pageable;
import java.util.List;
import java.util.Optional;

Expand All @@ -16,6 +17,7 @@ public interface PostRepository extends JpaRepository<PostEntity, Long> {
@Override
<S extends PostEntity> S save(S entity);

List<PostEntity> findByTitleContains(String search);

// List<PostEntity> findByTitleContains(String search);
List<PostEntity> findByDescriptionContains(String search);
Page<PostEntity> findAllByOrderByIdDesc(Pageable pageable);
}
25 changes: 24 additions & 1 deletion JWT/src/main/java/JWTLogIn/JWT/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import JWTLogIn.JWT.user.entity.UserEntity;
import JWTLogIn.JWT.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -17,6 +19,7 @@
import static JWTLogIn.JWT.post.entity.PostEntity.toDto;

@Service
@Transactional
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
Expand Down Expand Up @@ -60,7 +63,7 @@ public List<PostDto> searchPosts(String search)
{
List<PostDto> postDtos = new ArrayList<>();

List<PostEntity> SearchedEntity = postRepository.findByTitleContains(search);
List<PostEntity> SearchedEntity = postRepository.findByDescriptionContains(search);
for (PostEntity postEntity : SearchedEntity) {
System.out.println("postEntity = " + postEntity);
postDtos.add(toDto(postEntity));
Expand Down Expand Up @@ -115,4 +118,24 @@ public void deletePost(Long postId) // 게시글 삭제하기
{
postRepository.deleteById(postId);
}

public Page<PostDto> findPostsInPage(int page, int size) {
// Post DB에서 Page 단위로 가져오기
PageRequest pageRequest = PageRequest.of(page, size);
Page<PostEntity> postPage = postRepository.findAllByOrderByIdDesc(pageRequest);

// Post 반환 + DTO로 변환
List<PostEntity> posts = postPage.getContent();
List<PostDto> postDtos = new ArrayList<>();
for (PostEntity postEntity : posts) {
PostDto dto = toDto(postEntity);
postDtos.add(dto);
postDtos.toString();
}

Page<PostDto> postDtoPage = postPage.map(PostEntity::toDto);

return postDtoPage;
}

}

0 comments on commit 32d24b4

Please sign in to comment.