Skip to content

Commit

Permalink
Merge pull request #20 from olmangjolmang/OMJM-81-post-v-2
Browse files Browse the repository at this point in the history
아티클 가져오는 controller 경량화
  • Loading branch information
chaeyeonKong authored Jul 7, 2024
2 parents e829eba + 3c14219 commit a6ec0e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,16 @@ public class PostApiController {
@GetMapping
public ResponseEntity<ResponseTemplate<Object>> findAllArticles(@RequestParam(required = false) String category, @RequestParam(defaultValue = "1") int page) {

int size = 9; // 한 페이지에 보여질 객체 수
Page<PostResponse> postPage = postService.findAllByCategory(category, page);

Pageable pageable = PageRequest.of(page - 1, size);

Page<Post> postPage = postService.findAllByCategory(category, pageable);

List<PostResponse> postResponses = postPage.getContent().stream()
.map(PostResponse::from)
.collect(Collectors.toList());
// List<PostResponse> postResponses = postPage.getContent().stream()
// .map(PostResponse::from)
// .collect(Collectors.toList());


return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(postResponses));
.body(ResponseTemplate.from(postPage));
}

//특정 아티클 조회
Expand Down
22 changes: 14 additions & 8 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.MemoRepository;
import com.ticle.server.post.dto.PostResponse;
import com.ticle.server.scrapped.dto.ScrappedDto;
import com.ticle.server.user.domain.type.Category;
import com.ticle.server.post.domain.Post;
Expand All @@ -12,17 +13,14 @@
import com.ticle.server.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.Optional;
import java.util.List;

@RequiredArgsConstructor
@Service
Expand All @@ -34,14 +32,22 @@ public class PostService {
private final MemoRepository memoRepository;

// 카테고리에 맞는 글 찾기
public Page<Post> findAllByCategory(String category, Pageable pageable) {
public Page<PostResponse> findAllByCategory(String category, int page) {

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

Pageable pageable = PageRequest.of(page - 1, SIZE);
Page<Post> postPage;

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

}

//postId로 조회한 특정 post 정보 리턴
Expand Down Expand Up @@ -94,7 +100,7 @@ public Object writeMemo(long id, UserDetails userDetails, String targetText, Str
memo.setUser(user);
memo.setTargetText(targetText);
memo.setContent(content);

return memoRepository.save(memo);
}
}

0 comments on commit a6ec0e6

Please sign in to comment.