package sagan.blog.support;

import sagan.blog.Post;
import sagan.support.DateService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class PostViewFactory {
    private DateService dateService;

    @Autowired
    public PostViewFactory(DateService dateService) {
        this.dateService = dateService;
    }

    public PostView createPostView(Post post) {
        return new PostView(post, dateService);
    }

    public List<PostView> createPostViewList(List<Post> posts) {
        List<PostView> postViews = new ArrayList<>();
        for (Post post : posts) {
            postViews.add(createPostView(post));
        }
        return postViews;
    }

    public Page<PostView> createPostViewPage(Page<Post> posts) {
        List<PostView> postViews = createPostViewList(posts.getContent());
        PageRequest pageRequest = new PageRequest(posts.getNumber(), posts.getSize(), posts.getSort());
        return new PageImpl<>(postViews, pageRequest, posts.getTotalElements());
    }
}

@@ -384,26 +384,30 @@
 ype.
-Service;%0A%0A@Service
+Component;%0A%0A@Component
 %0Apub
@@ -434,16 +434,17 @@
 ctory %7B%0A
+%0A
     priv
@@ -703,20 +703,20 @@
  public 
-List
+Page
 %3CPostVie
@@ -736,17 +736,17 @@
 View
-List(List
+Page(Page
 %3CPos
@@ -837,24 +837,37 @@
 post : posts
+.getContent()
 ) %7B%0A        
@@ -921,181 +921,8 @@
   %7D%0A
-        return postViews;%0A    %7D%0A%0A    public Page%3CPostView%3E createPostViewPage(Page%3CPost%3E posts) %7B%0A        List%3CPostView%3E postViews = createPostViewList(posts.getContent());%0A
     

Refactor PostViewFactory