Skip to content

Commit

Permalink
Merge pull request #105 from KNU-HAEDAL/issue/#104
Browse files Browse the repository at this point in the history
Issue/#104
  • Loading branch information
momnpa333 authored Oct 7, 2024
2 parents 4daed80 + 5290a36 commit df00519
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
@RequiredArgsConstructor
@RestController
public class UserController {

private final UserService userService;

@Operation(summary = "내 정보 조회", description = "내 정보를 조회한다.")
@GetMapping("/api/user")
public ApiResponse<UserRes.UserMyInfo> getUserInfo(
@AuthenticationPrincipal JwtUser jwtUser
@AuthenticationPrincipal JwtUser jwtUser
) {
var userModel = userService.getUserModel(jwtUser.getId());
return ApiResponse.success(UserRes.UserMyInfo.from(userModel));
Expand All @@ -31,8 +32,8 @@ public ApiResponse<UserRes.UserMyInfo> getUserInfo(
@Operation(summary = "내 정보 수정", description = "내 정보를 수정한다.")
@PutMapping("/api/user")
public ApiResponse<Void> updateUser(
@Valid @RequestBody UserReq.Update request,
@AuthenticationPrincipal JwtUser jwtUser
@Valid @RequestBody UserReq.Update request,
@AuthenticationPrincipal JwtUser jwtUser
) {
var command = request.toCommand();
userService.updateUser(jwtUser.getId(), command);
Expand All @@ -42,29 +43,39 @@ public ApiResponse<Void> updateUser(
@Operation(summary = "스트릭 조회", description = "스트릭을 조회한다.")
@GetMapping("/api/user/streak")
public ApiResponse<UserRes.Streak> getStreak(
@AuthenticationPrincipal JwtUser jwtUser,
UserReq.GetStreak request
@AuthenticationPrincipal JwtUser jwtUser,
UserReq.GetStreak request
) {
if(request.startDate().isAfter(request.endDate())){
if (request.startDate().isAfter(request.endDate())) {
throw new IllegalArgumentException("시작일은 종료일보다 이전이어야 합니다.");
}
var userModelStreak = userService.getUserStreak(
jwtUser.getId(),
request.startDate(),
request.endDate()
jwtUser.getId(),
request.startDate(),
request.endDate()
);
return ApiResponse.success(UserRes.Streak.from(userModelStreak));
}

@Operation(summary = "유저 랭킹 페이징", description = "전체 유저 랭킹을 조회 페이징")
@GetMapping("/api/users/ranking")
public ApiResponse<PagingResponse<UserRes.User>> getUsersRanking(
@Valid PagingRequest request
@Valid PagingRequest request
) {
var userModelPage = userService.getUserPagingByRanking(request.toPageable());
var response = PagingResponse.from(userModelPage, UserRes.User::from);
return ApiResponse.success(response);
}

@Operation(summary = "내 랭킹 조회", description = "유저 랭킹을 조회한다.")
@GetMapping("/api/users/my-ranking")
public ApiResponse<UserRes.MyRankingInfo> getMyRanking(
@AuthenticationPrincipal JwtUser jwtUser
) {
var userModelRanking = userService.getMyRanking(jwtUser.getId());
var response = UserRes.MyRankingInfo.from(userModelRanking);
return ApiResponse.success(response);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,114 @@
import java.util.List;

public class UserRes {

@Builder
public record UserMyInfo(
Long id,
String nickname,
String profileImageUrl,
String email,
TierInfo tierInfo,
Role role
Long id,
String nickname,
String profileImageUrl,
String email,
TierInfo tierInfo,
Role role
) {

public static UserMyInfo from(UserModel.Main userMain) {
var tierInfo = TierInfo.from(userMain.exp());
return UserMyInfo.builder()
.id(userMain.id())
.nickname(userMain.nickname())
.profileImageUrl(userMain.profileImageUrl())
.email(userMain.email())
.tierInfo(tierInfo)
.role(userMain.role())
.build();
.id(userMain.id())
.nickname(userMain.nickname())
.profileImageUrl(userMain.profileImageUrl())
.email(userMain.email())
.tierInfo(tierInfo)
.role(userMain.role())
.build();
}
}

@Builder
public record User(
Long id,
String nickname,
String profileImageUrl,
TierInfo tierInfo
Long id,
String nickname,
String profileImageUrl,
TierInfo tierInfo
) {

public static User from(UserModel.Main userMain) {
var tierInfo = TierInfo.from(userMain.exp());
return User.builder()
.id(userMain.id())
.nickname(userMain.nickname())
.profileImageUrl(userMain.profileImageUrl())
.tierInfo(tierInfo)
.build();
.id(userMain.id())
.nickname(userMain.nickname())
.profileImageUrl(userMain.profileImageUrl())
.tierInfo(tierInfo)
.build();
}
}

@Builder
public record TierInfo(
String tier,
Integer totalExp,
Integer currentExp
String tier,
Integer totalExp,
Integer currentExp
) {

public static TierInfo from(Integer exp) {
var tier = TierSystem.getTier(exp);

return TierInfo.builder()
.tier(tier.getKorean())
.totalExp(tier.getEndExp() - tier.getStartExp()) // 티어 시작 경험치부터 끝 경험치까지
.currentExp(exp - tier.getStartExp()) // 현재 경험치 - 티어 시작 경험치
.build();
.tier(tier.getKorean())
.totalExp(tier.getEndExp() - tier.getStartExp()) // 티어 시작 경험치부터 끝 경험치까지
.currentExp(exp - tier.getStartExp()) // 현재 경험치 - 티어 시작 경험치
.build();
}
}

@Builder
public record Streak(
/** 여기서 Model의 DayCount를 사용해도 되는지 */
List<UserModel.DayCount> dayCounts
/** 여기서 Model의 DayCount를 사용해도 되는지 */
List<UserModel.DayCount> dayCounts
) {
public static Streak from(UserModel.Streak streak){

public static Streak from(UserModel.Streak streak) {
return Streak.builder()
.dayCounts(streak.dayCounts())
.build();
.dayCounts(streak.dayCounts())
.build();
}
}

/**
* 하루에 대한 스트릭 카운트
* 0인 것도 보낸다.
* 하루에 대한 스트릭 카운트 0인 것도 보낸다.
*/
@Builder
public record DayCount(
LocalDate date,
Integer count
LocalDate date,
Integer count
) {

}

@Builder
public record MyRankingInfo(
Long id,
String nickname,
String profileImageUrl,
String email,
TierInfo tierInfo,
Role role,
Integer rank
) {

public static MyRankingInfo from(UserModel.MyRanking myRanking) {
var tierInfo = TierInfo.from(myRanking.exp());
return MyRankingInfo.builder()
.id(myRanking.id())
.nickname(myRanking.nickname())
.profileImageUrl(myRanking.profileImageUrl())
.email(myRanking.email())
.tierInfo(tierInfo)
.rank(myRanking.rank())
.role(myRanking.role())
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,46 @@
import java.util.stream.Collectors;


public class UserModel{
public class UserModel {

@Builder
public record Main(
Long id,
String email,
String nickname,
String profileImageUrl,
Integer exp,
Role role,
LocalDateTime createdAt
Long id,
String email,
String nickname,
String profileImageUrl,
Integer exp,
Role role,
LocalDateTime createdAt
) {

public static Main from(User user) {
return Main.builder()
.id(user.getId())
.email(user.getEmail())
.nickname(user.getNickname())
.profileImageUrl(user.getProfileImageUrl())
.exp(user.getExp())
.role(user.getRole())
.createdAt(user.getCreatedAt())
.build();
.id(user.getId())
.email(user.getEmail())
.nickname(user.getNickname())
.profileImageUrl(user.getProfileImageUrl())
.exp(user.getExp())
.role(user.getRole())
.createdAt(user.getCreatedAt())
.build();
}
}

@Builder
public record Streak(
List<DayCount> dayCounts
List<DayCount> dayCounts
) {
/** 여기서 count 0인걸 포함하는 기능을 쓰는게 맞는지 */
public static Streak from(Map<LocalDate, Integer> streakCounts, LocalDate startDate, LocalDate endDate) {

/**
* 여기서 count 0인걸 포함하는 기능을 쓰는게 맞는지
*/
public static Streak from(Map<LocalDate, Integer> streakCounts, LocalDate startDate,
LocalDate endDate) {
List<DayCount> resultList = startDate.datesUntil(endDate.plusDays(1))
// 날짜가 존재하면 map(date)로 count를 가져오고, 없으면 0을 저장한다.
.map(date -> new DayCount(date, streakCounts.getOrDefault(date, 0)))
.collect(Collectors.toList());
// 날짜가 존재하면 map(date)로 count를 가져오고, 없으면 0을 저장한다.
.map(date -> new DayCount(date, streakCounts.getOrDefault(date, 0)))
.collect(Collectors.toList());
return new Streak(resultList);
}

Expand All @@ -55,25 +61,51 @@ public String toString() {
} else {
for (int i = 0; i < dayCounts.size(); i++) {
sb.append(dayCounts.get(i).toString());
if (i < dayCounts.size() - 1)
if (i < dayCounts.size() - 1) {
sb.append("\n");
}
}
}
return sb.toString();
}

}

@Builder
public record DayCount(
LocalDate date,
Integer count
LocalDate date,
Integer count
) {

@Override
public String toString() {
return "DayCount{" +
"date=" + date +
", count=" + count + '}';
"date=" + date +
", count=" + count + '}';
}
}

@Builder
public record MyRanking(
Long id,
String email,
String nickname,
String profileImageUrl,
Role role,
Integer exp,
Integer rank
) {

public static MyRanking from(User user, Integer rank) {
return MyRanking.builder()
.id(user.getId())
.email(user.getEmail())
.nickname(user.getNickname())
.profileImageUrl(user.getProfileImageUrl())
.role(user.getRole())
.exp(user.getExp())
.rank(rank)
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@RequiredArgsConstructor
@Service
public class UserService {

private final UserReader userReader;
private final UserChallengeReader userChallengeReader;

Expand All @@ -30,8 +31,8 @@ public UserModel.Main getUserModel(Long id) {
public List<UserModel.Main> getManagerAndAdmin() {
List<User> users = userReader.getManagerAndAdmin();
return users.stream()
.map(UserModel.Main::from)
.toList();
.map(UserModel.Main::from)
.toList();
}

/**
Expand All @@ -46,15 +47,23 @@ public void updateUser(Long id, UserCommand.Update userUpdate) {

@Transactional(readOnly = true)
public Page<UserModel.Main> getUserPagingByRanking(Pageable pageable) {
Page<User> users = userReader.getUserPagingByRanking(pageable);
Page<User> users = userReader.getUserPagingByRanking(pageable);
return users.map(UserModel.Main::from);
}

@Transactional(readOnly = true)
public UserModel.Streak getUserStreak(Long id, LocalDate startDate, LocalDate endDate){
List<DayCountType> userStreaks = userChallengeReader.countAllByUserIdAndDate(id, startDate, endDate);
public UserModel.Streak getUserStreak(Long id, LocalDate startDate, LocalDate endDate) {
List<DayCountType> userStreaks = userChallengeReader.countAllByUserIdAndDate(id, startDate,
endDate);
Map<LocalDate, Integer> map = userStreaks.stream()
.collect(Collectors.toMap(DayCountType::getDate, DayCountType::getCount));
.collect(Collectors.toMap(DayCountType::getDate, DayCountType::getCount));
return UserModel.Streak.from(map, startDate, endDate);
}

@Transactional(readOnly = true)
public UserModel.MyRanking getMyRanking(Long id) {
User user = userReader.getById(id);
Integer rank = userReader.getRanking(user.getExp());
return UserModel.MyRanking.from(user, rank);
}
}
Loading

0 comments on commit df00519

Please sign in to comment.