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] : 조각 결과 도출 알고리즘 구현 #21

Merged
merged 10 commits into from
May 14, 2024
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.jangkku.global.auth.application;
package kusitms.jangkku.domain.auth.application;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package kusitms.jangkku.global.auth.application;
package kusitms.jangkku.domain.auth.application;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import kusitms.jangkku.domain.auth.dto.GoogleUserInfo;
import kusitms.jangkku.domain.auth.dto.OAuth2UserInfo;
import kusitms.jangkku.domain.persona.dao.DefinePersonaRepository;
import kusitms.jangkku.domain.persona.domain.DefinePersona;
import kusitms.jangkku.domain.token.dao.RefreshTokenRepository;
import kusitms.jangkku.domain.token.domain.RefreshToken;
import kusitms.jangkku.domain.user.dao.UserRepository;
import kusitms.jangkku.domain.user.domain.User;
import kusitms.jangkku.global.auth.dto.GoogleUserInfo;
import kusitms.jangkku.global.auth.dto.KakaoUserInfo;
import kusitms.jangkku.global.auth.dto.NaverUserInfo;
import kusitms.jangkku.global.auth.dto.OAuth2UserInfo;
import kusitms.jangkku.domain.auth.dto.KakaoUserInfo;
import kusitms.jangkku.domain.auth.dto.NaverUserInfo;
import kusitms.jangkku.global.util.CookieUtil;
import kusitms.jangkku.global.util.JwtUtil;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.jangkku.global.common.constant;
package kusitms.jangkku.domain.auth.constant;

public enum Provider {
GOOGLE_PROVIDER("google"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.jangkku.global.auth.dto;
package kusitms.jangkku.domain.auth.dto;

import kusitms.jangkku.global.common.constant.Provider;
import kusitms.jangkku.domain.auth.constant.Provider;
import lombok.AllArgsConstructor;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.jangkku.global.auth.dto;
package kusitms.jangkku.domain.auth.dto;

import kusitms.jangkku.global.common.constant.Provider;
import kusitms.jangkku.domain.auth.constant.Provider;
import lombok.AllArgsConstructor;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.jangkku.global.auth.dto;
package kusitms.jangkku.domain.auth.dto;

import kusitms.jangkku.global.common.constant.Provider;
import kusitms.jangkku.domain.auth.constant.Provider;
import lombok.AllArgsConstructor;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.jangkku.global.auth.dto;
package kusitms.jangkku.domain.auth.dto;

public interface OAuth2UserInfo {
String getProviderId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kusitms.jangkku.domain.persona.api;

import kusitms.jangkku.domain.persona.application.DefinePersonaService;
import kusitms.jangkku.domain.persona.constant.PersonaSuccessStatus;
import kusitms.jangkku.domain.persona.dto.DefinePersonaDto;
import kusitms.jangkku.global.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/personas")
@RequiredArgsConstructor
public class DefinePersonaController {
private final DefinePersonaService definePersonaService;

// 현재 페르소나 결과를 반환하는 API
@PostMapping("/define")
public ResponseEntity<ApiResponse<DefinePersonaDto.DefinePersonaResponse>> getDefinePersona(
@RequestHeader(value = "Authorization", required = false) String authorizationHeader,
@RequestBody DefinePersonaDto.DefinePersonaRequest definePersonaRequest) {

DefinePersonaDto.DefinePersonaResponse definePersonaResponse = definePersonaService.getDefinePersona(authorizationHeader, definePersonaRequest);
return ApiResponse.onSuccess(PersonaSuccessStatus.CREATED_DEFINE_PERSONA, definePersonaResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kusitms.jangkku.domain.persona.application;

import kusitms.jangkku.domain.persona.dto.DefinePersonaDto;

public interface DefinePersonaService {
DefinePersonaDto.DefinePersonaResponse getDefinePersona(String authorizationHeader, DefinePersonaDto.DefinePersonaRequest definePersonaRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package kusitms.jangkku.domain.persona.application;


import kusitms.jangkku.domain.persona.constant.Type;
import kusitms.jangkku.domain.persona.constant.Keyword;
import kusitms.jangkku.domain.persona.dao.DefinePersonaKeywordRepository;
import kusitms.jangkku.domain.persona.dao.DefinePersonaRepository;
import kusitms.jangkku.domain.persona.domain.DefinePersona;
import kusitms.jangkku.domain.persona.domain.DefinePersonaKeyword;
import kusitms.jangkku.domain.persona.dto.DefinePersonaDto;
import kusitms.jangkku.domain.persona.exception.PersonaErrorResult;
import kusitms.jangkku.domain.persona.exception.PersonaException;
import kusitms.jangkku.domain.user.dao.UserRepository;
import kusitms.jangkku.domain.user.domain.User;
import kusitms.jangkku.domain.user.exception.UserErrorResult;
import kusitms.jangkku.domain.user.exception.UserException;
import kusitms.jangkku.global.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class DefinePersonaServiceImpl implements DefinePersonaService {
private final JwtUtil jwtUtil;
private final UserRepository userRepository;
private final DefinePersonaRepository definePersonaRepository;
private final DefinePersonaKeywordRepository definePersonaKeywordRepository;

@Override
public DefinePersonaDto.DefinePersonaResponse getDefinePersona(String authorizationHeader, DefinePersonaDto.DefinePersonaRequest definePersonaRequest) {
List<String> definePersonaKeywords = new ArrayList<>();
String stepOneKeyword = judgeStepOneType(definePersonaRequest.getStageOneKeywords(), definePersonaKeywords);
String stepTwoKeyword = judgeStepTwoType(definePersonaRequest.getStageTwoKeywords(), definePersonaKeywords);
String stepThreeKeyword = judgeStepThreeType(definePersonaRequest.getStageThreeKeywords(), definePersonaKeywords);

String definePersonaType = stepOneKeyword + stepTwoKeyword + stepThreeKeyword;
String definePersonaName = judgeDefinePersonaName(definePersonaType);

if (authorizationHeader != null) {
saveDefinePersona(authorizationHeader, definePersonaName, definePersonaKeywords);
}

return DefinePersonaDto.DefinePersonaResponse.builder()
.definePersonaName(definePersonaName)
.definePersonaKeywords(definePersonaKeywords)
.build();
}

private String judgeStepOneType(List<String> stepOneKeywords, List<String> definePersonaKeywords) {
List<String> realisticKeywords = Keyword.REALISTIC.getKeywords();
List<String> socialKeywords = Keyword.SOCIAL.getKeywords();

List<String> moreCountKeywords = judgeMoreCountKeywords(stepOneKeywords, realisticKeywords, socialKeywords);
definePersonaKeywords.add(moreCountKeywords.get(0));
definePersonaKeywords.add(moreCountKeywords.get(1));

return realisticKeywords.contains(moreCountKeywords.get(0)) ? "R" : "S";
}

private String judgeStepTwoType(List<String> stepTwoKeywords, List<String> definePersonaKeywords) {
List<String> investigativeKeywords = Keyword.INVESTIGATIVE.getKeywords();
List<String> enterprisingKeywords = Keyword.ENTERPRISING.getKeywords();

List<String> moreCountKeywords = judgeMoreCountKeywords(stepTwoKeywords, investigativeKeywords, enterprisingKeywords);
definePersonaKeywords.add(moreCountKeywords.get(0));
definePersonaKeywords.add(moreCountKeywords.get(1));

return investigativeKeywords.contains(moreCountKeywords.get(0)) ? "I" : "E";
}

private String judgeStepThreeType(List<String> stepThreeKeywords, List<String> definePersonaKeywords) {
List<String> artisticKeywords = Keyword.ARTISTIC.getKeywords();
List<String> conventionKeywords = Keyword.CONVENTION.getKeywords();

List<String> moreCountKeywords = judgeMoreCountKeywords(stepThreeKeywords, artisticKeywords, conventionKeywords);
definePersonaKeywords.add(moreCountKeywords.get(0));

return artisticKeywords.contains(moreCountKeywords.get(0)) ? "A" : "C";
}

private String judgeDefinePersonaName(String definePersonaType) {

for (Type type : Type.values()) {
if (type.getCode().equals(definePersonaType)) {
return type.getName();
}
}

throw new PersonaException(PersonaErrorResult.NOT_FOUND_PERSONA_TYPE);
}

private List<String> judgeMoreCountKeywords (List<String> stepKeywords, List<String> firstKeywords, List<String> secondKeywords) {
List<String> pickedFirstKeywords = new ArrayList<>();
List<String> pickedSecondKeywords = new ArrayList<>();

for (String stepKeyword : stepKeywords) {
if (firstKeywords.contains(stepKeyword)) {
pickedFirstKeywords.add(stepKeyword);
} else if (secondKeywords.contains(stepKeyword)) {
pickedSecondKeywords.add(stepKeyword);
}
}

return pickedFirstKeywords.size() > pickedSecondKeywords.size() ? pickedFirstKeywords : pickedSecondKeywords;
}

private void saveDefinePersona(String authorizationHeader, String definePersonaName, List<String> definePersonaKeywords) {
String token = jwtUtil.getTokenFromHeader(authorizationHeader);
UUID userId = UUID.fromString(jwtUtil.getUserIdFromToken(token));
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new UserException(UserErrorResult.NOT_FOUND_USER));

DefinePersona definePersona = DefinePersona.builder()
.user(user)
.name(definePersonaName)
.build();
definePersonaRepository.save(definePersona);

for (String keyword : definePersonaKeywords) {
DefinePersonaKeyword definePersonaKeyword = DefinePersonaKeyword.builder()
.definePersona(definePersona)
.name(keyword)
.build();
definePersonaKeywordRepository.save(definePersonaKeyword);
}
}
}
74 changes: 74 additions & 0 deletions src/main/java/kusitms/jangkku/domain/persona/constant/Keyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package kusitms.jangkku.domain.persona.constant;

import java.util.Arrays;
import java.util.List;

public enum Keyword {
REALISTIC(Arrays.asList(
"남성적임",
"솔직함",
"검소함",
"지구력 있음",
"신체적으로 건강함",
"소박함",
"말이 적음",
"단순함")),
SOCIAL(Arrays.asList(
"사람들을 좋아함",
"어울리기 좋아함",
"친절함",
"이해심 많음",
"남을 잘 도와줌",
"봉사적",
"감정적",
"이상주의적")),
INVESTIGATIVE(Arrays.asList(
"탐구심 많음",
"논리적",
"분석적",
"합리적",
"정확함",
"지적 호기심",
"비판적",
"내성적",
"수줍음",
"신중함")),
ENTERPRISING(Arrays.asList(
"지배적",
"통솔력",
"지도력 있음",
"말을 잘함",
"설득적",
"경제적",
"야심적",
"외향적",
"낙관적",
"열성적")),
ARTISTIC(Arrays.asList(
"상상력 풍부함",
"감수성 강함",
"자유분방함",
"개방적",
"독창적",
"개성적",
"협동적이지 않음")),
CONVENTION(Arrays.asList(
"정확함",
"빈틈없음",
"조심성 있음",
"세밀함",
"계획적",
"변화를 좋아하지 않음",
"완고함",
"책임감"));

private final List<String> keywords;

Keyword(List<String> keywords) {
this.keywords = keywords;
}

public List<String> getKeywords() {
return keywords;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kusitms.jangkku.domain.persona.constant;

import kusitms.jangkku.global.common.code.BaseCode;
import kusitms.jangkku.global.common.dto.ReasonDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum PersonaSuccessStatus implements BaseCode {
// 정의하기 페르소나
CREATED_DEFINE_PERSONA(HttpStatus.CREATED, "201", "정의하기 페르소나 생성에 성공했습니다.");

private final HttpStatus httpStatus;
private final String code;
private final String message;

@Override
public ReasonDto getReason() {
return ReasonDto.builder()
.isSuccess(true)
.code(code)
.message(message)
.build();
}

@Override
public ReasonDto getReasonHttpStatus() {
return ReasonDto.builder()
.isSuccess(true)
.httpStatus(httpStatus)
.code(code)
.message(message)
.build();
}
}
29 changes: 29 additions & 0 deletions src/main/java/kusitms/jangkku/domain/persona/constant/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kusitms.jangkku.domain.persona.constant;

public enum Type {
CREATOR("RIA", "크리에이터"),
INSIGHTER("RIC", "인사이터"),
INNOVATOR("REA", "이노베이터"),
INVENTOR("REC", "인벤터"),
PROJECTOR("SIA", "프로젝터"),
CONNECTOR("SIC", "커넥터"),
ENCOURAGER("SEA", "인커리져"),
ORGANIZER("SEC", "오거나이져");

private final String code;
private final String name;


Type(String code, String name) {
this.code = code;
this.name = name;
}

public String getCode() {
return code;
}

public String getName() {
return name;
}
}
Loading
Loading