Skip to content

Commit

Permalink
Merge pull request #169 from Team-HMH/refactor/#122-use-facade-pattern
Browse files Browse the repository at this point in the history
refactor - Facade 패턴 적용 및 리팩토링
  • Loading branch information
kseysh authored Jun 21, 2024
2 parents 2622309 + d712315 commit 8d432d9
Show file tree
Hide file tree
Showing 29 changed files with 323 additions and 279 deletions.
3 changes: 3 additions & 0 deletions src/main/java/sopt/org/hmh/domain/app/domain/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

@Getter
Expand All @@ -14,7 +15,9 @@ public abstract class App {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull(message = "os는 null일 수 없습니다.")
protected String os;

@NotNull(message = "appCode는 null일 수 없습니다.")
protected String appCode;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sopt.org.hmh.domain.app.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -16,6 +17,7 @@ public class ChallengeApp extends App {
@JoinColumn(name = "challenge_id")
private Challenge challenge;

@NotNull(message = "목표 시간은 null일 수 없습니다.")
private Long goalTime;

@Builder
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/sopt/org/hmh/domain/app/domain/HistoryApp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sopt.org.hmh.domain.app.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -16,7 +17,10 @@ public class HistoryApp extends App {
@JoinColumn(name = "daily_challenge_id")
private DailyChallenge dailyChallenge;

@NotNull(message = "사용 시간은 null일 수 없습니다.")
private Long usageTime;

@NotNull(message = "목표 시간은 null일 수 없습니다.")
private Long goalTime;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import jakarta.validation.constraints.NotNull;
import sopt.org.hmh.domain.app.domain.AppConstants;
import sopt.org.hmh.domain.app.domain.ChallengeApp;
import sopt.org.hmh.domain.app.domain.exception.AppError;
import sopt.org.hmh.domain.app.domain.exception.AppException;
import sopt.org.hmh.domain.challenge.domain.Challenge;

public record ChallengeAppRequest(
@NotNull(message = "앱 코드는 null일 수 없습니다.")
Expand All @@ -16,4 +18,13 @@ public record ChallengeAppRequest(
throw new AppException(AppError.INVALID_GOAL_TIME);
}
}

public ChallengeApp toEntity(Challenge challenge , String os) {
return ChallengeApp.builder()
.challenge(challenge)
.appCode(appCode)
.goalTime(goalTime)
.os(os)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sopt.org.hmh.domain.app.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import sopt.org.hmh.domain.app.domain.ChallengeApp;
import sopt.org.hmh.domain.app.domain.exception.AppError;
import sopt.org.hmh.domain.app.domain.exception.AppException;
import sopt.org.hmh.domain.app.dto.request.ChallengeAppRequest;
import sopt.org.hmh.domain.app.repository.ChallengeAppRepository;
import sopt.org.hmh.domain.challenge.domain.Challenge;

@Service
@RequiredArgsConstructor
public class ChallengeAppService {

private final ChallengeAppRepository challengeAppRepository;

public void removeApp(Challenge challenge, String appcode, String os) {
ChallengeApp appToRemove =
challengeAppRepository.findFirstByChallengeIdAndAppCodeAndOsOrElseThrow(challenge.getId(), appcode, os);
challengeAppRepository.delete(appToRemove);
}

public void addApps(Challenge challenge, List<ChallengeAppRequest> requests, String os) {
challengeAppRepository.saveAll(
requests.stream().map(
request -> {
validateAppExist(challenge.getId(), request.appCode(), os);
return request.toEntity(challenge, os);
}).toList());
}

private void validateAppExist(Long challengeId, String appCode, String os) {
if (challengeAppRepository.existsByChallengeIdAndAppCodeAndOs(challengeId, appCode, os)) {
throw new AppException(AppError.APP_EXIST_ALREADY);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.hmh.domain.app.domain.ChallengeApp;
import sopt.org.hmh.domain.app.domain.HistoryApp;
import sopt.org.hmh.domain.app.domain.exception.AppError;
Expand All @@ -14,13 +13,13 @@

@Service
@RequiredArgsConstructor
@Transactional
public class AppService {
public class HistoryAppService {

private final HistoryAppRepository historyAppRepository;

public void addAppForHistory(List<ChallengeApp> currentChallengeApps, List<HistoryAppRequest> apps,
DailyChallenge dailyChallenge, String os) {
public void addHistoryApp(
List<ChallengeApp> currentChallengeApps, List<HistoryAppRequest> apps,
DailyChallenge dailyChallenge, String os) {
historyAppRepository.saveAll(supplementAdditionalInfo(currentChallengeApps, apps, dailyChallenge, os));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sopt.org.hmh.domain.auth.exception.AuthSuccess;
import sopt.org.hmh.domain.auth.dto.request.SocialPlatformRequest;
import sopt.org.hmh.domain.auth.dto.request.SocialSignUpRequest;
import sopt.org.hmh.domain.auth.service.AuthService;
import sopt.org.hmh.domain.auth.service.AuthFacade;
import sopt.org.hmh.global.auth.social.SocialAccessTokenResponse;
import sopt.org.hmh.global.common.response.BaseResponse;

Expand All @@ -22,7 +22,7 @@
@RequestMapping("/api/v1/user")
public class AuthController implements AuthApi {

private final AuthService authService;
private final AuthFacade authFacade;

@PostMapping("/login")
@Override
Expand All @@ -32,7 +32,7 @@ public ResponseEntity<BaseResponse<?>> orderLogin(
) {
return ResponseEntity
.status(AuthSuccess.LOGIN_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.LOGIN_SUCCESS, authService.login(socialAccessToken, request)));
.body(BaseResponse.success(AuthSuccess.LOGIN_SUCCESS, authFacade.login(socialAccessToken, request)));
}

@PostMapping("/signup")
Expand All @@ -44,7 +44,7 @@ public ResponseEntity<BaseResponse<?>> orderSignup(
) {
return ResponseEntity
.status(AuthSuccess.SIGNUP_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.SIGNUP_SUCCESS, authService.signup(socialAccessToken, request, os)));
.body(BaseResponse.success(AuthSuccess.SIGNUP_SUCCESS, authFacade.signup(socialAccessToken, request, os)));
}

@PostMapping("/reissue")
Expand All @@ -54,7 +54,7 @@ public ResponseEntity<BaseResponse<?>> orderReissue(
) {
return ResponseEntity
.status(AuthSuccess.REISSUE_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.REISSUE_SUCCESS, authService.reissueToken(refreshToken)));
.body(BaseResponse.success(AuthSuccess.REISSUE_SUCCESS, authFacade.reissueToken(refreshToken)));
}

@GetMapping("/social/token/kakao")
Expand All @@ -63,6 +63,6 @@ public ResponseEntity<BaseResponse<SocialAccessTokenResponse>> orderGetKakaoAcce
) {
return ResponseEntity
.status(AuthSuccess.GET_SOCIAL_ACCESS_TOKEN_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.GET_SOCIAL_ACCESS_TOKEN_SUCCESS, authService.getSocialAccessTokenByAuthorizationCode(code)));
.body(BaseResponse.success(AuthSuccess.GET_SOCIAL_ACCESS_TOKEN_SUCCESS, authFacade.getSocialAccessTokenByAuthorizationCode(code)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import sopt.org.hmh.domain.challenge.dto.request.ChallengeRequest;
import sopt.org.hmh.domain.challenge.dto.request.ChallengeSignUpRequest;
import sopt.org.hmh.domain.user.domain.OnboardingInfo;
import sopt.org.hmh.domain.user.domain.OnboardingProblem;
import sopt.org.hmh.global.auth.social.SocialPlatform;

public record SocialSignUpRequest(
Expand All @@ -17,7 +20,24 @@ public record SocialSignUpRequest(
@JsonProperty(value = "challenge")
ChallengeSignUpRequest challengeSignUpRequest
) {
public ChallengeRequest toChallengeRequest() {
return new ChallengeRequest(challengeSignUpRequest.period(), challengeSignUpRequest.goalTime());
}

public ChallengeRequest toChallengeRequest() {
return new ChallengeRequest(challengeSignUpRequest.period(), challengeSignUpRequest.goalTime());
}

public OnboardingInfo toOnboardingInfo(Long userId) {
return OnboardingInfo.builder()
.averageUseTime(onboardingRequest.averageUseTime())
.userId(userId)
.build();
}

public List<OnboardingProblem> toProblemList(Long onboardingInfoId) {
return onboardingRequest.problemList().stream()
.map(problem -> OnboardingProblem.builder()
.onboardingInfoId(onboardingInfoId)
.problem(problem)
.build()
).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package sopt.org.hmh.domain.auth.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import sopt.org.hmh.domain.user.domain.User;
import sopt.org.hmh.global.auth.jwt.TokenResponse;

public record LoginResponse(
Long userId,
@JsonProperty(value = "token")
TokenResponse tokenResponse
){

public static LoginResponse of(User loginUser, TokenResponse tokenResponse) {
return new LoginResponse(
loginUser.getId(),
tokenResponse
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.hmh.domain.app.service.ChallengeAppService;
import sopt.org.hmh.domain.auth.dto.response.ReissueResponse;
import sopt.org.hmh.domain.challenge.domain.Challenge;
import sopt.org.hmh.domain.challenge.service.ChallengeService;
import sopt.org.hmh.domain.challenge.service.ChallengeFacade;
import sopt.org.hmh.domain.user.domain.User;
import sopt.org.hmh.domain.auth.dto.request.SocialPlatformRequest;
import sopt.org.hmh.domain.auth.dto.request.SocialSignUpRequest;
Expand All @@ -21,41 +22,36 @@

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AuthService {
public class AuthFacade {

private final KakaoLoginService kakaoLoginService;
private final AppleOAuthProvider appleOAuthProvider;

private final ChallengeService challengeService;
private final ChallengeFacade challengeFacade;
private final ChallengeAppService challengeAppService;
private final TokenService tokenService;
private final UserService userService;

@Transactional
@Transactional(readOnly = true)
public LoginResponse login(String socialAccessToken, SocialPlatformRequest request) {

SocialPlatform socialPlatform = request.socialPlatform();
String socialId = getSocialIdBySocialAccessToken(socialPlatform, socialAccessToken);

String socialId = this.getSocialIdBySocialAccessToken(socialPlatform, socialAccessToken);
User loginUser = userService.getUserBySocialPlatformAndSocialId(socialPlatform, socialId);

return performLogin(socialAccessToken, socialPlatform, loginUser);
}

@Transactional
public LoginResponse signup(String socialAccessToken, SocialSignUpRequest request, String os) {

SocialPlatform socialPlatform = request.socialPlatform();
String socialId = getSocialIdBySocialAccessToken(socialPlatform, socialAccessToken);
String socialId = this.getSocialIdBySocialAccessToken(socialPlatform, socialAccessToken);

userService.validateDuplicateUser(socialId, socialPlatform);

User user = userService.addUser(socialPlatform, socialId, request.name());
Long userId = user.getId();
userService.registerOnboardingInfo(request, userId);

Challenge challenge = challengeService.addChallenge(user.getId(), request.toChallengeRequest() , os);
challengeService.addApps(challenge, request.challengeSignUpRequest().apps(), os);

userService.registerOnboardingInfo(request);
Challenge challenge = challengeFacade.addChallenge(userId, request.toChallengeRequest() , os);
challengeAppService.addApps(challenge, request.challengeSignUpRequest().apps(), os);

return performLogin(socialAccessToken, socialPlatform, user);
}
Expand All @@ -72,16 +68,15 @@ public ReissueResponse reissueToken(String refreshToken) {
return tokenService.reissueToken(refreshToken);
}


private LoginResponse performLogin(String socialAccessToken, SocialPlatform socialPlatform, User loginUser) {
if (socialPlatform == SocialPlatform.KAKAO) {
kakaoLoginService.updateUserInfoByKakao(loginUser, socialAccessToken);
}
return LoginResponse.of(loginUser, tokenService.issueToken(loginUser.getId()));
Long userId = loginUser.getId();
return new LoginResponse(userId, tokenService.issueToken(userId));
}

public SocialAccessTokenResponse getSocialAccessTokenByAuthorizationCode(String code) {
return kakaoLoginService.getKakaoAccessToken(code);
}

}
Loading

0 comments on commit 8d432d9

Please sign in to comment.