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

refactor - Facade 패턴 적용 및 리팩토링 #169

Merged
merged 38 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d80fd58
refactor - #122 Request to Entity의 메서드로 코드 가독성 향상
kseysh Jun 18, 2024
6fc7780
refactor - #122 User domain에 제약조건 추가
kseysh Jun 18, 2024
9542602
refactor - #122 User domain 제약조건 변경
kseysh Jun 18, 2024
18939dd
refactor - #122 AppService -> HistoryAppService로 네이밍 변경
kseysh Jun 18, 2024
9c71768
refactor - #122 ChallengeAppService 추가
kseysh Jun 18, 2024
840ccae
refactor - #122 ChallengeFacade ChallengeService 분리
kseysh Jun 18, 2024
373e0be
refactor - #122 toEntity 메서드를 이용하여 가독성 향상
kseysh Jun 18, 2024
47e2a69
refactor - #122 addHistoryApp 네이밍 변경
kseysh Jun 18, 2024
0a83bd1
refactor - #122 this 추가로 인한 가독성 향상
kseysh Jun 18, 2024
f5bd894
refactor - #122 facade에서 facade를 참조하지 않도록 변경
kseysh Jun 18, 2024
4fc7307
refactor - #122 controller가 하나의 class를 바라보도록 변경
kseysh Jun 18, 2024
2d770bc
refactor - #122 toEntity 메서드를 이용하여 가독성 향상
kseysh Jun 18, 2024
376a540
refactor - #122 userService 트랜잭션 관리
kseysh Jun 18, 2024
b720f6e
refactor - #122 PointFacade 트랜잭션 관리
kseysh Jun 18, 2024
0aeb0fb
refactor - #122 DailyChallengeController 트랜잭션 관리
kseysh Jun 18, 2024
49808af
refactor - #122 ChallengeController 트랜잭션 관리
kseysh Jun 18, 2024
1a1d46a
refactor - #122 ExpiredUserDeleteScheduler 트랜잭션 관리
kseysh Jun 18, 2024
bd6a674
refactor - #122 AuthService에서 불필요한 Transactional 제거
kseysh Jun 18, 2024
e509d25
refactor - #122 AppService에서 불필요한 Transactional 제거
kseysh Jun 18, 2024
a7accad
refactor - #122 불필요한 of 메서드 삭제
kseysh Jun 18, 2024
29ed909
refactor - #122 Domain sql 내부 nullable 여부 명시
kseysh Jun 18, 2024
4893720
refactor - #122 Domain 내부 @NotNull 제약조건으로 인한 validation 적용
kseysh Jun 18, 2024
4354389
refactor - #122 nickname 변경 메서드 이름 변경
kseysh Jun 18, 2024
89edffc
refactor - #122 사용하지 않는 import 제거
kseysh Jun 18, 2024
72deb6b
refactor - #122 불필요한 코드 제거
kseysh Jun 18, 2024
d6f60aa
refactor - #122 불필요한 변수 제거
kseysh Jun 18, 2024
56df303
refactor - #122 사용하지 않는 import 제거
kseysh Jun 18, 2024
08af31c
refactor - #122 cotains를 이용한 함수 단순화
kseysh Jun 18, 2024
78a12bf
refactor - #122 불필요한 of 함수 제거
kseysh Jun 18, 2024
ce890e1
refactor - #122 온보딩 정보 domain에 NotNull 제약 조건 추가
kseysh Jun 18, 2024
dc904c7
refactor - #122 validate 단순화 및 변수명 수정
kseysh Jun 18, 2024
195ce23
refactor - #122 누락된 userId 필드 추가
kseysh Jun 18, 2024
abdcc8d
refactor - #122 불필요한 userService 메서드 호출 줄임
kseysh Jun 18, 2024
db0b82b
refactor - #122 AuthFacade 네이밍 변경
kseysh Jun 18, 2024
6c3eb8d
refactor - #122 Optinal을 이용해 null로부터 안전하도록 수정
kseysh Jun 18, 2024
00425ac
refactor - #122 가독성을 위한 네이밍 변경
kseysh Jun 18, 2024
d4eb82a
refactor - #122 가독성을 위한 네이밍 변경
kseysh Jun 18, 2024
d712315
refactor - #122 가독성을 위한 네이밍 변경
kseysh Jun 18, 2024
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
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
Loading