Skip to content

Commit

Permalink
Merge pull request #24 from Mojacknong/feature_23/11/11회의-수정
Browse files Browse the repository at this point in the history
Feature 23/11/11회의 수정
  • Loading branch information
Ryeolee authored Nov 11, 2023
2 parents af16044 + 70809d8 commit 0bd2ee0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
public enum ErrorMessage {

INTERVAL_SERVER_ERROR(1001, "요청을 처리하는 과정에서 서버가 예상하지 못한 오류가 발생하였습니다."),
REFRESH_NOTIFICATION_ERROR(4017, "Refresh Token 인증 오류"),
NO_USER_DATA(1002, "유저에 대한 정보가 없습니다.");
REFRESH_NOTIFICATION_ERROR(1002, "Refresh Token 인증 오류"),
NO_USER_DATA(1003, "유저에 대한 정보가 없습니다."),
NO_MOTIVATION_DATA(1004, "동기에 대한 정보가 없습니다.");



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package modernfarmer.server.farmususer.user.entity;

import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.LinkedHashSet;
import java.util.Set;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Entity
@Table(name = "motivation")
public class Motivation extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "motivation_id", nullable = false)
private Long id;


@Column(name = "motivation_reason", nullable = false, length = 40)
private String motivationReason;

@OneToMany(mappedBy = "motivation", fetch = FetchType.LAZY)
private Set<UserMotivation> userMotivations = new LinkedHashSet<>();



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.Instant;

@Builder
@AllArgsConstructor
Expand All @@ -28,10 +26,18 @@ public class UserMotivation extends BaseEntity{
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Size(max = 40)
@NotNull
@Column(name = "user_motivation", nullable = false, length = 40)
private String userMotivation;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "motivation_id", nullable = false)
private Motivation motivation;










Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package modernfarmer.server.farmususer.user.repository;


import modernfarmer.server.farmususer.user.entity.Motivation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface MotivationRepository extends JpaRepository<Motivation, Long> {

@Query("select m.id from Motivation as m where m.motivationReason = :motivationReason")
Long getMotivationId(@Param("motivationReason") String motivationReason);


Optional<Motivation> findByMotivationReason(String motivationReason);



}
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,15 @@ public BaseResponseDto googleLogin(String accessToken) {
User user;
boolean early;

Mono<GoogleUserResponseDto> userInfoMono = getUserGoogleInfo(accessToken);
Mono<GoogleUserResponseDto> userInfoMono = getUserInfo(accessToken, "https://www.googleapis.com/oauth2/v2/userinfo", GoogleUserResponseDto.class);
GoogleUserResponseDto userInfo = userInfoMono.block();

Optional<User> userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId()));

log.info(String.valueOf(userInfo.getEmail()));
log.info(String.valueOf(userInfo.getPicture()));
log.info(String.valueOf(userInfo.getId()));

if(userData.isEmpty()){
user = User.builder()
.userNumber(String.valueOf(userInfo.getId()))
.roles("USER")
.profileImage(userInfo.getPicture())
.early(true)
.build();

Expand Down Expand Up @@ -86,23 +81,16 @@ public BaseResponseDto kakaoLogin(String accessToken) {

User user;
boolean early;
Mono<KakaoUserResponseDto> userInfoMono = getUserKakaoInfo(accessToken);
Mono<KakaoUserResponseDto> userInfoMono = getUserInfo(accessToken, "https://kapi.kakao.com/v2/user/me", KakaoUserResponseDto.class);
KakaoUserResponseDto userInfo = userInfoMono.block();


log.info(String.valueOf(userInfo.getKakao_account().getEmail()));
log.info(String.valueOf(userInfo.getKakao_account().getProfile().getProfile_image_url()));
log.info(String.valueOf(userInfo.getKakao_account().getProfile().getNickname()));


Optional<User> userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId()));


if(userData.isEmpty()){
user = User.builder()
.userNumber(String.valueOf(userInfo.getId()))
.roles("USER")
.profileImage(userInfo.getKakao_account().getProfile().getProfile_image_url())
.early(true)
.build();

Expand Down Expand Up @@ -131,21 +119,13 @@ public BaseResponseDto kakaoLogin(String accessToken) {
return baseResponseDto;
}

public Mono<KakaoUserResponseDto> getUserKakaoInfo(String accessToken) {
public <T> Mono<T> getUserInfo(String accessToken, String apiUrl, Class<T> responseType) {
return webClient
.get()
.uri("https://kapi.kakao.com/v2/user/me") // 카카오 사용자 정보 엔드포인트
.uri(apiUrl)
.headers(headers -> headers.setBearerAuth(accessToken))
.retrieve()
.bodyToMono(KakaoUserResponseDto.class);
.bodyToMono(responseType);
}

public Mono<GoogleUserResponseDto> getUserGoogleInfo(String accessToken) {
return webClient
.get()
.uri("https://www.googleapis.com/oauth2/v2/userinfo") // 카카오 사용자 정보 엔드포인트
.headers(headers -> headers.setBearerAuth(accessToken))
.retrieve()
.bodyToMono(GoogleUserResponseDto.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import modernfarmer.server.farmususer.user.dto.request.OnBoardingMotivationRequest;
import modernfarmer.server.farmususer.user.dto.response.BaseResponseDto;
import modernfarmer.server.farmususer.user.dto.response.OnBoardingLevelResponse;
import modernfarmer.server.farmususer.user.entity.Motivation;
import modernfarmer.server.farmususer.user.entity.User;
import modernfarmer.server.farmususer.user.entity.UserMotivation;
import modernfarmer.server.farmususer.user.repository.MotivationRepository;
import modernfarmer.server.farmususer.user.repository.UserMotivationRepository;
import modernfarmer.server.farmususer.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Optional;


@RequiredArgsConstructor
Expand All @@ -31,16 +32,24 @@ public class OnBoardingService {

private final UserMotivationRepository userMotivationRepository;

private final MotivationRepository motivationRepository;

public BaseResponseDto onBoardingMotivation(Long userId, OnBoardingMotivationRequest onBoardingMotivationRequest){

User user = User.builder().id(userId).build();

for(String motivations : onBoardingMotivationRequest.getMotivation()){

Optional<Motivation> motivation = motivationRepository.findByMotivationReason(motivations);

if(motivation.isEmpty()){
return BaseResponseDto.of(ErrorMessage.NO_MOTIVATION_DATA);
}

UserMotivation userMotivation = UserMotivation
.builder()
.user(user)
.userMotivation(motivations)
.motivation(motivation.get())
.build();

userMotivationRepository.save(userMotivation);
Expand All @@ -62,39 +71,38 @@ public BaseResponseDto onBoardingLevel(Long userId, OnBoardingLevelRequest onBoa

private String recommendAlgorithms(int time,String skill) {
boolean isIntermediate = false;
boolean isAdvanced = false;
boolean isExperienced = false;
boolean isMaster = false;
boolean isElementary = false;
boolean isBeginner = false;


if ("홈파밍 중급".equals(skill)) {
isIntermediate = true;
} else if ("홈파밍 고수".equals(skill)) {
isAdvanced = true;
isMaster = true;
} else if ("홈파밍 초보".equals(skill)) {
isExperienced = true;
isElementary = true;
} else if ("홈파밍 입문".equals(skill)) {
isBeginner = true;
}


if (time == 2 && (isIntermediate || isAdvanced)) {
if (time == 2 && (isIntermediate || isMaster)) {
return "HARD";
} else if (time == 2 && (isBeginner || isExperienced)) {
} else if (time == 2 && (isBeginner || isElementary)) {
return "NORMAL";
} else if (time == 1 && isAdvanced) {
} else if (time == 1 && isMaster) {
return "HARD";
} else if (time == 1 && (isIntermediate || isExperienced)) {
} else if (time == 1 && (isIntermediate || isElementary)) {
return "NORMAL";
} else if (time == 1 && isBeginner) {
return "EASY";
} else if (time == 0 && (isExperienced && isBeginner)) {
return "EASY";
} else if (time == 0 && isExperienced) {
return "NORMAL";

} else if (time == 0 && isAdvanced) {
} else if (time == 0 && isMaster) {
return "HARD";
} else if (time == 0 && isIntermediate) {
return "NORMAL";
} else if (time == 0 && (isElementary || isBeginner)) {
return "EASY";
}

return "알 수 없음";
Expand Down

0 comments on commit 0bd2ee0

Please sign in to comment.