diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 46f527b0..0c9cd26b 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,15 +1,24 @@ ## ๐Ÿš€ Description - +Included PageInfo object as an instance variable in the GetCommunityDetailsResponse component. I also included a +listAllWithPages(Pageable pageable) method which returns Page object in the CommunityService interface, and implemented in the +CommunitySDJpaService. This method was used in the listAllCommunities method in the CommunityController class. ## ๐Ÿ“„ Motivation and Context +When you list all communities by GET /communities, you get all the communities in a communities array, you can also page +them using page and size query parameters, but no pageInfo object is returned, which is required for further pagination +on the front-end. +This change was added to include page information to navigate the list of communities returned by the GET /communities +request. +https://github.com/jmprathab/MyHome/issues/262 ## ๐Ÿงช How Has This Been Tested? +Mockito unit test and postman test @@ -23,7 +32,7 @@ -- [ ] Bug fix (non-breaking change which fixes an issue) +- [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) @@ -32,6 +41,6 @@ -- [ ] My code follows the code style of this project(Do your best to follow code styles. If none apply just skip this). +- [x] My code follows the code style of this project(Do your best to follow code styles. If none apply just skip this). - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. diff --git a/api/src/main/resources/public/swagger/api.yaml b/api/src/main/resources/public/swagger/api.yaml index 2c7c766c..1a7a91bb 100644 --- a/api/src/main/resources/public/swagger/api.yaml +++ b/api/src/main/resources/public/swagger/api.yaml @@ -1175,6 +1175,8 @@ components: uniqueItems: true items: $ref: '#/components/schemas/GetCommunityDetailsResponseCommunity' + page_info: + $ref: '#/components/schemas/PageInfo' GetCommunityDetailsResponseCommunity: type: object properties: diff --git a/service/src/main/java/com/myhome/controllers/CommunityController.java b/service/src/main/java/com/myhome/controllers/CommunityController.java index 61d12782..030b8662 100644 --- a/service/src/main/java/com/myhome/controllers/CommunityController.java +++ b/service/src/main/java/com/myhome/controllers/CommunityController.java @@ -34,14 +34,15 @@ import com.myhome.model.GetHouseDetailsResponse; import com.myhome.model.ListCommunityAdminsResponse; import com.myhome.services.CommunityService; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Optional; -import java.util.Set; + +import java.util.*; import java.util.stream.Collectors; import javax.validation.Valid; + +import com.myhome.utils.PageInfo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.http.HttpStatus; @@ -72,17 +73,21 @@ public ResponseEntity createCommunity(@Valid @RequestBo return ResponseEntity.status(HttpStatus.CREATED).body(createdCommunityResponse); } + @Override public ResponseEntity listAllCommunity( @PageableDefault(size = 200) Pageable pageable) { log.trace("Received request to list all community"); - Set communityDetails = communityService.listAll(pageable); + Page communityDetails = communityService.listAllInPages(pageable); Set communityDetailsResponse = - communityApiMapper.communitySetToRestApiResponseCommunitySet(communityDetails); + communityApiMapper.communitySetToRestApiResponseCommunitySet(communityDetails.toSet()); + + PageInfo pageInfo = PageInfo.of(pageable, communityDetails); GetCommunityDetailsResponse response = new GetCommunityDetailsResponse(); response.getCommunities().addAll(communityDetailsResponse); + response.setPageInfo(pageInfo); return ResponseEntity.status(HttpStatus.OK).body(response); } diff --git a/service/src/main/java/com/myhome/services/CommunityService.java b/service/src/main/java/com/myhome/services/CommunityService.java index cc4a4f2e..272a0a9c 100644 --- a/service/src/main/java/com/myhome/services/CommunityService.java +++ b/service/src/main/java/com/myhome/services/CommunityService.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Optional; import java.util.Set; + +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface CommunityService { @@ -32,6 +34,8 @@ public interface CommunityService { Set listAll(Pageable pageable); + Page listAllInPages(Pageable pageable); + Optional getCommunityDetailsById(String communityId); Optional> findCommunityHousesById(String communityId, Pageable pageable); diff --git a/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java b/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java index 3bfd7bea..9c6aaf81 100644 --- a/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java +++ b/service/src/main/java/com/myhome/services/springdatajpa/CommunitySDJpaService.java @@ -36,6 +36,7 @@ import javax.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @@ -78,6 +79,11 @@ public Set listAll(Pageable pageable) { return communityListSet; } + @Override + public Page listAllInPages(Pageable pageable) { + return communityRepository.findAll(pageable); + } + @Override public Set listAll() { Set communities = new HashSet<>(); communityRepository.findAll().forEach(communities::add); diff --git a/service/src/test/java/com/myhome/controllers/CommunityControllerTest.java b/service/src/test/java/com/myhome/controllers/CommunityControllerTest.java index da33efae..c46c04e7 100644 --- a/service/src/test/java/com/myhome/controllers/CommunityControllerTest.java +++ b/service/src/test/java/com/myhome/controllers/CommunityControllerTest.java @@ -41,11 +41,15 @@ import java.util.List; import java.util.Optional; import java.util.Set; + +import com.myhome.utils.PageInfo; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; 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; @@ -161,6 +165,7 @@ void shouldListAllCommunitiesSuccessfully() { Community community = createTestCommunity(); communities.add(community); + Set communityDetailsResponse = new HashSet<>(); communityDetailsResponse.add( @@ -170,14 +175,26 @@ void shouldListAllCommunitiesSuccessfully() { .district(COMMUNITY_DISTRICT) ); + List communityList = new ArrayList<>(); + communityList.add(community); + + Page communityPage = new PageImpl<>(communityList) ; + + Pageable pageable = PageRequest.of(0, 1); + PageInfo pageInfo = PageInfo.of(pageable, communityPage); + GetCommunityDetailsResponse response = new GetCommunityDetailsResponse(); response.getCommunities().addAll(communityDetailsResponse); + response.setPageInfo(pageInfo); + - Pageable pageable = PageRequest.of(0, 1); given(communityService.listAll(pageable)) .willReturn(communities); given(communityApiMapper.communitySetToRestApiResponseCommunitySet(communities)) .willReturn(communityDetailsResponse); + given(communityService.listAllInPages(pageable)) + .willReturn(communityPage); + // when ResponseEntity responseEntity = @@ -187,7 +204,7 @@ void shouldListAllCommunitiesSuccessfully() { assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(response, responseEntity.getBody()); verify(communityApiMapper).communitySetToRestApiResponseCommunitySet(communities); - verify(communityService).listAll(pageable); + verify(communityService).listAllInPages(pageable); } @Test