Skip to content

Commit

Permalink
Merge pull request #24 from KUSITMS-29th-TEAM-D/feature/#18/define-pe…
Browse files Browse the repository at this point in the history
…rsona

[feat] : 조각 조회 API 구현 (GET)
  • Loading branch information
bbbang105 authored May 15, 2024
2 parents bf7d063 + e81ff44 commit d0d35af
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 34 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
//h2
runtimeOnly 'com.h2database:h2'

//WebClient
implementation 'org.springframework.boot:spring-boot-starter-webflux'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@
public class DefinePersonaController {
private final DefinePersonaService definePersonaService;

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

DefinePersonaDto.DefinePersonaResponse definePersonaResponse = definePersonaService.getDefinePersona(authorizationHeader, definePersonaRequest);
DefinePersonaDto.DefinePersonaResponse definePersonaResponse = definePersonaService.createDefinePersona(authorizationHeader, definePersonaRequest);

return ApiResponse.onSuccess(PersonaSuccessStatus.CREATED_DEFINE_PERSONA, definePersonaResponse);
}

// 정의하기 페르소나 결과를 반환하는 API
@GetMapping("/define")
public ResponseEntity<ApiResponse<DefinePersonaDto.DefinePersonaResponse>> getDefinePersona(
@RequestHeader("Authorization") String authorizationHeader) {

DefinePersonaDto.DefinePersonaResponse definePersonaResponse = definePersonaService.getDefinePersona(authorizationHeader);

return ApiResponse.onSuccess(PersonaSuccessStatus.GET_DEFINE_PERSONA, definePersonaResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import kusitms.jangkku.domain.persona.dto.DefinePersonaDto;

public interface DefinePersonaService {
DefinePersonaDto.DefinePersonaResponse getDefinePersona(String authorizationHeader, DefinePersonaDto.DefinePersonaRequest definePersonaRequest);
DefinePersonaDto.DefinePersonaResponse createDefinePersona(String authorizationHeader, DefinePersonaDto.DefinePersonaRequest definePersonaRequest);
DefinePersonaDto.DefinePersonaResponse getDefinePersona(String authorizationHeader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,42 @@ public class DefinePersonaServiceImpl implements DefinePersonaService {

// 정의하기 페르소나 결과를 도출하는 메서드
@Override
public DefinePersonaDto.DefinePersonaResponse getDefinePersona(String authorizationHeader, DefinePersonaDto.DefinePersonaRequest definePersonaRequest) {
public DefinePersonaDto.DefinePersonaResponse createDefinePersona(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);
String definePersonaCode = stepOneKeyword + stepTwoKeyword + stepThreeKeyword;
String definePersonaName = judgeDefinePersonaName(definePersonaCode);

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

return createDefinePersonaResponse(definePersonaType, definePersonaKeywords);
return createDefinePersonaResponse(definePersonaCode, definePersonaKeywords);
}

// 정의하기 페르소나 결과를 반환하는 메서드
@Override
public DefinePersonaDto.DefinePersonaResponse getDefinePersona(String authorizationHeader) {
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 = definePersonaRepository.findTopByUserOrderByCreatedAtDesc(user); // 가장 최근 결과만 가져옴

if (definePersona == null) {
throw new PersonaException(PersonaErrorResult.NOT_FOUND_DEFINE_PERSONA);
}

List<DefinePersonaKeyword> definePersonaKeywords = definePersonaKeywordRepository.findAllByDefinePersona(definePersona);
List<String> keywordStrings = definePersonaKeywords.stream()
.map(DefinePersonaKeyword::getName)
.toList();

return createDefinePersonaResponse(definePersona.getCode(), keywordStrings);
}

// 첫번째 유형 도출 메서드
Expand Down Expand Up @@ -86,10 +108,10 @@ private String judgeStepThreeType(List<String> stepThreeKeywords, List<String> d
}

// 3가지 유형으로 페르소나를 판단하는 메서드
private String judgeDefinePersonaName(String definePersonaType) {
private String judgeDefinePersonaName(String definePersonaCode) {

for (Type type : Type.values()) {
if (type.getCode().equals(definePersonaType)) {
if (type.getCode().equals(definePersonaCode)) {
return type.getName();
}
}
Expand All @@ -114,7 +136,7 @@ private List<String> judgeMoreCountKeywords (List<String> stepKeywords, List<Str
}

// 정의하기 페르소나를 저장하는 메서드
private void saveDefinePersona(String authorizationHeader, String definePersonaName, List<String> definePersonaKeywords) {
private void saveDefinePersona(String authorizationHeader, String definePersonaName, String definePersonaCode, List<String> definePersonaKeywords) {
String token = jwtUtil.getTokenFromHeader(authorizationHeader);
UUID userId = UUID.fromString(jwtUtil.getUserIdFromToken(token));
User user = userRepository.findByUserId(userId)
Expand All @@ -123,6 +145,7 @@ private void saveDefinePersona(String authorizationHeader, String definePersonaN
DefinePersona definePersona = DefinePersona.builder()
.user(user)
.name(definePersonaName)
.code(definePersonaCode)
.build();
definePersonaRepository.save(definePersona);

Expand All @@ -136,9 +159,9 @@ private void saveDefinePersona(String authorizationHeader, String definePersonaN
}

// 정의하기 페르소나 응답 객체를 만드는 메서드
private DefinePersonaDto.DefinePersonaResponse createDefinePersonaResponse(String definePersonaType, List<String> definePersonaKeywords) {
private DefinePersonaDto.DefinePersonaResponse createDefinePersonaResponse(String definePersonaCode, List<String> definePersonaKeywords) {
Content content = Arrays.stream(Content.values())
.filter(desc -> desc.getCode().equals(definePersonaType))
.filter(desc -> desc.getCode().equals(definePersonaCode))
.findFirst()
.orElse(null);

Expand All @@ -147,6 +170,7 @@ private DefinePersonaDto.DefinePersonaResponse createDefinePersonaResponse(Strin
}

return DefinePersonaDto.DefinePersonaResponse.builder()
.name(content.getName())
.comment(content.getComment())
.description(content.getDescription())
.ability(content.getAbility())
Expand Down
Loading

0 comments on commit d0d35af

Please sign in to comment.