From d3b706909b972c8516b6e6d1dc65d7f80d95ffb7 Mon Sep 17 00:00:00 2001 From: Jordi <55429631+jorbush@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:16:44 +0200 Subject: [PATCH 1/5] implement pagination --- .../postrifybackend/config/WebConfig.java | 3 + .../controller/PostController.java | 10 +- .../postrifybackend/service/PostService.java | 14 +-- .../postrifybackend/PostControllerTest.java | 16 ++- .../src/app/components/home/home.component.ts | 103 +++++++++++++++++- .../src/app/models/page.model.ts | 9 ++ .../src/app/services/post.service.ts | 14 ++- 7 files changed, 145 insertions(+), 24 deletions(-) create mode 100644 postrify-frontend/src/app/models/page.model.ts diff --git a/postrify-backend/src/main/java/com/postrify/postrifybackend/config/WebConfig.java b/postrify-backend/src/main/java/com/postrify/postrifybackend/config/WebConfig.java index d2a89b5..bc7354b 100644 --- a/postrify-backend/src/main/java/com/postrify/postrifybackend/config/WebConfig.java +++ b/postrify-backend/src/main/java/com/postrify/postrifybackend/config/WebConfig.java @@ -1,9 +1,12 @@ package com.postrify.postrifybackend.config; +import org.springframework.data.web.config.EnableSpringDataWebSupport; +import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @org.springframework.context.annotation.Configuration +@EnableSpringDataWebSupport(pageSerializationMode = PageSerializationMode.VIA_DTO) public class WebConfig implements WebMvcConfigurer { @Override diff --git a/postrify-backend/src/main/java/com/postrify/postrifybackend/controller/PostController.java b/postrify-backend/src/main/java/com/postrify/postrifybackend/controller/PostController.java index 39086d6..d3cc803 100644 --- a/postrify-backend/src/main/java/com/postrify/postrifybackend/controller/PostController.java +++ b/postrify-backend/src/main/java/com/postrify/postrifybackend/controller/PostController.java @@ -9,6 +9,10 @@ import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @@ -22,8 +26,10 @@ public class PostController { @Autowired private UserService userService; @GetMapping - public List getAllPosts() { - return postService.getAllPosts(); + public Page getAllPosts( + @PageableDefault(page = 0, size = 10, sort = "updatedAt", direction = Sort.Direction.DESC) + Pageable pageable) { + return postService.getAllPosts(pageable); } @GetMapping("/{id}") diff --git a/postrify-backend/src/main/java/com/postrify/postrifybackend/service/PostService.java b/postrify-backend/src/main/java/com/postrify/postrifybackend/service/PostService.java index 2f76902..0852e27 100644 --- a/postrify-backend/src/main/java/com/postrify/postrifybackend/service/PostService.java +++ b/postrify-backend/src/main/java/com/postrify/postrifybackend/service/PostService.java @@ -9,6 +9,8 @@ import java.util.Optional; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,16 +22,8 @@ public class PostService { @Autowired private UserService userService; @Transactional(readOnly = true) - public List getAllPosts() { - return postRepository.findAll().stream() - .map(this::convertToDTO) - .collect( - Collectors.collectingAndThen( - Collectors.toList(), - list -> { - java.util.Collections.reverse(list); - return list; - })); + public Page getAllPosts(Pageable pageable) { + return postRepository.findAll(pageable).map(this::convertToDTO); } @Transactional(readOnly = true) diff --git a/postrify-backend/src/test/java/com/postrify/postrifybackend/PostControllerTest.java b/postrify-backend/src/test/java/com/postrify/postrifybackend/PostControllerTest.java index 68c0dc1..72108ed 100644 --- a/postrify-backend/src/test/java/com/postrify/postrifybackend/PostControllerTest.java +++ b/postrify-backend/src/test/java/com/postrify/postrifybackend/PostControllerTest.java @@ -22,6 +22,10 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -41,6 +45,7 @@ void setUp() { MockitoAnnotations.openMocks(this); } + @SuppressWarnings("unchecked") @Test void getAllPosts_Success() { UserDTO userDTO = new UserDTO(1L, "jordi", "jordi@mail.com"); @@ -52,13 +57,16 @@ void getAllPosts_Success() { 2L, "Post 2", "Content 2", userDTO, LocalDateTime.now(), LocalDateTime.now()); List posts = Arrays.asList(post1, post2); - when(postService.getAllPosts()).thenReturn(posts); + Pageable pageable = PageRequest.of(0, 10); + Page page = new PageImpl<>(posts, pageable, posts.size()); - List result = postController.getAllPosts(); + when(postService.getAllPosts(pageable)).thenReturn(page); + + Page result = postController.getAllPosts(pageable); assertNotNull(result); - assertEquals(2, result.size()); - verify(postService, times(1)).getAllPosts(); + assertEquals(2, result.getContent().size()); + verify(postService, times(1)).getAllPosts(pageable); } @Test diff --git a/postrify-frontend/src/app/components/home/home.component.ts b/postrify-frontend/src/app/components/home/home.component.ts index 6012d3e..e384992 100644 --- a/postrify-frontend/src/app/components/home/home.component.ts +++ b/postrify-frontend/src/app/components/home/home.component.ts @@ -4,6 +4,7 @@ import { PostService } from '../../services/post.service'; import { Router } from '@angular/router'; import { AuthService } from '../../services/auth.service'; import { CommonModule } from '@angular/common'; +import { Page } from '../../models/page.model'; @Component({ selector: 'app-home', @@ -33,6 +34,24 @@ import { CommonModule } from '@angular/common'; } +
+ + + + + + + +
@if (isLogged) {