Skip to content

Commit

Permalink
[feat] SoptLog API 추가 (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Nov 22, 2024
1 parent 0214a15 commit a463d50
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/sopt/app/application/poke/PokeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ private PokeHistory createPokeByApplyingReply(
.isAnonymous(isAnonymous)
.build());
}

public Long getUserPokeCount(Long userId) {
return historyRepository.countByPokerIdOrPokedId(userId, userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sopt.app.application.rank;

import java.util.AbstractMap;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -20,4 +21,16 @@ public List<Main> calculateRank() {
.map(user -> Main.of(rankPoint.getAndIncrement(), user))
.toList();
}

public Long getUserRank(Long userId) {
AtomicInteger rankPoint = new AtomicInteger(1);

return Long.valueOf(soptampUserInfos.stream()
.sorted(Comparator.comparing(SoptampUserInfo::getTotalPoints).reversed())
.map(user -> new AbstractMap.SimpleEntry<>(rankPoint.getAndIncrement(), user))
.filter(entry -> entry.getValue().getId().equals(userId))
.map(AbstractMap.SimpleEntry::getKey)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("User not found")));
}
}
19 changes: 16 additions & 3 deletions src/main/java/org/sopt/app/facade/AuthFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import lombok.RequiredArgsConstructor;
import org.sopt.app.application.auth.JwtTokenService;
import org.sopt.app.application.auth.dto.PlaygroundAuthTokenInfo.*;
import org.sopt.app.application.auth.dto.PlaygroundAuthTokenInfo.AppToken;
import org.sopt.app.application.playground.PlaygroundAuthService;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.*;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.LoginInfo;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.PlaygroundMain;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.PlaygroundProfile;
import org.sopt.app.application.poke.PokeService;
import org.sopt.app.application.soptamp.SoptampUserService;
import org.sopt.app.application.user.UserService;
import org.sopt.app.presentation.auth.AppAuthRequest.*;
import org.sopt.app.domain.entity.User;
import org.sopt.app.presentation.auth.AppAuthRequest.AccessTokenRequest;
import org.sopt.app.presentation.auth.AppAuthRequest.CodeRequest;
import org.sopt.app.presentation.auth.AppAuthResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,6 +25,7 @@ public class AuthFacade {
private final UserService userService;
private final PlaygroundAuthService playgroundAuthService;
private final SoptampUserService soptampUserService;
private final PokeService pokeService;

@Transactional
public AppAuthResponse loginWithPlayground(CodeRequest codeRequest) {
Expand Down Expand Up @@ -66,4 +72,11 @@ public AppAuthResponse getRefreshToken(String refreshToken) {
.build();
}

public int getUserSoptLevel(User user) {
return playgroundAuthService.getUserSoptLevel(user);
}

public PlaygroundProfile getUserDetails(User user) {
return playgroundAuthService.getPlayGroundProfile(user.getPlaygroundToken());
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/sopt/app/facade/PokeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,8 @@ public RecommendedFriendsRequest getRecommendedFriendsByTypeList(
public boolean getIsNewUser(Long userId) {
return friendService.getIsNewUser(userId);
}

public Long getUserPokeCount(Long userId) {
return pokeService.getUserPokeCount(userId);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/sopt/app/facade/RankFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public PartRank findPartRank(Part part) {
.filter(partRank -> partRank.getPart().equals(part.getPartName()))
.findFirst().orElseThrow();
}

@Transactional(readOnly = true)
public Long findUserRank(Long userId) {
List<SoptampUserInfo> soptampUserInfos = soptampUserFinder.findAllOfCurrentGeneration();
SoptampUserRankCalculator soptampUserRankCalculator = new SoptampUserRankCalculator(soptampUserInfos);
return soptampUserRankCalculator.getUserRank(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sopt.app.interfaces.postgres;

import jakarta.validation.constraints.NotNull;
import java.util.List;
import org.sopt.app.domain.entity.poke.PokeHistory;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -36,4 +37,6 @@ List<PokeHistory> findAllWithFriendOrderByCreatedAtDescIsReplyFalse(@Param("user
List<PokeHistory> findAllPokeHistoryByUsers(@Param("userId") Long userId, @Param("friendId") Long friendId);

Long countByPokedIdAndIsReplyIsFalse(Long pokedId);

Long countByPokerIdOrPokedId(@NotNull Long pokerId, @NotNull Long pokedId);
}
24 changes: 23 additions & 1 deletion src/main/java/org/sopt/app/presentation/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.PlaygroundProfile;
import org.sopt.app.application.soptamp.SoptampUserService;
import org.sopt.app.domain.entity.User;
import org.sopt.app.facade.AuthFacade;
import org.sopt.app.facade.PokeFacade;
import org.sopt.app.facade.RankFacade;
import org.sopt.app.facade.SoptampFacade;
import org.sopt.app.presentation.user.UserResponse.SoptLog;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/user")
Expand All @@ -28,6 +34,9 @@ public class UserController {

private final SoptampUserService soptampUserService;
private final SoptampFacade soptampFacade;
private final AuthFacade authFacade;
private final PokeFacade pokeFacade;
private final RankFacade rankFacade;

@Operation(summary = "솝탬프 정보 조회")
@ApiResponses({
Expand Down Expand Up @@ -62,4 +71,17 @@ public ResponseEntity<UserResponse.ProfileMessage> editProfileMessage(
return ResponseEntity.ok(response);
}

@Operation(summary = "유저 솝트로그 조회")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@GetMapping(value = "/sopt-log")
public ResponseEntity<UserResponse.SoptLog> getUserSoptLog(@AuthenticationPrincipal User user) {
int soptLevel = authFacade.getUserSoptLevel(user);
Long pokeCount = pokeFacade.getUserPokeCount(user.getId());
Long soptampRank = rankFacade.findUserRank(user.getId());
PlaygroundProfile playgroundProfile = authFacade.getUserDetails(user);
return ResponseEntity.ok(SoptLog.of(soptLevel, pokeCount, soptampRank, playgroundProfile));
}
}

0 comments on commit a463d50

Please sign in to comment.