From 1b08c9f7b664e823f1017bdf92222a3ff3747501 Mon Sep 17 00:00:00 2001 From: moomint8 Date: Tue, 16 Apr 2024 17:32:43 +0900 Subject: [PATCH] =?UTF-8?q?refactor(Solved):=20Solved=20userId=20token=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit userId를 프론트로부터 제공받는 것이 아닌, 로그인한 사용자의 토큰을 이용해 값을 반환하도록 개선했습니다. --- .../Application/service/AuthService.java | 2 ++ .../Application/service/AuthServiceImpl.java | 5 +++++ .../Application/service/SolvedServiceImpl.java | 11 ++++------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/AuthService.java b/src/main/java/org/triumers/newsnippetback/Application/service/AuthService.java index 0be6ab4..2db1a61 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/AuthService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/AuthService.java @@ -21,4 +21,6 @@ public interface AuthService { void modifyPassword(PasswordDTO passwordDTO) throws WrongPasswordException, WrongInputTypeException; void updateSolvedQuiz(boolean isCorrect); + + public int getUserId(); } diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/AuthServiceImpl.java b/src/main/java/org/triumers/newsnippetback/Application/service/AuthServiceImpl.java index dca8966..022d693 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/AuthServiceImpl.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/AuthServiceImpl.java @@ -113,6 +113,11 @@ public void updateSolvedQuiz(boolean isCorrect) { userRepository.save(user); } + @Override + public int getUserId() { + return userRepository.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName()).getId(); + } + private User userMapper(AuthDTO request) { User user = new User(); diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/SolvedServiceImpl.java b/src/main/java/org/triumers/newsnippetback/Application/service/SolvedServiceImpl.java index 578dccb..410de1f 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/SolvedServiceImpl.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/SolvedServiceImpl.java @@ -41,7 +41,7 @@ public SolvedDTO findSelectedOptionAndCompareAnswer(SolvedRequest solvedRequest) modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); - int userId = solvedRequest.getUserId(); + int userId = authService.getUserId(); int quizId = solvedRequest.getQuizId(); String seletedOption = solvedRequest.getSelectedOption(); @@ -87,7 +87,7 @@ public List findSolvedQuizListByUserId(SolvedRequest solvedRequest) { /* 설명. 사용자의 ID와 문제 ID로 문제 내용 불러오기 */ @Override public SolvedDTO findSolvedQuizByUserIdAndQuizId(SolvedRequest solvedRequest) { - int userId = solvedRequest.getUserId(); + int userId = authService.getUserId(); int quizId = solvedRequest.getQuizId(); Solved solvedQuiz = solvedRepository.findSelectedOptionByUserIdAndQuizId(userId, quizId); @@ -118,7 +118,7 @@ public SolvedDTO findSolvedQuizByUserIdAndQuizId(SolvedRequest solvedRequest) { @Override public List findCorrectQuizByUserIdAndSolvedDate(SolvedResultRequest solvedResultRequest) { - int userId = solvedResultRequest.getUserId(); + int userId = authService.getUserId(); boolean isCorrect = solvedResultRequest.isCorrect(); LocalDate solvedDate = solvedResultRequest.getSolvedDate(); @@ -129,8 +129,7 @@ public List findCorrectQuizByUserIdAndSolvedDate(SolvedResultRequest sol @Override public List findSolvedQuizListByUserIdAndDate(SolvedRequest solvedRequest) { - System.out.println(solvedRequest); - int userId = solvedRequest.getUserId(); + int userId = authService.getUserId(); LocalDate solvedDate = solvedRequest.getSolvedDate(); List solvedList = solvedRepository.findSolvedQuizByUserIdAndSolvedDate(userId, solvedDate); @@ -161,6 +160,4 @@ public List findSolvedQuizListByUserIdAndDate(SolvedRequest solvedReq return solvedDTOList; } - - }