From 1846033cd168f68fecd638045f19b69bf8c3ca48 Mon Sep 17 00:00:00 2001 From: HyeonSeong Date: Wed, 12 Feb 2025 19:21:38 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat=20:=20category=20=EB=B3=84=EB=A1=9C=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EA=B2=80=EC=83=89=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiV1ArticleController.java | 13 ++++++++++-- .../article/dto/request/ArticleRequest.java | 4 +++- .../article/dto/response/ArticleResponse.java | 7 +++++-- .../domain/article/entity/Article.java | 10 ++++++++-- .../domain/article/entity/Category.java | 15 ++++++++++++++ .../article/repository/ArticleRepository.java | 5 ----- .../repository/ArticleRepositoryCustom.java | 4 ++++ .../impl/ArticleRepositoryCustomImpl.java | 16 +++++++++++++++ .../article/service/ArticleService.java | 8 ++++++-- .../service/impl/ArticleServiceImpl.java | 20 +++++++++++++++---- .../global/config/WebConfig.java | 7 +++++++ 11 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Category.java diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java index 2b8d301..f30c035 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java @@ -10,6 +10,7 @@ import org.springframework.web.multipart.MultipartFile; import project.buysellservice.domain.article.dto.request.ArticleRequest; import project.buysellservice.domain.article.dto.response.ArticleResponse; +import project.buysellservice.domain.article.entity.Category; import project.buysellservice.domain.article.service.ArticleService; import java.util.List; @@ -39,7 +40,7 @@ public ResponseEntity getArticleById(@PathVariable("id") Long i public ResponseEntity createArticle(@RequestPart("articleRequest") @Valid ArticleRequest articleRequest, @RequestPart("file") List files) throws Exception { return ResponseEntity.ok(articleService.createArticle( - articleRequest.title(), articleRequest.content(), files, articleRequest.price() + articleRequest.title(), articleRequest.content(), files, articleRequest.price(), articleRequest.category() )); } @@ -49,7 +50,7 @@ public ResponseEntity updateArticle(@Valid @PathVariable("id") @RequestPart("articleRequest") ArticleRequest articleRequest, @RequestPart("file") List files) throws Exception { return ResponseEntity.ok(articleService.updateArticle( - id, articleRequest.title(), articleRequest.content(), files, articleRequest.price() + id, articleRequest.title(), articleRequest.content(), files, articleRequest.price(), articleRequest.category() )); } @@ -65,4 +66,12 @@ public ResponseEntity soldArticle(@Valid @PathVariable("id") Lo return ResponseEntity.ok(articleService.soldArticle(id)); } + // 해당 카테고리 글만 가져오기 + @GetMapping("/category") + public ResponseEntity> getArticleByCategory(@Valid @RequestParam("category") Category category, + @Valid @RequestParam("page") int page, + @Valid @RequestParam("size") int size) { + return ResponseEntity.ok(articleService.readCategoryArticle(category, PageRequest.of(page, size))); + } + } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/request/ArticleRequest.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/request/ArticleRequest.java index 840a8e8..60eacf2 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/request/ArticleRequest.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/request/ArticleRequest.java @@ -1,11 +1,13 @@ package project.buysellservice.domain.article.dto.request; import org.springframework.web.multipart.MultipartFile; +import project.buysellservice.domain.article.entity.Category; public record ArticleRequest( String title, String content, - Long price + Long price, + Category category ) { } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/response/ArticleResponse.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/response/ArticleResponse.java index 67b85d3..e6a2413 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/response/ArticleResponse.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/dto/response/ArticleResponse.java @@ -1,6 +1,7 @@ package project.buysellservice.domain.article.dto.response; import project.buysellservice.domain.article.entity.Article; +import project.buysellservice.domain.article.entity.Category; import project.buysellservice.domain.article.entity.State; import java.util.List; @@ -11,7 +12,8 @@ public record ArticleResponse( String content, Long price, List files, - State state + State state, + Category category ) { public static ArticleResponse of(Article article) { @@ -20,7 +22,8 @@ public static ArticleResponse of(Article article) { article.getContent(), article.getPrice(), article.getFiles(), - article.getState() + article.getState(), + article.getCategory() ); } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java index 64fa7b7..f5519c3 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java @@ -40,20 +40,25 @@ public class Article extends BaseEntity { @Enumerated(EnumType.STRING) private State state; + @Column(name = "category") + @Enumerated(EnumType.STRING) + private Category category; + // 게시글 생성 빌더 - public static Article createFrom(String title, String content, List files, Long price, String bucketName) { + public static Article createFrom(String title, String content, List files, Long price, String bucketName, Category category) { return Article.builder() .title(title) .content(content) .files(files) .price(price) .bucketName(bucketName) + .category(category) .state(State.SELL) .build(); } // 게시글 수정 빌더 - public static Article updateFrom(Long id, String title, String content, List files, Long price, String bucketName) { + public static Article updateFrom(Long id, String title, String content, List files, Long price, String bucketName, Category category) { return Article.builder() .id(id) .title(title) @@ -61,6 +66,7 @@ public static Article updateFrom(Long id, String title, String content, List findAllByOrderByCreatedAt(Pageable pageable); + + // 해당 카테고리 글 가져오기 + Page
findAllByCategory(Category category, Pageable pageable); } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java index 49c4202..bab245a 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java @@ -6,6 +6,7 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import project.buysellservice.domain.article.entity.Article; +import project.buysellservice.domain.article.entity.Category; import project.buysellservice.domain.article.entity.QArticle; import project.buysellservice.domain.article.repository.ArticleRepositoryCustom; @@ -28,4 +29,19 @@ public Page
findAllByOrderByCreatedAt(Pageable pageable) { return new PageImpl<>(articles); } + + @Override + public Page
findAllByCategory(Category category, Pageable pageable) { + QArticle article = QArticle.article; + + List
articles = jpaQueryFactory + .selectFrom(article) + .where(article.category.eq(category)) + .orderBy(article.createDate.desc()) + .offset(pageable.getOffset()) // 페이지 시작 위치 + .limit(pageable.getPageSize()) // 한 페이지에 표시할 개수 + .fetch(); + + return new PageImpl<>(articles); + } } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java index b404828..5260091 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java @@ -3,18 +3,19 @@ import org.springframework.data.domain.Pageable; import org.springframework.web.multipart.MultipartFile; import project.buysellservice.domain.article.dto.response.ArticleResponse; +import project.buysellservice.domain.article.entity.Category; import java.util.List; public interface ArticleService { // 게시글 생성 - ArticleResponse createArticle(String name, String content, List files, Long Price) throws Exception; + ArticleResponse createArticle(String name, String content, List files, Long Price, Category category) throws Exception; // 게시글 읽기 ArticleResponse readArticle(Long id); // 게시글 수정 - ArticleResponse updateArticle(Long id, String name, String content, List files, Long price) throws Exception; + ArticleResponse updateArticle(Long id, String name, String content, List files, Long price, Category category) throws Exception; // 게시글 삭제 void deleteArticle(Long id) throws Exception; @@ -25,4 +26,7 @@ public interface ArticleService { // 게시글 팔림 처리 ArticleResponse soldArticle(Long id); + // 카테고리 게시글 가져오기 + List readCategoryArticle(Category category, Pageable pageable); + } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java index ff215af..050eab0 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java @@ -9,6 +9,7 @@ import project.buysellservice.domain.article.dto.response.ArticleResponse; import project.buysellservice.domain.article.dto.response.FileResponse; import project.buysellservice.domain.article.entity.Article; +import project.buysellservice.domain.article.entity.Category; import project.buysellservice.domain.article.repository.ArticleRepository; import project.buysellservice.domain.article.service.ArticleService; @@ -31,6 +32,7 @@ public List readAllArticles(Pageable pageable) { return ArticleResponse.listOf(article); } + // 게시글 팔림 처리 @Override public ArticleResponse soldArticle(Long id) { Article article = articleRepository.getByIdOrThrow(id); @@ -50,13 +52,23 @@ public ArticleResponse readArticle(Long id) { return ArticleResponse.of(article); } + // 카테고리 게시글 가져오기 + @Override + public List readCategoryArticle(Category category, Pageable pageable) { + + Page
articles = articleRepository.findAllByCategory(category, pageable); + + List
article = articles.getContent(); + + return ArticleResponse.listOf(article); + } // 생성 @Override - public ArticleResponse createArticle(String name, String content, List files, Long price) throws Exception { + public ArticleResponse createArticle(String name, String content, List files, Long price, Category category) throws Exception { FileResponse fileData = fileService.createFile(files); - Article article = Article.createFrom(name, content, fileData.files(), price, fileData.bucketName()); + Article article = Article.createFrom(name, content, fileData.files(), price, fileData.bucketName(), category); articleRepository.save(article); @@ -65,14 +77,14 @@ public ArticleResponse createArticle(String name, String content, List files, Long price) throws Exception { + public ArticleResponse updateArticle(Long id, String name, String content, List files, Long price, Category category) throws Exception { fileService.deleteFile(id); FileResponse fileData = fileService.updateFile(files, id); Article article = articleRepository.getByIdOrThrow(id); - Article newArticle = Article.updateFrom(article.getId(), name, content, fileData.files(), price, fileData.bucketName()); + Article newArticle = Article.updateFrom(article.getId(), name, content, fileData.files(), price, fileData.bucketName(), category); articleRepository.save(newArticle); diff --git a/buy-sell-service/src/main/java/project/buysellservice/global/config/WebConfig.java b/buy-sell-service/src/main/java/project/buysellservice/global/config/WebConfig.java index 032d542..06e3b99 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/global/config/WebConfig.java +++ b/buy-sell-service/src/main/java/project/buysellservice/global/config/WebConfig.java @@ -3,6 +3,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; @@ -20,4 +21,10 @@ public WebConfig(OctetStreamReadMsgConverter octetStreamReadMsgConverter) { public void configureMessageConverters(List> converters) { converters.add(octetStreamReadMsgConverter); } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/**") + .addResourceLocations("classpath:/static/"); + } } From 8bda31c933e4c3edd51ba5e22a5fa02607bcdfc9 Mon Sep 17 00:00:00 2001 From: HyeonSeong Date: Thu, 20 Feb 2025 14:58:57 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor=20:=20bucketName=20-=20=ED=95=98?= =?UTF-8?q?=EB=82=98=EC=9D=98=20=EC=84=9C=EB=B9=84=EC=8A=A4=EC=97=90=20buc?= =?UTF-8?q?ketName=20=EC=9D=84=20=EC=83=9D=EC=84=B1=20-=20fileName=20=3D?= =?UTF-8?q?=20uuid=20v1=20=EC=9C=BC=EB=A1=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/File/service/FileService.java | 3 +-- .../File/service/impl/FileServiceImpl.java | 20 +++++++++++-------- .../domain/article/entity/Article.java | 6 ++++-- .../service/impl/ArticleServiceImpl.java | 3 +-- .../global/config/MinioConfig.java | 3 --- .../src/main/resources/application.yml | 3 +-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/FileService.java b/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/FileService.java index ecc1e3a..d44db4b 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/FileService.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/FileService.java @@ -25,6 +25,5 @@ public interface FileService { // 파일 이름 가져오기 List getFileName(Long id) throws Exception; - // 버킷 이름 생성 - String getBucketName() throws Exception; + String newFileName() throws Exception; } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/impl/FileServiceImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/impl/FileServiceImpl.java index ea55253..2bdee60 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/impl/FileServiceImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/File/service/impl/FileServiceImpl.java @@ -22,9 +22,11 @@ public class FileServiceImpl implements FileService { private final MinioClient minioClient; private final ArticleRepository articleRepository; + String bucketName = "article"; + // 버킷 네임 생성 @Override - public String getBucketName() { + public String newFileName() { TimeBasedGenerator generator = Generators.timeBasedGenerator(); // UUID 버전 1 생성 UUID uuid = generator.generate(); @@ -35,13 +37,16 @@ public String getBucketName() { // 파일 업로드 과정 (생성, 수정 중복) @Override public FileResponse uploadFile(List files, String bucketName) throws Exception { + // 이미지 url 저장 List urls = new ArrayList<>(); for (MultipartFile file : files) { - String fileName = file.getOriginalFilename(); // 파일이름 가져오고 + String fileName = newFileName(); // 파일이름 가져오고 InputStream fileStream = file.getInputStream(); // 파일 데이터를 읽고 가져온다. + + PutObjectArgs putObjectArgs = PutObjectArgs.builder() .bucket(bucketName) .object(fileName) @@ -69,8 +74,6 @@ public FileResponse uploadFile(List files, String bucketName) thr // 파일 업로드 @Override public FileResponse createFile(List files) throws Exception { - // 버킷 이름 저장 - String bucketName = getBucketName(); if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); @@ -81,8 +84,8 @@ public FileResponse createFile(List files) throws Exception { @Override public FileResponse updateFile(List files, Long id) throws Exception { - String bucketName = articleRepository.getByIdOrThrow(id).getBucketName(); + deleteFile(id); return uploadFile(files, bucketName); } @@ -90,8 +93,8 @@ public FileResponse updateFile(List files, Long id) throws Except // 이미지 파일 삭제하기 @Override public void deleteFile(Long id) throws Exception { - String bucketName = articleRepository.getByIdOrThrow(id).getBucketName(); - List fileNames = getFileName(id); + + List fileNames = getFileName(id); // 각 파일이름 받아오고 거기서 삭제 for (String file : fileNames) { minioClient.removeObject( @@ -121,6 +124,7 @@ public void deleteBucket(Long id) throws Exception { @Override public List getFileName(Long id) { List urls = articleRepository.getByIdOrThrow(id).getFiles(); + log.info("[getFileName] urls : {}", urls); List fileNames = new ArrayList<>(); @@ -131,7 +135,7 @@ public List getFileName(Long id) { fileNames.add(fileName); } - + log.info("[getFileName] fileNames : {}", fileNames); return fileNames; } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java index f5519c3..34facfb 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/entity/Article.java @@ -28,7 +28,9 @@ public class Article extends BaseEntity { @Column(name = "price", length = 20) private Long price; // 가격 - @ElementCollection // List을 JPA에서 사용할 수 있도록 설정 + // 도메인, 엔티티로 빼야되는 경우가 많음 + // 원투매니 ( 수정이 거의 없을 때 ) + @ElementCollection(fetch = FetchType.EAGER) // List을 JPA에서 사용할 수 있도록 설정 ( 더티체킹을 변경할 때 마다 안 함) / 로딩 비효율적 잘 찾아볼것 @CollectionTable(name = "bucketName") // 별도의 테이블을 생성 (file_images 테이블) @Column(name = "image_url", length = 2048) // 테이블의 컬럼명 지정 private List files; // 이미지 저장 @@ -41,7 +43,7 @@ public class Article extends BaseEntity { private State state; @Column(name = "category") - @Enumerated(EnumType.STRING) + @Enumerated(EnumType.STRING) // 다이어리 코드 공부 좀 했슴다 private Category category; // 게시글 생성 빌더 diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java index 050eab0..27d1c2a 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java @@ -94,8 +94,7 @@ public ArticleResponse updateArticle(Long id, String name, String content, List< // 삭제 @Override public void deleteArticle(Long id) throws Exception { - fileService.deleteBucket(id); - + fileService.deleteFile(id); articleRepository.deleteById(id); } diff --git a/buy-sell-service/src/main/java/project/buysellservice/global/config/MinioConfig.java b/buy-sell-service/src/main/java/project/buysellservice/global/config/MinioConfig.java index 8da1f9e..9a5ff10 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/global/config/MinioConfig.java +++ b/buy-sell-service/src/main/java/project/buysellservice/global/config/MinioConfig.java @@ -1,11 +1,8 @@ package project.buysellservice.global.config; import io.minio.MinioClient; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; @ConfigurationProperties public class MinioConfig { diff --git a/buy-sell-service/src/main/resources/application.yml b/buy-sell-service/src/main/resources/application.yml index 79671f6..2ba0726 100644 --- a/buy-sell-service/src/main/resources/application.yml +++ b/buy-sell-service/src/main/resources/application.yml @@ -9,5 +9,4 @@ minio: server: url: http://localhost:9000 access-key: minioadmin - secret-key: minioadmin123 - bucket: files \ No newline at end of file + secret-key: minioadmin123 \ No newline at end of file From ec63329dd6344ad81f2e9809254db351d092c89a Mon Sep 17 00:00:00 2001 From: HyeonSeong Date: Thu, 20 Feb 2025 15:03:10 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat=20:=20=EC=A0=84=EC=B2=B4=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20(=EC=98=A4=EB=9E=98=EB=90=9C=EC=88=9C)=20?= =?UTF-8?q?=EB=B3=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ArticleRepositoryCustom.java | 18 ++++++++++++++++-- .../impl/ArticleRepositoryCustomImpl.java | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java index c30b3e3..9e65bc4 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java @@ -6,9 +6,23 @@ import project.buysellservice.domain.article.entity.Category; public interface ArticleRepositoryCustom { - // 전체 게시글 받아오기 - Page
findAllByOrderByCreatedAt(Pageable pageable); + // 전체 게시글 받아오기 (최신순) + Page
findAllByOrderByCreatedAtDesc(Pageable pageable); + + // 전체 게시글 받아오기 (오래된순) + Page
findAllByOrderByCreatedAtAsc(Pageable pageable); // 해당 카테고리 글 가져오기 Page
findAllByCategory(Category category, Pageable pageable); + + // 거래가능만 보기 + + // 가격별로 보기 (나눔) + + // 가격별로 보기 (5000원 이하) + + // 가격별로 보기 (10000원 이하) + + // 가격별로 보기 (직접 설정) + } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java index bab245a..791312e 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java @@ -17,7 +17,7 @@ public class ArticleRepositoryCustomImpl implements ArticleRepositoryCustom { private final JPAQueryFactory jpaQueryFactory; @Override - public Page
findAllByOrderByCreatedAt(Pageable pageable) { + public Page
findAllByOrderByCreatedAtDesc(Pageable pageable) { QArticle article = QArticle.article; List
articles = jpaQueryFactory @@ -30,6 +30,20 @@ public Page
findAllByOrderByCreatedAt(Pageable pageable) { return new PageImpl<>(articles); } + @Override + public Page
findAllByOrderByCreatedAtAsc(Pageable pageable) { + QArticle article = QArticle.article; + + List
articles = jpaQueryFactory + .selectFrom(article) + .orderBy(article.createDate.asc()) + .offset(pageable.getOffset()) // 페이지 시작 위치 + .limit(pageable.getPageSize()) // 한 페이지에 표시할 개수 + .fetch(); + + return new PageImpl<>(articles); + } + @Override public Page
findAllByCategory(Category category, Pageable pageable) { QArticle article = QArticle.article; From 59194b19e91b0178a3ee08e5d6853cb3cfaeeefa Mon Sep 17 00:00:00 2001 From: HyeonSeong Date: Thu, 20 Feb 2025 15:12:37 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat=20:=20=EA=B5=AC=EB=A7=A4=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=20=EA=B2=8C=EC=8B=9C=ED=8C=90=EB=A7=8C=20=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiV1ArticleController.java | 15 ++++++++++- .../repository/ArticleRepositoryCustom.java | 1 + .../impl/ArticleRepositoryCustomImpl.java | 17 +++++++++++++ .../article/service/ArticleService.java | 6 +++++ .../service/impl/ArticleServiceImpl.java | 25 +++++++++++++++++-- 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java index f30c035..9b335de 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java @@ -1,7 +1,6 @@ package project.buysellservice.domain.article.controller; import jakarta.validation.Valid; -import jakarta.ws.rs.core.Response; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.PageRequest; @@ -29,12 +28,26 @@ public ResponseEntity> getArticle(@Valid @RequestParam("pa return ResponseEntity.ok(articleService.readAllArticles(PageRequest.of(page, size))); } + // 게시글 전체 (오래된순) + @GetMapping("/asc") + public ResponseEntity> getArticleAsc(@Valid @RequestParam("page") int page, + @Valid @RequestParam("size") int size) { + return ResponseEntity.ok(articleService.readAllArticlesOrderByAsc(PageRequest.of(page, size))); + } + // 게시글 가져오기 @GetMapping("/{id}") public ResponseEntity getArticleById(@PathVariable("id") Long id) { return ResponseEntity.ok(articleService.readArticle(id)); } + // 구매가능 게시글만 보기 + @GetMapping("/stateSell") + public ResponseEntity> getArticleStateSell(@Valid @RequestParam("page") int page, + @Valid @RequestParam("size") int size) { + return ResponseEntity.ok(articleService.readByStateSell(PageRequest.of(page, size))); + } + // 게시글 생성 @PostMapping(consumes = "multipart/form-data") public ResponseEntity createArticle(@RequestPart("articleRequest") @Valid ArticleRequest articleRequest, diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java index 9e65bc4..1d30100 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java @@ -16,6 +16,7 @@ public interface ArticleRepositoryCustom { Page
findAllByCategory(Category category, Pageable pageable); // 거래가능만 보기 + Page
findBySellStatus(Pageable pageable); // 가격별로 보기 (나눔) diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java index 791312e..ea483cc 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java @@ -8,6 +8,7 @@ import project.buysellservice.domain.article.entity.Article; import project.buysellservice.domain.article.entity.Category; import project.buysellservice.domain.article.entity.QArticle; +import project.buysellservice.domain.article.entity.State; import project.buysellservice.domain.article.repository.ArticleRepositoryCustom; import java.util.List; @@ -58,4 +59,20 @@ public Page
findAllByCategory(Category category, Pageable pageable) { return new PageImpl<>(articles); } + + @Override + public Page
findBySellStatus(Pageable pageable) { + QArticle article = QArticle.article; + + List
articles = jpaQueryFactory + .selectFrom(article) + .where(article.state.eq(State.SELL)) + .orderBy(article.createDate.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + return new PageImpl<>(articles); + + } } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java index 5260091..bc9f073 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java @@ -23,10 +23,16 @@ public interface ArticleService { // 전체 게시글 읽기 List readAllArticles(Pageable pageable); + // 전체 게시글 읽기 (오래된순) + List readAllArticlesOrderByAsc(Pageable pageable); + // 게시글 팔림 처리 ArticleResponse soldArticle(Long id); // 카테고리 게시글 가져오기 List readCategoryArticle(Category category, Pageable pageable); + // 구매가능 게시판만 보기 + List readByStateSell(Pageable pageable); + } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java index 27d1c2a..224a47f 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java @@ -22,16 +22,37 @@ public class ArticleServiceImpl implements ArticleService { private final ArticleRepository articleRepository; private final FileService fileService; - // 모든 게시글 가져오기 + // 모든 게시글 가져오기 (최신순) @Override public List readAllArticles(Pageable pageable) { - Page
articles = articleRepository.findAllByOrderByCreatedAt(pageable); + Page
articles = articleRepository.findAllByOrderByCreatedAtDesc(pageable); List
article = articles.getContent(); return ArticleResponse.listOf(article); } + // 모든 게시글 가져오기 (오래된순) + @Override + public List readAllArticlesOrderByAsc(Pageable pageable) { + Page
articles = articleRepository.findAllByOrderByCreatedAtAsc(pageable); + + List
article = articles.getContent(); + + return ArticleResponse.listOf(article); + } + + // 구매 가능 게시판만 보기 + @Override + public List readByStateSell(Pageable pageable) { + Page
articles = articleRepository.findBySellStatus(pageable); + + List
article = articles.getContent(); + + return ArticleResponse.listOf(article); + } + + // 게시글 팔림 처리 @Override public ArticleResponse soldArticle(Long id) { From 46025cfd1ef6720b58c3f944e64c04c92f1620db Mon Sep 17 00:00:00 2001 From: HyeonSeong Date: Thu, 20 Feb 2025 15:28:18 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat=20:=20=EA=B0=80=EA=B2=A9=EB=8C=80?= =?UTF-8?q?=EB=B3=84=20=EA=B2=8C=EC=8B=9C=ED=8C=90=20=EB=B3=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiV1ArticleController.java | 9 +++++++++ .../repository/ArticleRepositoryCustom.java | 7 +------ .../impl/ArticleRepositoryCustomImpl.java | 15 +++++++++++++++ .../domain/article/service/ArticleService.java | 3 +++ .../article/service/impl/ArticleServiceImpl.java | 8 ++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java index 9b335de..3a0094a 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java @@ -48,6 +48,15 @@ public ResponseEntity> getArticleStateSell(@Valid @Request return ResponseEntity.ok(articleService.readByStateSell(PageRequest.of(page, size))); } + // 가격대 별로 게시판 보기 + @GetMapping("/price") + public ResponseEntity> getArticlePrice(@Valid @RequestParam("page") int page, + @Valid @RequestParam("size") int size, + @Valid @RequestParam("minPrice") int minPrice, + @Valid @RequestParam("maxPrice") int maxPrice) { + return ResponseEntity.ok(articleService.readByPrice(PageRequest.of(page, size), minPrice, maxPrice)); + } + // 게시글 생성 @PostMapping(consumes = "multipart/form-data") public ResponseEntity createArticle(@RequestPart("articleRequest") @Valid ArticleRequest articleRequest, diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java index 1d30100..4ff77be 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java @@ -18,12 +18,7 @@ public interface ArticleRepositoryCustom { // 거래가능만 보기 Page
findBySellStatus(Pageable pageable); - // 가격별로 보기 (나눔) - - // 가격별로 보기 (5000원 이하) - - // 가격별로 보기 (10000원 이하) - // 가격별로 보기 (직접 설정) + Page
findByPrice(Pageable pageable, int minPrice, int maxPrice); } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java index ea483cc..2cd1d1e 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java @@ -75,4 +75,19 @@ public Page
findBySellStatus(Pageable pageable) { return new PageImpl<>(articles); } + + @Override + public Page
findByPrice(Pageable pageable, int minPrice, int maxPrice) { + QArticle article = QArticle.article; + + List
articles = jpaQueryFactory + .selectFrom(article) + .where(article.price.goe(minPrice), article.price.loe(maxPrice)) + .orderBy(article.createDate.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + return new PageImpl<>(articles); + } } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java index bc9f073..4235ecf 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/ArticleService.java @@ -35,4 +35,7 @@ public interface ArticleService { // 구매가능 게시판만 보기 List readByStateSell(Pageable pageable); + // 가격대 별로 게시판 보기 + List readByPrice(Pageable pageable, int minPrice, int maxPrice); + } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java index 224a47f..767be6b 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java @@ -52,6 +52,14 @@ public List readByStateSell(Pageable pageable) { return ArticleResponse.listOf(article); } + // 가격대별로 게시판 보기 + @Override + public List readByPrice(Pageable pageable, int minPrice, int maxPrice) { + Page
articles = articleRepository.findByPrice(pageable, minPrice, maxPrice); + List
article = articles.getContent(); + return ArticleResponse.listOf(article); + } + // 게시글 팔림 처리 @Override From fff468c2c52a8037a22d967a748a36f8c485cd50 Mon Sep 17 00:00:00 2001 From: HyeonSeong Date: Thu, 20 Feb 2025 16:04:12 +0900 Subject: [PATCH 6/6] refactor : page -> slice --- .../controller/ApiV1ArticleController.java | 26 ++++++-------- .../repository/ArticleRepositoryCustom.java | 11 +++--- .../impl/ArticleRepositoryCustomImpl.java | 36 +++++++++++++------ .../service/impl/ArticleServiceImpl.java | 11 +++--- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java index 3a0094a..5f7d496 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/controller/ApiV1ArticleController.java @@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -23,16 +24,14 @@ public class ApiV1ArticleController { // 게시글 전체 페이징 처리 @GetMapping - public ResponseEntity> getArticle(@Valid @RequestParam("page") int page, - @Valid @RequestParam("size") int size) { - return ResponseEntity.ok(articleService.readAllArticles(PageRequest.of(page, size))); + public ResponseEntity> getArticle(Pageable pageable) { + return ResponseEntity.ok(articleService.readAllArticles(pageable)); } // 게시글 전체 (오래된순) @GetMapping("/asc") - public ResponseEntity> getArticleAsc(@Valid @RequestParam("page") int page, - @Valid @RequestParam("size") int size) { - return ResponseEntity.ok(articleService.readAllArticlesOrderByAsc(PageRequest.of(page, size))); + public ResponseEntity> getArticleAsc(Pageable pageable) { + return ResponseEntity.ok(articleService.readAllArticlesOrderByAsc(pageable)); } // 게시글 가져오기 @@ -43,18 +42,16 @@ public ResponseEntity getArticleById(@PathVariable("id") Long i // 구매가능 게시글만 보기 @GetMapping("/stateSell") - public ResponseEntity> getArticleStateSell(@Valid @RequestParam("page") int page, - @Valid @RequestParam("size") int size) { - return ResponseEntity.ok(articleService.readByStateSell(PageRequest.of(page, size))); + public ResponseEntity> getArticleStateSell(Pageable pageable) { + return ResponseEntity.ok(articleService.readByStateSell(pageable)); } // 가격대 별로 게시판 보기 @GetMapping("/price") - public ResponseEntity> getArticlePrice(@Valid @RequestParam("page") int page, - @Valid @RequestParam("size") int size, + public ResponseEntity> getArticlePrice(Pageable pageable, @Valid @RequestParam("minPrice") int minPrice, @Valid @RequestParam("maxPrice") int maxPrice) { - return ResponseEntity.ok(articleService.readByPrice(PageRequest.of(page, size), minPrice, maxPrice)); + return ResponseEntity.ok(articleService.readByPrice(pageable, minPrice, maxPrice)); } // 게시글 생성 @@ -91,9 +88,8 @@ public ResponseEntity soldArticle(@Valid @PathVariable("id") Lo // 해당 카테고리 글만 가져오기 @GetMapping("/category") public ResponseEntity> getArticleByCategory(@Valid @RequestParam("category") Category category, - @Valid @RequestParam("page") int page, - @Valid @RequestParam("size") int size) { - return ResponseEntity.ok(articleService.readCategoryArticle(category, PageRequest.of(page, size))); + Pageable pageable) { + return ResponseEntity.ok(articleService.readCategoryArticle(category, pageable)); } } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java index 4ff77be..41625b4 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/ArticleRepositoryCustom.java @@ -2,23 +2,24 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import project.buysellservice.domain.article.entity.Article; import project.buysellservice.domain.article.entity.Category; public interface ArticleRepositoryCustom { // 전체 게시글 받아오기 (최신순) - Page
findAllByOrderByCreatedAtDesc(Pageable pageable); + Slice
findAllByOrderByCreatedAtDesc(Pageable pageable); // 전체 게시글 받아오기 (오래된순) - Page
findAllByOrderByCreatedAtAsc(Pageable pageable); + Slice
findAllByOrderByCreatedAtAsc(Pageable pageable); // 해당 카테고리 글 가져오기 - Page
findAllByCategory(Category category, Pageable pageable); + Slice
findAllByCategory(Category category, Pageable pageable); // 거래가능만 보기 - Page
findBySellStatus(Pageable pageable); + Slice
findBySellStatus(Pageable pageable); // 가격별로 보기 (직접 설정) - Page
findByPrice(Pageable pageable, int minPrice, int maxPrice); + Slice
findByPrice(Pageable pageable, int minPrice, int maxPrice); } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java index 2cd1d1e..70c0ac6 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/repository/impl/ArticleRepositoryCustomImpl.java @@ -2,9 +2,10 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.SliceImpl; import project.buysellservice.domain.article.entity.Article; import project.buysellservice.domain.article.entity.Category; import project.buysellservice.domain.article.entity.QArticle; @@ -17,8 +18,20 @@ public class ArticleRepositoryCustomImpl implements ArticleRepositoryCustom { private final JPAQueryFactory jpaQueryFactory; + // 다음 페이지가 있는지 여부 확인 + public static boolean checkHasNext(List items, Pageable pageable) { + boolean hasNext = false; + + if (items.size() > pageable.getPageSize()) { + items.remove(items.size() - 1); // 마지막 요소 제거 + hasNext = true; + } + + return hasNext; + } + @Override - public Page
findAllByOrderByCreatedAtDesc(Pageable pageable) { + public Slice
findAllByOrderByCreatedAtDesc(Pageable pageable) { QArticle article = QArticle.article; List
articles = jpaQueryFactory @@ -28,11 +41,12 @@ public Page
findAllByOrderByCreatedAtDesc(Pageable pageable) { .limit(pageable.getPageSize()) // 한 페이지에 표시할 개수 .fetch(); - return new PageImpl<>(articles); + + return new SliceImpl<>(articles, pageable, checkHasNext(articles, pageable)); } @Override - public Page
findAllByOrderByCreatedAtAsc(Pageable pageable) { + public Slice
findAllByOrderByCreatedAtAsc(Pageable pageable) { QArticle article = QArticle.article; List
articles = jpaQueryFactory @@ -42,11 +56,11 @@ public Page
findAllByOrderByCreatedAtAsc(Pageable pageable) { .limit(pageable.getPageSize()) // 한 페이지에 표시할 개수 .fetch(); - return new PageImpl<>(articles); + return new SliceImpl<>(articles, pageable, checkHasNext(articles, pageable)); } @Override - public Page
findAllByCategory(Category category, Pageable pageable) { + public Slice
findAllByCategory(Category category, Pageable pageable) { QArticle article = QArticle.article; List
articles = jpaQueryFactory @@ -57,11 +71,11 @@ public Page
findAllByCategory(Category category, Pageable pageable) { .limit(pageable.getPageSize()) // 한 페이지에 표시할 개수 .fetch(); - return new PageImpl<>(articles); + return new SliceImpl<>(articles, pageable, checkHasNext(articles, pageable)); } @Override - public Page
findBySellStatus(Pageable pageable) { + public Slice
findBySellStatus(Pageable pageable) { QArticle article = QArticle.article; List
articles = jpaQueryFactory @@ -72,12 +86,12 @@ public Page
findBySellStatus(Pageable pageable) { .limit(pageable.getPageSize()) .fetch(); - return new PageImpl<>(articles); + return new SliceImpl<>(articles, pageable, checkHasNext(articles, pageable)); } @Override - public Page
findByPrice(Pageable pageable, int minPrice, int maxPrice) { + public Slice
findByPrice(Pageable pageable, int minPrice, int maxPrice) { QArticle article = QArticle.article; List
articles = jpaQueryFactory @@ -88,6 +102,6 @@ public Page
findByPrice(Pageable pageable, int minPrice, int maxPrice) .limit(pageable.getPageSize()) .fetch(); - return new PageImpl<>(articles); + return new SliceImpl<>(articles, pageable, checkHasNext(articles, pageable)); } } diff --git a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java index 767be6b..1de1e55 100644 --- a/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java +++ b/buy-sell-service/src/main/java/project/buysellservice/domain/article/service/impl/ArticleServiceImpl.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; 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.web.multipart.MultipartFile; import project.buysellservice.domain.File.service.FileService; @@ -25,7 +26,7 @@ public class ArticleServiceImpl implements ArticleService { // 모든 게시글 가져오기 (최신순) @Override public List readAllArticles(Pageable pageable) { - Page
articles = articleRepository.findAllByOrderByCreatedAtDesc(pageable); + Slice
articles = articleRepository.findAllByOrderByCreatedAtDesc(pageable); List
article = articles.getContent(); @@ -35,7 +36,7 @@ public List readAllArticles(Pageable pageable) { // 모든 게시글 가져오기 (오래된순) @Override public List readAllArticlesOrderByAsc(Pageable pageable) { - Page
articles = articleRepository.findAllByOrderByCreatedAtAsc(pageable); + Slice
articles = articleRepository.findAllByOrderByCreatedAtAsc(pageable); List
article = articles.getContent(); @@ -45,7 +46,7 @@ public List readAllArticlesOrderByAsc(Pageable pageable) { // 구매 가능 게시판만 보기 @Override public List readByStateSell(Pageable pageable) { - Page
articles = articleRepository.findBySellStatus(pageable); + Slice
articles = articleRepository.findBySellStatus(pageable); List
article = articles.getContent(); @@ -55,7 +56,7 @@ public List readByStateSell(Pageable pageable) { // 가격대별로 게시판 보기 @Override public List readByPrice(Pageable pageable, int minPrice, int maxPrice) { - Page
articles = articleRepository.findByPrice(pageable, minPrice, maxPrice); + Slice
articles = articleRepository.findByPrice(pageable, minPrice, maxPrice); List
article = articles.getContent(); return ArticleResponse.listOf(article); } @@ -85,7 +86,7 @@ public ArticleResponse readArticle(Long id) { @Override public List readCategoryArticle(Category category, Pageable pageable) { - Page
articles = articleRepository.findAllByCategory(category, pageable); + Slice
articles = articleRepository.findAllByCategory(category, pageable); List
article = articles.getContent();