Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/#104 #105

Merged
merged 6 commits into from
Oct 7, 2024
Merged
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
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
Loading