From aca84cc3878d1f9eb12f8ed2a75fa7105027b9a9 Mon Sep 17 00:00:00 2001 From: fing9 Date: Wed, 6 Dec 2023 13:20:07 +0900 Subject: [PATCH] feat : problem recommendation --- ...RecommendationAlreadySolvedController.java | 25 ++++++++++++++++ .../RecommendationAlreadySolvedService.java | 30 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/com/randps/randomdefence/domain/recommendation/controller/RecommendationAlreadySolvedController.java create mode 100644 src/main/java/com/randps/randomdefence/domain/recommendation/service/RecommendationAlreadySolvedService.java diff --git a/src/main/java/com/randps/randomdefence/domain/recommendation/controller/RecommendationAlreadySolvedController.java b/src/main/java/com/randps/randomdefence/domain/recommendation/controller/RecommendationAlreadySolvedController.java new file mode 100644 index 0000000..d5b6bbe --- /dev/null +++ b/src/main/java/com/randps/randomdefence/domain/recommendation/controller/RecommendationAlreadySolvedController.java @@ -0,0 +1,25 @@ +package com.randps.randomdefence.domain.recommendation.controller; + +import com.randps.randomdefence.domain.problem.dto.ProblemSolveJudgedDto; +import com.randps.randomdefence.domain.recommendation.service.RecommendationAlreadySolvedService; +import lombok.RequiredArgsConstructor; +import org.springframework.data.repository.query.Param; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/recommend/solved") +public class RecommendationAlreadySolvedController { + + private final RecommendationAlreadySolvedService recommendationAlreadySolvedService; + + /* + * 유저가 기존에 푼 문제중 하나를 랜덤하게 뽑아온다. + */ + @GetMapping("/") + public ProblemSolveJudgedDto getSolvedProblemRandom(@Param("bojHandle") String bojHandle) { + return recommendationAlreadySolvedService.findOneRandomSolvedProblem(bojHandle); + } +} diff --git a/src/main/java/com/randps/randomdefence/domain/recommendation/service/RecommendationAlreadySolvedService.java b/src/main/java/com/randps/randomdefence/domain/recommendation/service/RecommendationAlreadySolvedService.java new file mode 100644 index 0000000..2a702d4 --- /dev/null +++ b/src/main/java/com/randps/randomdefence/domain/recommendation/service/RecommendationAlreadySolvedService.java @@ -0,0 +1,30 @@ +package com.randps.randomdefence.domain.recommendation.service; + +import com.randps.randomdefence.domain.problem.dto.ProblemDto; +import com.randps.randomdefence.domain.problem.dto.ProblemSolveJudgedDto; +import com.randps.randomdefence.domain.problem.service.ProblemService; +import com.randps.randomdefence.domain.user.domain.UserAlreadySolved; +import com.randps.randomdefence.domain.user.domain.UserAlreadySolvedRepository; +import java.util.List; +import java.util.NoSuchElementException; +import javax.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class RecommendationAlreadySolvedService { + + private final ProblemService problemService; + + private final UserAlreadySolvedRepository userAlreadySolvedRepository; + + @Transactional + public ProblemSolveJudgedDto findOneRandomSolvedProblem(String bojHandle) { + UserAlreadySolved userAlreadySolved = userAlreadySolvedRepository.findByBojHandle(bojHandle).orElseThrow(() -> new NoSuchElementException("유저가 기존에 푼 문제가 존재하지 않습니다.")); + List solvedProblems = userAlreadySolved.getAlreadySolvedList(); + ProblemDto problemDto = problemService.findProblem(solvedProblems.get((int)Math.round(Math.random() * (solvedProblems.size() - 1)))); + return new ProblemSolveJudgedDto(problemDto, true); + } + +}