Skip to content

Commit

Permalink
hotfix : region, school pagination 및 무한 스크롤 정상 동작하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
khyojun committed Nov 10, 2024
1 parent 7947f6c commit 84fe06a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class UserPageListByRegionResponseDto {
private List<UserInfoByRegion> userInfoByRegion;
private int currentPage;
Expand All @@ -26,11 +28,13 @@ public UserPageListByRegionResponseDto(List<UserInfoByRegion> userInfoByRegion,
return;
}

int pageSize = 10;
log.info("totalCount : {}", totalCount);

int pageSize = 14;
this.totalPages = (int) Math.ceil((double) totalCount / pageSize);

this.end = (int) (Math.ceil(page / 10.0)) * 10;
this.start = end - 9;
this.end = (int) (Math.ceil(page / 14.0)) * 14;
this.start = end - 13;

int last = (int) (Math.ceil(totalCount / (double) pageSize));
end = end > last ? last : end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Objects;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -35,6 +36,7 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class RegionRankService {

private static final int PAGE_SIZE = 14;
Expand Down Expand Up @@ -107,10 +109,11 @@ public UserPageListByRegionResponseDto getUserListByRegionName(int page, String
Pageable pageable = initpageable(page, "score");
Page<User> userByRegionName = userRepository.findUserByRegion_RegionName(regionName,
pageable);
log.info("userByRegionName: {}", userByRegionName.getTotalPages());
if (userByRegionName.getTotalPages() < page)
throw new PageOutOfRangeException();
List<UserInfoByRegion> userInfoByRegions = convertPageByUserInfoByRegion(
userByRegionName);
userByRegionName, page);
return buildUserPageListByRegionResponseDto(page, userInfoByRegions, userByRegionName);
} catch (Exception e) {
if (Objects.isNull(e.getMessage()))
Expand All @@ -120,12 +123,13 @@ public UserPageListByRegionResponseDto getUserListByRegionName(int page, String
}


private List<UserInfoByRegion> convertPageByUserInfoByRegion(Page<User> userBySchoolName) {
private List<UserInfoByRegion> convertPageByUserInfoByRegion(Page<User> userBySchoolName, int page) {
List<User> users = userBySchoolName.get().toList();
List<UserInfoByRegion> userInfoByRegions= new ArrayList<>();
for(int index=0; index<users.size(); index++){
User user = users.get(index);
userInfoByRegions.add(convertToUserInfoByRegion(user, index+1));
int startNum = (page-1) * PAGE_SIZE +1;
for(int index=startNum; index<users.size()+startNum; index++){
User user = users.get(index-startNum);
userInfoByRegions.add(convertToUserInfoByRegion(user, index));
}

return userInfoByRegions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public UserPageListBySchoolResponseDto(List<UserInfoBySchool> userInfoBySchools,
return;
}

int pageSize = 10;
int pageSize = 14;
this.totalPages = (int) Math.ceil((double) totalCount / pageSize);

this.end = (int) (Math.ceil(page / 10.0)) * 10;
this.start = end - 9;
this.end = (int) (Math.ceil(page / 14.0)) * 14;
this.start = end - 13;

int last = (int) (Math.ceil(totalCount / (double) pageSize));
end = end > last ? last : end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public UserPageListBySchoolResponseDto getUserListBySchoolName(int page, String
throw new PageOutOfRangeException();

List<UserInfoBySchool> userInfoBySchools = convertPageByUserInfoBySchool(
userBySchoolName);
userBySchoolName, page);
log.info("userBySchoolName : {}",userInfoBySchools);

return buildUserPageListBySchoolResponseDto(page, userInfoBySchools, userBySchoolName);
Expand All @@ -141,12 +141,13 @@ private boolean isPresentSchoolName(String schoolName) {
return schoolRepository.findBySchoolName(schoolName).isPresent();
}

private List<UserInfoBySchool> convertPageByUserInfoBySchool(Page<User> userBySchoolName) {
private List<UserInfoBySchool> convertPageByUserInfoBySchool(Page<User> userBySchoolName, int page) {
List<User> users = userBySchoolName.get().toList();
List<UserInfoBySchool> userInfoBySchools = new ArrayList<>();
for (int index = 0; index < users.size(); index++) {
User user = users.get(index);
UserInfoBySchool userInfo = convertToUserInfoBySchool(user, index+1);
int startNum = (page-1) * PAGE_SIZE +1;
for (int index = startNum; index < users.size()+startNum; index++) {
User user = users.get(index-startNum);
UserInfoBySchool userInfo = convertToUserInfoBySchool(user, index);
userInfoBySchools.add(userInfo);
}
return userInfoBySchools;
Expand Down

0 comments on commit 84fe06a

Please sign in to comment.