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

[FEAT] 솝트 로그 API 개발 - #437 #459

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 @@ -35,6 +35,7 @@
import org.sopt.app.common.exception.BadRequestException;
import org.sopt.app.common.exception.UnauthorizedException;
import org.sopt.app.common.response.ErrorCode;
import org.sopt.app.domain.entity.User;
import org.sopt.app.domain.enums.UserStatus;
import org.sopt.app.presentation.auth.AppAuthRequest.AccessTokenRequest;
import org.sopt.app.presentation.auth.AppAuthRequest.CodeRequest;
Expand Down Expand Up @@ -239,4 +240,15 @@ private <T extends PostWithMemberInfo> List<T> getPostsWithMemberInfo(String pla
}
return mutablePosts;
}

public int getUserSoptLevel(User user) {
final Map<String, String> accessToken = createAuthorizationHeaderByUserPlaygroundToken(user.getPlaygroundToken());
return playgroundClient.getPlayGroundUserSoptLevel(accessToken,user.getPlaygroundId()).soptProjectCount();
}

public PlaygroundProfile getPlayGroundProfile(String accessToken) {
Map<String, String> requestHeader = createAuthorizationHeaderByUserPlaygroundToken(accessToken);
return playgroundClient.getPlayGroundProfile(requestHeader);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.sopt.app.application.auth.dto.PlaygroundAuthTokenInfo.RefreshedToken;
import org.sopt.app.application.playground.dto.PlayGroundEmploymentResponse;
import org.sopt.app.application.playground.dto.PlayGroundPostDetailResponse;
import org.sopt.app.application.playground.dto.PlayGroundUserSoptLevelResponse;
import org.sopt.app.application.playground.dto.PlaygroundPostInfo.PlaygroundPostResponse;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.ActiveUserIds;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.OwnPlaygroundProfile;
Expand Down Expand Up @@ -66,4 +67,10 @@ PlayGroundEmploymentResponse getPlaygroundEmploymentPost(@HeaderMap Map<String,
@RequestLine("GET /api/v1/community/posts/{postId}")
PlayGroundPostDetailResponse getPlayGroundPostDetail(@HeaderMap Map<String, String> headers,
@Param Long postId);

@RequestLine("GET /internal/api/v1/members/{memberId}/project")
PlayGroundUserSoptLevelResponse getPlayGroundUserSoptLevel(@HeaderMap Map<String, String> headers, @Param Long memberId);

@RequestLine("GET /api/v1/members/profile/me")
PlaygroundProfile getPlayGroundProfile(@HeaderMap Map<String, String> headers);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.app.application.playground.dto;

public record PlayGroundUserSoptLevelResponse(
Long id,
String profileImage,
int soptProjectCount
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public static class PlaygroundProfile {
private Long memberId;
private String name;
private String profileImage;
private String introduction;
private List<ActivityCardinalInfo> activities;

public ActivityCardinalInfo getLatestActivity() {
Expand Down
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")));
Comment on lines +30 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 전부를 map으로 변환하고 찾는 것이 아닌 totalPoint를 이용해 sort된 유저를 1등부터 확인하며 userId와 일치하는 유저를 찾아 반환하는 것이 더 빠르게 순위를 반환하는 방법일 것 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋은 방법이네요 감사합니다!

}
}
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4. user를 매개변수로 전달하는 것이 아닌 필요한 playgroundToken만 전달하는 것이 좋을 것 같아요

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5. Controller에 비즈니스 로직을 포함하는 것은 @transactional을 사용하기 어려워 Controller에서의 비즈니스 로직 작성을 최대한 피하고 있었는데 기훈님 생각도 궁금합니다!
제 생각에는 전부 다 굳이 facade에 들어가지 않아도 되는 메서드 같아서 차라리 솝트로그를 관리하는 새로운 facade를 만들고 그 facade에 playgroundAuthService, pokeService, RankFacade(RankService로 변환하는게 좋을 것 같네요)를 사용하는 것이 �좋지 않을까라는 생각이 들어요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 제 생각에는 이 코드는 비즈니스 로직보다는, 각계층별로 데이터를 조회하고 매핑하여 반환하는 역할을 수행하기 때문에 비즈니스 로직보다는 , 응답 매핑 역할을 하는 어플리케이션 코드측에 속한다고 생각합니다!
그리고 트랜잭션을 사용함에 있는 부분을 생각해보면 롤백이 일어나는 부분이 필요하지 않다고 생각했고 오히려 새로운 facade를 만든다면 그안에 또다른 서비스를 사용한다면 각 service는 이제 더많은 의존관계가 생긴다고 하여 이렇게 작성했습니다!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠 그렇군요! 기훈님 의견 들어보고 고민해보는 것도 재밌네용

저는 나중에 반환해야 할 api가 변경될 때도 고려를 했어요. 솝탬프는 이전 기수에서도 사라지려고 했었던 피쳐이기도 하고, 새로운 피쳐가 생기면 새로운 피쳐를 솝트로그에 나타내도록 할 수도 있을 것 같아서요
그때마다 controller를 변경하는 것은 맞지 않다고 생각하고 그래서 저는 이것도 비즈니스 로직이라 봤습니다!
따라서 새로운 facade를 이용해 솝트 로그에 대한 책임을 갖는 객체를 하나 더 만든다면 솝트 로그 변경에 있어 다른 객체에 대한 영향이 적을 것으로 판단했어요.
또한 각 service는 어차피 controller에 의존관계가 생기나, facade에 의존 관계가 생기나의 차이라 크게 문제 없을 것으로 판단했습니다!

또한 Transactional(readonly=true)는 롤백 이후에도 변경감지를 위한 스냅샷을 보관하지 않아 메모리상 이점이 있다는 생각을 했는데, 그렇다면 DB 커넥션을 너무 오래 잡고 있는 거려나 생각이 들어서 Transactional에 대해서는 그냥 의견이었습니다!

}
}
50 changes: 49 additions & 1 deletion src/main/java/org/sopt/app/presentation/user/UserResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
Comment on lines +5 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5. 기존에 사용하던 와일드 카드로 변경하는 것이 가독성에 좋을 것 같아요

import org.sopt.app.application.app_service.dto.AppServiceInfo;
import org.sopt.app.application.playground.dto.PlaygroundProfileInfo.PlaygroundProfile;
import org.sopt.app.domain.enums.PlaygroundPart;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserResponse {
Expand Down Expand Up @@ -103,4 +109,46 @@ public static AppService of(final AppServiceInfo appServiceInfo) {
.build();
}
}

@Getter
@Builder
public static class SoptLog {
@Schema(description = "유저 이름", example = "차은우")
private String userName;
@Schema(description = "프로필 이미지 url", example = "www.png")
private String profileImage;
@Schema(description = "파트")
private PlaygroundPart part;
@Schema(description = "콕찌르기 횟수")
private String pokeCount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. 콕 찌르기, 솝활동, 솝탬프 모두 뒤에 ~등, ~회, LV등을 붙이는 것 클라와 상의 후에 모두 붙이거나, 모두 붙이지 않거나 하는 것이 좋을 것 같아요.
또한 현재 API 명세서에 정의되어 있는 값들과 응답값이 많이 다른 것 같아요 API 명세 수정 혹은 응답 값 수정 부탁드립니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 수정했습니다!

@Schema(description = "", example = "14등")
private String soptampRank;
@Schema(description = "솝력 ", example = "LV.7")
private String soptLevel;
@Schema(description = "유저 소개", example = "false")
private String profileMessage;

public static SoptLog of(final String userName, final String profileImage, final PlaygroundPart part, final String pokeCount, final String soptampRank, final String soptLevel, final String profileMessage) {
return SoptLog.builder()
.userName(userName)
.profileImage(profileImage)
.part(part)
.pokeCount(pokeCount)
.soptampRank(soptampRank)
.soptLevel(soptLevel)
.profileMessage(profileMessage)
.build();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4. 매개변수가 많은 생성자는 builder 메서드를 활용하는 것이 더 유용할 것 같습니다.
soptampRank, soptLevel, profileMessage가 모두 String을 사용하므로 혼동의 가능성이 있어 builder로 명확히 표현해주는 것이 좋을 것 같아요
+ 또한 사용되지 않는 메서드라면 삭제하는 것이 좋을 것 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용하지 않는 메소드라 제거했습니다

public static SoptLog of(int soptLevel, Long pokeCount, Long soptampRank, PlaygroundProfile playgroundProfile) {
return SoptLog.builder()
.soptLevel("LV." + soptLevel)
.pokeCount(pokeCount.toString())
.soptampRank(soptampRank.toString())
.userName(playgroundProfile.getName())
.profileImage(playgroundProfile.getProfileImage())
.part(playgroundProfile.getLatestActivity().getPlaygroundPart())
.profileMessage(playgroundProfile.getIntroduction())
.build();
}
}
}
9 changes: 5 additions & 4 deletions src/test/java/org/sopt/app/facade/PokeFacadeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class PokeFacadeTest {
private final UserProfile userProfile3 = UserProfile.builder().userId(3L).name("name3").playgroundId(3L).build();
private final List<UserProfile> userProfileList = List.of(userProfile2, userProfile3);
private final ActivityCardinalInfo activityCardinalInfo = new ActivityCardinalInfo("34,서버");
private final String instruction = "test";
private final List<PlaygroundProfile> playgroundProfileList = List.of(
new PlaygroundProfile(2L, "name2", "image", List.of(activityCardinalInfo)),
new PlaygroundProfile(3L, "name3", "image", List.of(activityCardinalInfo))
new PlaygroundProfile(2L, "name2", "image", instruction,List.of(activityCardinalInfo)),
new PlaygroundProfile(3L, "name3", "image", instruction,List.of(activityCardinalInfo))
);
private final List<PlaygroundProfile> playgroundProfileListWithoutImage = List.of(
new PlaygroundProfile(2L, "name2", "", List.of(activityCardinalInfo)),
new PlaygroundProfile(3L, "name3", "", List.of(activityCardinalInfo))
new PlaygroundProfile(2L, "name2", "", instruction,List.of(activityCardinalInfo)),
new PlaygroundProfile(3L, "name3", "", instruction,List.of(activityCardinalInfo))
);
private final PokeHistory pokeHistory2 = PokeHistory.builder().id(2L).pokedId(1L).pokerId(2L).isReply(false)
.isAnonymous(false).build();
Expand Down
Loading