Skip to content

Commit

Permalink
fix 문제 추천 및 문제 검색 숫자 integer 넘어가는 값 예외처리 및 integer에서 long으로 필드값 변경
Browse files Browse the repository at this point in the history
* fix: BojProblem 필드값 text로 변경 (#70)

* fix: Integer 값 Long으로 변경 (#70)
  • Loading branch information
toychip authored Jan 26, 2024
1 parent 8110681 commit 2a5f692
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ public class BojProblem {
@Column(name = "Title", columnDefinition = "TEXT")
private String title;

@Lob
@Column(name = "Description")
@Column(name = "Description", columnDefinition = "TEXT")
private String description;

@Lob
@Column(name = "Input Description")
@Column(name = "Input Description", columnDefinition = "TEXT")
private String inputDescription;

@Column(name = "Output Description", columnDefinition = "TEXT")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.api.TaveShot.domain.recommend.controller;

import com.api.TaveShot.domain.post.post.dto.response.PostResponse;
import com.api.TaveShot.domain.recommend.dto.RecProRequestDto;
import com.api.TaveShot.domain.recommend.dto.RecProResponseDto;
import com.api.TaveShot.domain.recommend.dto.RecResponseDto;
import com.api.TaveShot.domain.recommend.service.RecommendService;
import com.api.TaveShot.global.success.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Slf4j
@RequiredArgsConstructor
Expand All @@ -46,7 +38,8 @@ public SuccessResponse<RecResponseDto> getUserBasedProList() throws IOException
@ApiResponse(responseCode = "200", description = "추천 성공")
})
@GetMapping("/problem")
public SuccessResponse<RecResponseDto> getSolvedProList(@RequestParam(value = "solvedRecentId") int solvedNumber) throws IOException {
public SuccessResponse<RecResponseDto> getSolvedProList(@RequestParam(value = "solvedRecentId") Long solvedNumber)
throws IOException {
RecResponseDto responseDto = recommendService.getListByProblem(solvedNumber);
return new SuccessResponse<>(responseDto);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.api.TaveShot.domain.recommend.dto;

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

@Getter
@Builder
public class RecProRequestDto {

private int solvedRecentId;
private Long solvedRecentId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public interface ProblemElementRepository extends JpaRepository<ProblemElement, Long> {

@Query("select p from ProblemElement as p where p.problemId=:problemId")
Optional<ProblemElement> findByProblemId(Integer problemId);
Optional<ProblemElement> findByProblemId(Long problemId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
import com.api.TaveShot.domain.Member.domain.Member;
import com.api.TaveShot.domain.Member.repository.MemberRepository;
import com.api.TaveShot.domain.recommend.domain.ProblemElement;
import com.api.TaveShot.domain.recommend.dto.*;
import com.api.TaveShot.domain.recommend.dto.RecProDetailResponseDto;
import com.api.TaveShot.domain.recommend.dto.RecProRequestDto;
import com.api.TaveShot.domain.recommend.dto.RecProResponseDto;
import com.api.TaveShot.domain.recommend.dto.RecResponseDto;
import com.api.TaveShot.domain.recommend.dto.UserCrawlingDto;
import com.api.TaveShot.domain.recommend.repository.ProblemElementRepository;
import com.api.TaveShot.domain.recommend.repository.TierCountRepository;
import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;
import com.api.TaveShot.global.util.SecurityUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -52,10 +55,9 @@ public RecResponseDto getListByUser() throws IOException {
.block();

Integer tierCount;
if(dto.getTier().equals("0")){
if (dto.getTier().equals("0")) {
tierCount = 0;
}
else {
} else {
tierCount = tierCountRepository.findByTier(Integer.parseInt(dto.getTier()));
}
log.info("tier:{}", tierCount);
Expand All @@ -74,15 +76,16 @@ public RecResponseDto getListByUser() throws IOException {
}

// 문제 기반 추천 서비스
public RecResponseDto getListByProblem(int solvedRecentId) throws IOException {
public RecResponseDto getListByProblem(Long solvedRecentId) throws IOException {

UserCrawlingDto dto = getUserInfo();

WebClient webClient = WebClient.builder()
.baseUrl(lambda2)
.build();

ProblemElement problemElement = problemElementRepository.findByProblemId(solvedRecentId).orElseThrow( () -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));
problemElementRepository.findByProblemId(solvedRecentId)
.orElseThrow(() -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));

RecProRequestDto requestDto = RecProRequestDto.builder()
.solvedRecentId(solvedRecentId)
Expand All @@ -95,10 +98,9 @@ public RecResponseDto getListByProblem(int solvedRecentId) throws IOException {
.block();

Integer tierCount;
if(dto.getTier().equals("0")){
if (dto.getTier().equals("0")) {
tierCount = 0;
}
else {
} else {
tierCount = tierCountRepository.findByTier(Integer.parseInt(dto.getTier()));
}
log.info("tier:{}", tierCount);
Expand Down Expand Up @@ -126,55 +128,59 @@ public UserCrawlingDto getUserInfo() throws IOException {
return dto;
}

private Member getCurrentMember(){
private Member getCurrentMember() {
Member currentMember = SecurityUtil.getCurrentMember();
Long currentMemberId = currentMember.getId();
return memberRepository.findById(currentMemberId)
.orElseThrow(() -> new ApiException(ErrorType._USER_NOT_FOUND_DB));
}

private String getTierName(Integer bojLevel){
private String getTierName(Integer bojLevel) {
String[] tiers = {"BRONZE", "SILVER", "GOLD", "PLATINUM", "DIAMOND", "RUBY", "MASTER"};
if(bojLevel <= 5)
if (bojLevel <= 5) {
return tiers[0];
else if(bojLevel <= 10)
} else if (bojLevel <= 10) {
return tiers[1];
else if(bojLevel <= 15)
} else if (bojLevel <= 15) {
return tiers[2];
else if(bojLevel <= 20)
} else if (bojLevel <= 20) {
return tiers[3];
else if(bojLevel <= 25)
} else if (bojLevel <= 25) {
return tiers[4];
else if(bojLevel <= 30)
} else if (bojLevel <= 30) {
return tiers[5];
}

return tiers[6];
}

private List<String> extractWords(String bojTags){
private List<String> extractWords(String bojTags) {
List<String> tags = new ArrayList<>();

Pattern pattern = Pattern.compile("\\b\\w+\\b");
Matcher matcher = pattern.matcher(bojTags);

while(matcher.find()){
while (matcher.find()) {
tags.add(matcher.group());
}

return tags;
}

private List<RecProDetailResponseDto> getProblemDetail(RecProResponseDto proList){
private List<RecProDetailResponseDto> getProblemDetail(RecProResponseDto proList) {
List<String> result = proList.getResult();
List<RecProDetailResponseDto> proDetailResponseDto = new ArrayList<>();

// 문제 세부 정보 찾기
for(int i=0;i<15;i++){
for (int i = 0; i < 15; i++) {
Integer num = Integer.parseInt(result.get(i));
log.info("num:{}", num);
try {
ProblemElement problemElement = problemElementRepository.findByProblemId(Integer.parseInt(String.valueOf(num))).orElseThrow( () -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));
log.info("{}, {}, {}", problemElement.getProblemId(), problemElement.getBojLevel(), problemElement.getBojKey());
ProblemElement problemElement = problemElementRepository.findByProblemId(
Long.parseLong(String.valueOf(num)))
.orElseThrow(() -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));
log.info("{}, {}, {}", problemElement.getProblemId(), problemElement.getBojLevel(),
problemElement.getBojKey());
String tierName = getTierName(problemElement.getBojLevel());
List<String> tags = extractWords(problemElement.getBojKey());
RecProDetailResponseDto detailResponseDto = RecProDetailResponseDto.builder()
Expand All @@ -184,7 +190,7 @@ private List<RecProDetailResponseDto> getProblemDetail(RecProResponseDto proList
.bojTag(tags)
.build();
proDetailResponseDto.add(detailResponseDto);
} catch(Exception e){
} catch (Exception e) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.api.TaveShot.domain.search.service;

import com.api.TaveShot.domain.recommend.domain.ProblemElement;
import com.api.TaveShot.domain.recommend.repository.ProblemElementRepository;
import com.api.TaveShot.domain.search.dto.GoogleItemDto;
import com.api.TaveShot.domain.search.dto.GoogleListResponseDto;
import com.api.TaveShot.domain.search.dto.GoogleResponseDto;
import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -32,9 +33,9 @@ public class SearchService {

public GoogleListResponseDto findBlog(String query, int index) {

String questionNumber = query.replaceAll("[^0-9]", "");
Long questionNumber = extractNumberFromBojString(query);

problemElementRepository.findByProblemId(Integer.parseInt(questionNumber))
problemElementRepository.findByProblemId(questionNumber)
.orElseThrow(() -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));

WebClient webClient = WebClient.builder()
Expand Down Expand Up @@ -66,4 +67,13 @@ public GoogleListResponseDto findBlog(String query, int index) {
.build();

}

public Long extractNumberFromBojString(String input) {
Pattern pattern = Pattern.compile("백준 (\\d+)번");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return Long.parseLong(matcher.group(1));
}
throw new ApiException(ErrorType._PROBLEM_NOT_FOUND);
}
}

0 comments on commit 2a5f692

Please sign in to comment.