Skip to content

Commit

Permalink
Merge pull request #212 from capstone-kw-jjiggle/feat/#209-apiIssue
Browse files Browse the repository at this point in the history
[fix] #209 - apiIssue관련 정리 네이밍 관련 정리
  • Loading branch information
khyojun authored Oct 15, 2024
2 parents 09ea248 + 4806b27 commit 4e505b8
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 100 deletions.
16 changes: 8 additions & 8 deletions src/main/java/gitbal/backend/api/auth/dto/UserInfoDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
@Getter
public class UserInfoDto {

private final String userName;
private final String username;
private final String univName;
private final String regionName;
private final String imgUrl;
private final String profileImg;
private final String title;
private final Integer rank;
private final Integer userRank;
private final Grade grade;

public UserInfoDto(String userName, String univName, String regionName, String imgUrl,
String title, Integer rank, Grade grade) {
this.userName = userName;
public UserInfoDto(String username, String univName, String regionName, String profileImg,
String title, Integer userRank, Grade grade) {
this.username = username;
this.univName = univName;
this.regionName = regionName;
this.imgUrl = imgUrl;
this.profileImg = profileImg;
this.title = title;
this.rank = rank;
this.userRank = userRank;
this.grade=grade;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gitbal.backend.api.mainPage.dto;


import gitbal.backend.global.constant.Grade;

public record MainPageUserDto(String username, long userscore, int rank) {
public record MainPageUserDto(String username, String profileImg, Long userscore, int userRank) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private MainPageUserResponseDto getSearchedUserList(String searchedname, int pag

private List<MainPageUserDto> convertToMainPageUserDto(Page<User> users) {
return users.stream().map(
(u) -> new MainPageUserDto(u.getNickname(), u.getScore(),
(u) -> new MainPageUserDto(u.getNickname(), u.getProfile_img(), u.getScore(),
u.getUserRank())
).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gitbal.backend.api.regionPage.dto;


public record RegionListDto(String regionlName, Long regionScore) {
public record RegionListDto(String regionlName, Long regionScore, int regionRank) {

public static RegionListDto of(String regionName, Long regionScore) {
return new RegionListDto(regionName, regionScore);
public static RegionListDto of(String regionName, Long regionScore, int regionRank) {
return new RegionListDto(regionName, regionScore, regionRank);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package gitbal.backend.api.regionPage.dto;

public record UserInfoByRegion(String nickname, Long score ) {
public record UserInfoByRegion(String username, String profileImg, Long userscore, int userRank) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import gitbal.backend.api.regionPage.dto.RegionListPageResponseDto;
import gitbal.backend.api.regionPage.dto.UserInfoByRegion;
import gitbal.backend.api.regionPage.dto.UserPageListByRegionResponseDto;
import gitbal.backend.api.schoolPage.dto.UserInfoBySchool;
import gitbal.backend.domain.region.Region;
import gitbal.backend.domain.region.application.repository.RegionRepository;
import gitbal.backend.domain.user.User;
Expand All @@ -16,11 +15,13 @@
import gitbal.backend.domain.user.UserRepository;
import gitbal.backend.global.exception.PageOutOfRangeException;
import gitbal.backend.global.exception.RegionRankPageUserInfoByRegionException;
import gitbal.backend.global.exception.SchoolRankPageUserInfoBySchoolException;
import gitbal.backend.global.security.CustomUserDetails;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -31,8 +32,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//TODO : 이후에 regionChangeScore 반영해야함!


@Service
@RequiredArgsConstructor
Expand All @@ -48,23 +47,31 @@ public ResponseEntity<RegionListPageResponseDto<RegionListDto>> getRegionList()
Pageable pageable = PageRequest.of(page - 1, 10, sort);
Page<Region> regionPage = regionRepository.findAll(pageable);

List<RegionListDto> regionDtoList = regionPage.stream()
.map(this::convertToDto)
.collect(Collectors.toList());
List<Region> sortedRegion = regionPage.stream()
.sorted(Comparator.comparing(Region::getScore).reversed())
.toList();


List<RegionListDto> regionListDtos = convertListToDto(sortedRegion);

// 3. PageResponseDto 생성
RegionListPageResponseDto<RegionListDto> RegionList = RegionListPageResponseDto.<RegionListDto>withALl()
.regionList(regionDtoList)
.build();
.regionList(regionListDtos)
.build();

return ResponseEntity.ok(RegionList);
}

private RegionListDto convertToDto(Region region) {
return new RegionListDto(
region.getRegionName(),
region.getScore()
);
private List<RegionListDto> convertListToDto(List<Region> regions) {
List<RegionListDto> regionListDtos = new ArrayList<>();
for(int index = 0; index < regions.size(); index++) {
Region region = regions.get(index);
regionListDtos.add(new RegionListDto(
region.getRegionName(),
region.getScore(),
index+1
));
}
return regionListDtos;
}

@Transactional(readOnly = true)
Expand All @@ -75,19 +82,19 @@ public MyRegionInfoResponseDto getMyRegionInfo(Authentication authentication) {
CustomUserDetails principal = (CustomUserDetails) authentication.getPrincipal();
String username = principal.getNickname();
User user = userRepository.findByNickname(username).orElseThrow(
NotFoundUserException::new
NotFoundUserException::new
);
Region region = user.getRegion();
if(Objects.isNull(region)) return MyRegionInfoResponseDto.of(0, null);
if (Objects.isNull(region)) return MyRegionInfoResponseDto.of(0, null);
return MyRegionInfoResponseDto.of(findRegionRank(region.getRegionName()), region);
}

//TODO: 이후에 school, region 관련하여서 더 생각해보기
private int findRegionRank(String regionName){
private int findRegionRank(String regionName) {
List<Region> regions = regionRepository.findAll(Sort.by("score").descending());
for (int i = 0; i < regions.size(); i++) {
Region region = regions.get(i);
if(region.getRegionName().equals(regionName)) return i+1;
if (region.getRegionName().equals(regionName)) return i + 1;
}
throw new NotFoundRegionException();
}
Expand All @@ -99,50 +106,55 @@ public UserPageListByRegionResponseDto getUserListByRegionName(int page, String
Region region = regionRepository.findByRegionName(regionName).orElseThrow(NotFoundRegionException::new);
Pageable pageable = initpageable(page, "score");
Page<User> userByRegionName = userRepository.findUserByRegion_RegionName(regionName,
pageable);
pageable);
if (userByRegionName.getTotalPages() < page)
throw new PageOutOfRangeException();
List<UserInfoByRegion> userInfoByRegions = convertPageByUserInfoByRegion(
userByRegionName);
userByRegionName);
return buildUserPageListByRegionResponseDto(page, userInfoByRegions, userByRegionName);
}catch (Exception e){
if(Objects.isNull(e.getMessage()))
} catch (Exception e) {
if (Objects.isNull(e.getMessage()))
throw new RegionRankPageUserInfoByRegionException("지역 랭킹 페이지 유저 정보 조회 중 오류가 발생했습니다.");
throw new RegionRankPageUserInfoByRegionException(e.getMessage());
}
}



private List<UserInfoByRegion> convertPageByUserInfoByRegion(Page<User> userBySchoolName) {
return userBySchoolName.stream().
map(this::convertToUserInfoByRegion)
.toList();
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));
}

return userInfoByRegions;
}


private UserInfoByRegion convertToUserInfoByRegion(User user){
private UserInfoByRegion convertToUserInfoByRegion(User user, int rank) {
return new UserInfoByRegion(
user.getNickname(),
user.getScore()
user.getNickname(),
user.getProfile_img(),
user.getScore(),
rank
);
}



private Pageable initpageable(int page, String sortProperties) {
Sort sort = Sort.by(sortProperties).descending();
return PageRequest.of(page - 1, PAGE_SIZE, sort);
}


private UserPageListByRegionResponseDto buildUserPageListByRegionResponseDto(int page,
List<UserInfoByRegion> userInfoByRegions, Page<User> userBySchoolName) {
List<UserInfoByRegion> userInfoByRegions, Page<User> userBySchoolName) {
return UserPageListByRegionResponseDto.withAll()
.userInfoByRegion(userInfoByRegions)
.page(page)
.total(userBySchoolName.getTotalElements())
.build();
.userInfoByRegion(userInfoByRegions)
.page(page)
.total(userBySchoolName.getTotalElements())
.build();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package gitbal.backend.api.schoolPage.dto;

public record UserInfoBySchool(String nickname, Long score ) {
public record UserInfoBySchool(String username, String profileImg, Long userscore, int userRank) {

}
Loading

0 comments on commit 4e505b8

Please sign in to comment.