Skip to content

Commit

Permalink
Merge pull request #41 from olmangjolmang/OMJM-104-post_sort_enhance
Browse files Browse the repository at this point in the history
✨ Feat: post 최신순, 오래된순, 스크랩순 정렬 기능
  • Loading branch information
chaeyeonKong authored Jul 18, 2024
2 parents face3b2 + 3930ee7 commit 03d7458
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
import com.ticle.server.memo.dto.MemoDto;
import com.ticle.server.memo.dto.MemoRequest;
import com.ticle.server.post.domain.Post;
import com.ticle.server.post.domain.type.PostSort;
import com.ticle.server.post.dto.PostResponse;
import com.ticle.server.post.dto.QuizResponse;
import com.ticle.server.post.service.PostService;
import com.ticle.server.scrapped.domain.Scrapped;
import com.ticle.server.scrapped.dto.ScrappedDto;
import com.ticle.server.user.domain.type.Category;
import com.ticle.server.user.service.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -33,11 +34,14 @@ public class PostApiController {
private final PostService postService;

//카테고리로 아티클 조회
@Operation(summary = "아티클 조회", description = "카테고리로 아티클 조회 | 공백 입력시 모든 카테고리의 아티클 나타남 ")
@Operation(summary = "아티클 조회", description = "카테고리로 아티클 조회 | post?category={category}&orderBy={orderBy}&page={page} 과 같은 형식으로 동작합니다. category 미입력시 모든 아티클, orderBy 미입력시 최신순, page 미입력시 첫 페이지입니다. ")
@GetMapping
public ResponseEntity<ResponseTemplate<Object>> findAllArticles(@RequestParam(required = false) String category, @RequestParam(defaultValue = "1") int page) {
public ResponseEntity<ResponseTemplate<Object>>
findAllArticles(@RequestParam(required = false, defaultValue = "") Category category,
@RequestParam(required = false, defaultValue = "LATEST") PostSort orderBy,
@RequestParam(required = false, defaultValue = "1") Integer page) {

Page<PostResponse> postPage = postService.findAllByCategory(category, page);
Page<PostResponse> postPage = postService.findAllByCategory(category, orderBy, page);

return ResponseEntity
.status(HttpStatus.OK)
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/ticle/server/post/domain/type/PostSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ticle.server.post.domain.type;

import com.ticle.server.opinion.domain.type.Order;
import org.springframework.data.domain.Sort;

public enum PostSort {
LATEST, // 최신순
OLDEST, // 오래된순
SCRAPPED; // 스크랩순

public static Sort getOrder(PostSort orderBy) {
return switch (orderBy) {
case LATEST -> Sort.by(Sort.Direction.DESC, "createdDate");
case OLDEST -> Sort.by(Sort.Direction.ASC, "createdDate");
case SCRAPPED -> Sort.by(Sort.Direction.DESC, "scrapCount");

};
}

}
16 changes: 9 additions & 7 deletions src/main/java/com/ticle/server/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ticle.server.memo.domain.Memo;
import com.ticle.server.mypage.repository.NoteRepository;
import com.ticle.server.post.domain.type.PostSort;
import com.ticle.server.post.dto.*;
import com.ticle.server.scrapped.dto.ScrappedDto;
import com.ticle.server.user.domain.type.Category;
Expand Down Expand Up @@ -42,23 +43,24 @@ public class PostService {


// 카테고리에 맞는 글 찾기
public Page<PostResponse> findAllByCategory(String category, int page) {
public Page<PostResponse> findAllByCategory(Category category, PostSort orderBy, Integer page) {

final int SIZE = 9; // 한 페이지에 보여질 객체 수
Pageable pageable;

Sort sort = PostSort.getOrder(orderBy);
pageable = PageRequest.of(page - 1, SIZE, sort);

//최신순으로 post 정렬
Pageable pageable = PageRequest.of(page - 1, SIZE, Sort.by(Sort.Direction.DESC, "createdDate"));
Page<Post> postPage;

if (category == null || category.isEmpty()) {
if (category == null) {
// 모든 글 조회
postPage = postRepository.findAll(pageable);
} else {
//카테고리에 맞는 글 조회
postPage = postRepository.findByCategory(Category.valueOf(category), pageable);
// 카테고리에 맞는 글 조회
postPage = postRepository.findByCategory(category, pageable);
}
return postPage.map(PostResponse::from);

}


Expand Down

0 comments on commit 03d7458

Please sign in to comment.