Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<!-- Thanks for taking the time to write this Pull Request ❀️ -->

## πŸš€ Description

Included PageInfo object as an instance variable in the GetCommunityDetailsResponse component. I also included a
listAllWithPages(Pageable pageable) method which returns Page<Community> object in the CommunityService interface, and implemented in the
CommunitySDJpaService. This method was used in the listAllCommunities method in the CommunityController class.
<!-- Describe your changes in detail -->

## πŸ“„ 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

<!-- Why is this change required? What problem does it solve? -->
<!-- If it fixes an open issue, please link to the issue here. -->

## πŸ§ͺ How Has This Been Tested?
Mockito unit test and postman test

<!-- Please describe in detail how you tested your changes. -->
<!-- Include details of your testing environment, tests ran to see how -->
Expand All @@ -23,7 +32,7 @@

<!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] 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)

Expand All @@ -32,6 +41,6 @@
<!-- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] 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.
2 changes: 2 additions & 0 deletions api/src/main/resources/public/swagger/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,8 @@ components:
uniqueItems: true
items:
$ref: '#/components/schemas/GetCommunityDetailsResponseCommunity'
page_info:
$ref: '#/components/schemas/PageInfo'
GetCommunityDetailsResponseCommunity:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,17 +73,21 @@ public ResponseEntity<CreateCommunityResponse> createCommunity(@Valid @RequestBo
return ResponseEntity.status(HttpStatus.CREATED).body(createdCommunityResponse);
}


@Override
public ResponseEntity<GetCommunityDetailsResponse> listAllCommunity(
@PageableDefault(size = 200) Pageable pageable) {
log.trace("Received request to list all community");

Set<Community> communityDetails = communityService.listAll(pageable);
Page<Community> communityDetails = communityService.listAllInPages(pageable);
Set<GetCommunityDetailsResponseCommunity> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,6 +34,8 @@ public interface CommunityService {

Set<Community> listAll(Pageable pageable);

Page<Community> listAllInPages(Pageable pageable);

Optional<Community> getCommunityDetailsById(String communityId);

Optional<List<CommunityHouse>> findCommunityHousesById(String communityId, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +79,11 @@ public Set<Community> listAll(Pageable pageable) {
return communityListSet;
}

@Override
public Page<Community> listAllInPages(Pageable pageable) {
return communityRepository.findAll(pageable);
}

@Override public Set<Community> listAll() {
Set<Community> communities = new HashSet<>();
communityRepository.findAll().forEach(communities::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,6 +165,7 @@ void shouldListAllCommunitiesSuccessfully() {
Community community = createTestCommunity();
communities.add(community);


Set<GetCommunityDetailsResponseCommunity> communityDetailsResponse
= new HashSet<>();
communityDetailsResponse.add(
Expand All @@ -170,14 +175,26 @@ void shouldListAllCommunitiesSuccessfully() {
.district(COMMUNITY_DISTRICT)
);

List<Community> communityList = new ArrayList<>();
communityList.add(community);

Page<Community> 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<GetCommunityDetailsResponse> responseEntity =
Expand All @@ -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
Expand Down