Skip to content

Commit

Permalink
Merge pull request #31 from WE-ARE-RACCOONS/RAC-100
Browse files Browse the repository at this point in the history
Rac 100
  • Loading branch information
ywj9811 authored Nov 7, 2023
2 parents 202ee2b + bce1469 commit dc375fa
Show file tree
Hide file tree
Showing 26 changed files with 466 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.postgraduate.domain.auth.application.dto.req;

import jakarta.validation.constraints.NotNull;
import lombok.*;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class KakaoCodeRequest {
@NotNull
private String code;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.postgraduate.domain.auth.application.dto.res;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class KakaoTokenInfoResponse {
private String access_token;
private String token_type;
private String refresh_token;
private String id_token;
private int expires_in;
private String cope;
private int refresh_token_expires_in;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.postgraduate.domain.auth.application.usecase.kakao;

import com.postgraduate.domain.auth.application.dto.res.KakaoAccessTokenResponse;
import com.postgraduate.domain.auth.application.dto.res.KakaoTokenInfoResponse;
import com.postgraduate.domain.auth.application.dto.res.KakaoUserInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.WebClient;

@RequiredArgsConstructor
Expand All @@ -13,13 +17,38 @@ public class KakaoAccessTokenUseCase {

@Value("${app-id.kakao}")
private String APP_ID;
@Value("${kakao.redirect-uri}")
private String REDRECT_URI;
@Value("${kakao.authorization-grant-type}")
private String AUTHORIZATION_GRANT_TYPE;
private final WebClient webClient;

private static final String KAKAO_TOKEN_URI = "https://kauth.kakao.com/oauth/token";
private static final String USER_INFO_URI = "https://kapi.kakao.com/v2/user/me";
private static final String VALIDATE_TOKEN_URI = "https://kapi.kakao.com/v1/user/access_token_info";

public KakaoUserInfoResponse getKakaoToken(String code) {
MultiValueMap<String, String> requestBody = getRequestBody(code);
KakaoTokenInfoResponse tokenInfoResponse = webClient.post()
.uri(KAKAO_TOKEN_URI)
.headers(h -> h.setContentType(MediaType.APPLICATION_FORM_URLENCODED))
.bodyValue(requestBody)
.retrieve()
.bodyToMono(KakaoTokenInfoResponse.class)
.block();
return getUserInfo(tokenInfoResponse.getAccess_token());
}

public KakaoUserInfoResponse getUserInfo(String accessToken) {
private MultiValueMap<String, String> getRequestBody(String code) {
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
requestBody.add("grant_type", AUTHORIZATION_GRANT_TYPE);
requestBody.add("client_id", APP_ID);
requestBody.add("redirect_uri", REDRECT_URI);
requestBody.add("code", code);
return requestBody;
}

private KakaoUserInfoResponse getUserInfo(String accessToken) {
verifyAccessToken(accessToken);

return webClient.get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.postgraduate.domain.auth.application.usecase.kakao;

import com.postgraduate.domain.auth.application.dto.req.KakaoCodeRequest;
import com.postgraduate.domain.auth.application.dto.res.AuthUserResponse;
import com.postgraduate.domain.auth.application.dto.res.KakaoUserInfoResponse;
import com.postgraduate.domain.auth.application.dto.req.SignUpRequest;
Expand All @@ -22,8 +23,8 @@ public class KakaoSignInUseCase {
private final UserGetService userGetService;

@Transactional
public AuthUserResponse getUser(String token) {
KakaoUserInfoResponse userInfo = kakaoTokenUseCase.getUserInfo(token);
public AuthUserResponse getUser(KakaoCodeRequest codeRequest) {
KakaoUserInfoResponse userInfo = kakaoTokenUseCase.getKakaoToken(codeRequest.getCode());
Long socialId = userInfo.getId();
Optional<User> user = userGetService.bySocialId(socialId);
return AuthMapper.mapToAuthUser(user, socialId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.postgraduate.domain.auth.presentation;

import com.postgraduate.domain.auth.application.dto.req.KakaoLoginRequest;
import com.postgraduate.domain.auth.application.dto.req.KakaoCodeRequest;
import com.postgraduate.domain.auth.application.dto.req.SignUpRequest;
import com.postgraduate.domain.auth.application.dto.res.AuthUserResponse;
import com.postgraduate.domain.auth.application.dto.res.JwtTokenResponse;
Expand Down Expand Up @@ -32,11 +32,10 @@ public class AuthController {

@PostMapping("/login")
@Operation(summary = "카카오 로그인", description = "회원인 경우 JWT를, 회원이 아닌 경우 socialId를 반환합니다(회원가입은 진행하지 않습니다).")
public ResponseDto<?> authLogin(@RequestBody KakaoLoginRequest request) {
AuthUserResponse authUser = kakaoSignInUseCase.getUser(request.getAccessToken());
public ResponseDto<?> authLogin(@RequestBody KakaoCodeRequest tokenRequest) {
AuthUserResponse authUser = kakaoSignInUseCase.getUser(tokenRequest);
if (authUser.getUser().isEmpty())
return ResponseDto.create(AUTH_NONE.getCode(), NOT_REGISTERED_USER_MESSAGE.getMessage(), authUser);

JwtTokenResponse jwtToken = jwtUseCase.signIn(authUser.getUser().get());
return ResponseDto.create(AUTH_ALREADY.getCode(), SUCCESS_AUTH_MESSAGE.getMessage(), jwtToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public static AppliedMentoringInfo mapToExpectedAppliedInfo(Mentoring mentoring)
.dates(dates)
.seniorId(senior.getSeniorId())
.nickName(senior.getUser().getNickName())
.postgradu(senior.getPostgradu())
.professor(senior.getProfessor())
.field(senior.getField())
.chatLink(senior.getChatLink())
.postgradu(senior.getInfo().getPostgradu())
.professor(senior.getInfo().getProfessor())
.field(senior.getInfo().getField())
.chatLink(senior.getProfile().getChatLink())
.build();
}
public static AppliedMentoringInfo mapToWaitingOrDoneAppliedInfo(Mentoring mentoring) {
Expand All @@ -31,12 +31,12 @@ public static AppliedMentoringInfo mapToWaitingOrDoneAppliedInfo(Mentoring mento
return AppliedMentoringInfo.builder()
.mentoringId(mentoring.getMentoringId())
.dates(dates)
.term(senior.getTerm())
.term(senior.getProfile().getTerm())
.seniorId(senior.getSeniorId())
.nickName(senior.getUser().getNickName())
.postgradu(senior.getPostgradu())
.professor(senior.getProfessor())
.field(senior.getField())
.postgradu(senior.getInfo().getPostgradu())
.professor(senior.getInfo().getProfessor())
.field(senior.getInfo().getField())
.build();
}
public static AppliedMentoringDetailResponse mapToAppliedDetailInfo(Mentoring mentoring) {
Expand All @@ -46,8 +46,8 @@ public static AppliedMentoringDetailResponse mapToAppliedDetailInfo(Mentoring me
return AppliedMentoringDetailResponse.builder()
.seniorId(senior.getSeniorId())
.nickName(senior.getUser().getNickName())
.field(senior.getField())
.professor(senior.getProfessor())
.field(senior.getInfo().getField())
.professor(senior.getInfo().getProfessor())
.topic(mentoring.getTopic())
.question(mentoring.getQuestion())
.dates(dates)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.postgraduate.domain.auth.application.dto.req;
package com.postgraduate.domain.senior.application.dto.req;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
Expand All @@ -8,7 +8,7 @@
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class KakaoLoginRequest {
public class SeniorCertificationRequest {
@NotNull
private String accessToken;
private String certification;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.postgraduate.domain.senior.application.dto.req;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class SeniorProfileAndAccountRequest {
@NotNull
private String info;
@NotNull
private String target;
@NotNull
private String chatLink;
@NotNull
private String time;
@NotNull
private String account;
@NotNull
private String bank;
@NotNull
private String rrn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.postgraduate.domain.senior.application.dto.res;

import com.postgraduate.domain.senior.domain.entity.constant.Status;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor
public class SeniorInfoResponse {
private String nickName;
private String profile;
private Status certificationRegister;
private boolean profileRegister;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.postgraduate.domain.senior.application.dto.res;

import com.postgraduate.domain.senior.domain.entity.Account;
import com.postgraduate.domain.senior.domain.entity.Profile;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor
public class SeniorProfileResponse {
private Profile profile;
private Account account;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,86 @@
package com.postgraduate.domain.senior.application.mapper;

import com.postgraduate.domain.senior.application.dto.req.SeniorProfileAndAccountRequest;
import com.postgraduate.domain.senior.application.dto.req.SeniorProfileRequest;
import com.postgraduate.domain.senior.application.dto.req.SeniorSignUpRequest;
import com.postgraduate.domain.senior.application.dto.res.SeniorInfoResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorProfileResponse;
import com.postgraduate.domain.senior.domain.entity.Account;
import com.postgraduate.domain.senior.domain.entity.Info;
import com.postgraduate.domain.senior.domain.entity.Profile;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.senior.domain.entity.constant.Status;
import com.postgraduate.domain.user.domain.entity.User;

public class SeniorMapper {

public static Senior mapToSenior(User user, SeniorSignUpRequest request) {
return Senior.builder()
.user(user)
.info(mapToInfo(request))
.certification(request.getCertification())
.account(mapToAccount(request))
.build();
}

public static Info mapToInfo(SeniorSignUpRequest request) {
return Info.builder()
.college(request.getCollege())
.major(request.getMajor())
.postgradu(request.getPostgradu())
.professor(request.getProfessor())
.lab(request.getLab())
.field(request.getField())
.certification(request.getCertification())
.account(request.getAccount())
.build();
}

public static Account mapToAccount(SeniorSignUpRequest request) {
return Account.builder()
.bank(request.getBank())
.account(request.getAccount())
.rrn(request.getRrn())
.build();
}

public static Account mapToAccount(SeniorProfileAndAccountRequest profileAndAccountPageRequest) {
return Account.builder()
.bank(profileAndAccountPageRequest.getBank())
.account(profileAndAccountPageRequest.getAccount())
.rrn(profileAndAccountPageRequest.getRrn())
.build();
}

public static Profile mapToProfile(SeniorProfileAndAccountRequest profileAndAccountPageRequest) {
return Profile.builder()
.info(profileAndAccountPageRequest.getInfo())
.chatLink(profileAndAccountPageRequest.getChatLink())
.target(profileAndAccountPageRequest.getTarget())
.time(profileAndAccountPageRequest.getTime())
.build();
}

public static Profile mapToProfile(SeniorProfileRequest profileRequest) {
return Profile.builder()
.info(profileRequest.getInfo())
.chatLink(profileRequest.getChatLink())
.target(profileRequest.getTarget())
.time(profileRequest.getTime())
.build();
}

public static SeniorInfoResponse mapToSeniorInfo(Senior senior, Status certificationRegister, boolean profileRegister) {
return SeniorInfoResponse.builder()
.nickName(senior.getUser().getNickName())
.profile(senior.getUser().getProfile())
.certificationRegister(certificationRegister)
.profileRegister(profileRegister)
.build();
}

public static SeniorProfileResponse mapToSeniorProfileInfo(Senior senior) {
return SeniorProfileResponse.builder()
.profile(senior.getProfile())
.account(senior.getAccount())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.postgraduate.domain.senior.application.usecase;

import com.postgraduate.domain.senior.application.dto.req.SeniorCertificationRequest;
import com.postgraduate.domain.senior.application.dto.req.SeniorProfileAndAccountRequest;
import com.postgraduate.domain.senior.application.dto.res.SeniorInfoResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorProfileResponse;
import com.postgraduate.domain.senior.application.mapper.SeniorMapper;
import com.postgraduate.domain.senior.domain.entity.Account;
import com.postgraduate.domain.senior.domain.entity.Profile;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.senior.domain.entity.constant.Status;
import com.postgraduate.domain.senior.domain.service.SeniorGetService;
import com.postgraduate.domain.senior.domain.service.SeniorUpdateService;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.auth.AuthDetails;
import com.postgraduate.global.config.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static java.util.Optional.ofNullable;

@Service
@RequiredArgsConstructor
@Transactional
public class SeniorMyPageUseCase {
private final SeniorGetService seniorGetService;
private final SeniorUpdateService seniorUpdateService;
private final SecurityUtils securityUtils;

public SeniorInfoResponse seniorInfo(AuthDetails authDetails) {
User user = securityUtils.getLoggedInUser(authDetails);
Senior senior = seniorGetService.byUser(user);
Status status = senior.getStatus();
Optional<Profile> profile = ofNullable(senior.getProfile());
return SeniorMapper.mapToSeniorInfo(senior, status, profile.isPresent());
}

public void updateCertification(AuthDetails authDetails, SeniorCertificationRequest certificationRequest) {
User user = securityUtils.getLoggedInUser(authDetails);
Senior senior = seniorGetService.byUser(user);
seniorUpdateService.updateCertification(senior, certificationRequest.getCertification());
}

public void updateProfile(AuthDetails authDetails, SeniorProfileAndAccountRequest profileRequest) {
User user = securityUtils.getLoggedInUser(authDetails);
Senior senior = seniorGetService.byUser(user);
Profile profile = SeniorMapper.mapToProfile(profileRequest);
Account account = SeniorMapper.mapToAccount(profileRequest);
seniorUpdateService.updateSeniorProfileAndAccount(senior, profile, account);
}

public SeniorProfileResponse getSeniorProfile(AuthDetails authDetails) {
User user = securityUtils.getLoggedInUser(authDetails);
Senior senior = seniorGetService.byUser(user);
SeniorProfileResponse seniorProfileResponse = SeniorMapper.mapToSeniorProfileInfo(senior);
return seniorProfileResponse;
}
}
Loading

0 comments on commit dc375fa

Please sign in to comment.