-
Notifications
You must be signed in to change notification settings - Fork 2
[FEAT] 학습 도메인 api 기본 구현 #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7cedf61
feat: 학습 결과 저장 API 기본 구조 구현 (#32)
rkdehdrbs7885-oss 647cb26
Merge remote-tracking branch 'origin/develop' into feat/#32-learning-…
rkdehdrbs7885-oss e8dda65
feat: 학습 진행률 조회 api 구현 (#32)
rkdehdrbs7885-oss 74f7103
refactor: 유저 임시 사항 변경 (#32)
rkdehdrbs7885-oss 7fc950d
refactor: 코드 래빗 충돌 수정 (#32)
rkdehdrbs7885-oss 04faef4
Merge branch 'develop' into feat/#32-learning-status-api
rkdehdrbs7885-oss 3bf01a7
refactor: 충돌 수정_2 (#32)
rkdehdrbs7885-oss 6865798
refactor: 충돌 수정_3 (#32)
rkdehdrbs7885-oss 15b0fb4
refactor: 팀 리뷰 수정_1 (#32)
rkdehdrbs7885-oss bbe2d76
refactor: 팀 리뷰 수정_2 (#32)
rkdehdrbs7885-oss 4a2cbd4
refactor: 코드 래빗 리뷰 수정_1 (#32)
rkdehdrbs7885-oss ed2975d
refactor: 불필요한 join 제거 (#32)
rkdehdrbs7885-oss 47f7de2
refactor: 팀 리뷰 수정_3 (#32)
rkdehdrbs7885-oss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/mr/domain/learning/controller/LearningController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.mr.domain.learning.controller; | ||
|
|
||
| import com.mr.domain.learning.dto.req.LearningResultSaveRequestDTO; | ||
| import com.mr.domain.learning.dto.res.LearningProgressResponseDTO; | ||
| import com.mr.domain.learning.dto.res.LearningResultResponseDTO; | ||
| import com.mr.domain.learning.service.LearningService; | ||
| import com.mr.global.apipayload.ApiResponse; | ||
| import com.mr.global.security.principal.CustomUserDetails; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/learnings") | ||
| public class LearningController { | ||
|
|
||
| private final LearningService learningService; | ||
|
|
||
| // 학습 결과 저장 | ||
| @PostMapping("/{learningId}/result") | ||
| public ApiResponse<LearningResultResponseDTO.SaveResultResultDTO> saveLearningResult( | ||
| @AuthenticationPrincipal CustomUserDetails userDetails, | ||
| @PathVariable Long learningId, | ||
| @Valid @RequestBody LearningResultSaveRequestDTO.SaveResultDTO request | ||
| ){ | ||
| LearningResultResponseDTO.SaveResultResultDTO response = learningService.saveResult(userDetails.getUserId(), learningId, request); | ||
|
|
||
| return ApiResponse.onSuccess(response); | ||
| } | ||
|
|
||
| // 학습 진행률 조회 | ||
| @GetMapping("/{learningId}/progress") | ||
| public ApiResponse<LearningProgressResponseDTO.ProgressResultDTO> getLearningProgress( | ||
| @AuthenticationPrincipal CustomUserDetails userDetails, | ||
| @PathVariable Long learningId | ||
| ) { | ||
| LearningProgressResponseDTO.ProgressResultDTO result = learningService.getLearningProgress(userDetails.getUserId(), learningId); | ||
| return ApiResponse.onSuccess(result); | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/main/java/com/mr/domain/learning/dto/req/LearningResultSaveRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.mr.domain.learning.dto.req; | ||
|
|
||
| import jakarta.validation.constraints.Max; | ||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public class LearningResultSaveRequestDTO { | ||
| public record SaveResultDTO( | ||
| @NotNull(message = "점수는 필수 입력값입니다.") | ||
| @Min(value = 0, message = "점수는 0점 이상이어야 합니다.") | ||
| @Max(value = 100, message = "점수는 100점 이하여야 합니다.") | ||
| Integer score, | ||
|
|
||
| @NotNull(message = "학습 스텝 ID는 필수 입력값입니다.") | ||
| Long learningStepId | ||
| ) {} | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/mr/domain/learning/dto/res/LearningProgressResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.mr.domain.learning.dto.res; | ||
|
|
||
| public class LearningProgressResponseDTO { | ||
|
|
||
| public record ProgressResultDTO( | ||
| Long learningId, | ||
| Integer progressRate | ||
| ){ | ||
| public static ProgressResultDTO of(Long learningId, Integer progressRate){ | ||
| return new ProgressResultDTO(learningId, progressRate); | ||
| } | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/mr/domain/learning/dto/res/LearningResultResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.mr.domain.learning.dto.res; | ||
|
|
||
| import com.mr.domain.learning.entity.UserLearningProgress; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class LearningResultResponseDTO { | ||
| public record SaveResultResultDTO( | ||
| Long userLearningProgressId, | ||
| Long userId, | ||
| Long learningId, | ||
| String status, | ||
| Integer score, | ||
| LocalDateTime completedAt | ||
| ) { | ||
| public static SaveResultResultDTO from(UserLearningProgress progress) { | ||
| return new SaveResultResultDTO( | ||
| progress.getId(), | ||
| progress.getUser().getUserId(), | ||
| progress.getLearning().getId(), | ||
| progress.getLearningStatus(), | ||
| progress.getScore(), | ||
| progress.getLastStudiedAt() | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/mr/domain/learning/repository/LearningRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.mr.domain.learning.repository; | ||
|
|
||
| import com.mr.domain.learning.entity.Learning; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface LearningRepository extends JpaRepository<Learning, Long> { | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/mr/domain/learning/repository/LearningStepRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.mr.domain.learning.repository; | ||
|
|
||
| import com.mr.domain.learning.entity.LearningStep; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface LearningStepRepository extends JpaRepository<LearningStep, Long> { | ||
| // 특정 학습에 속한 전체 단계 수 조회 | ||
| long countByLearningId(Long learningId); | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/mr/domain/learning/repository/UserLearningProgressRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.mr.domain.learning.repository; | ||
|
|
||
| import com.mr.domain.learning.entity.UserLearningProgress; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface UserLearningProgressRepository extends JpaRepository<UserLearningProgress, Long> { | ||
|
|
||
| Optional<UserLearningProgress> findByUser_UserIdAndLearningStep_Id(Long userId, Long learningStepId); | ||
| // 유저가 완료한(점수 90점 이상) 학습 단계 수 조회 | ||
| @Query("SELECT COUNT(ulp) FROM UserLearningProgress ulp " + | ||
| "WHERE ulp.user.userId = :userId AND ulp.learning.id = :learningId AND ulp.score >= 90") | ||
| long countCompletedStepsByUserIdAndLearningId(@Param("userId") Long userId, @Param("learningId") Long learningId); | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/main/java/com/mr/domain/learning/service/LearningService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.mr.domain.learning.service; | ||
|
|
||
| import com.mr.domain.learning.dto.req.LearningResultSaveRequestDTO; | ||
| import com.mr.domain.learning.dto.res.LearningProgressResponseDTO; | ||
| import com.mr.domain.learning.dto.res.LearningResultResponseDTO; | ||
| import com.mr.domain.learning.entity.Learning; | ||
| import com.mr.domain.learning.entity.LearningStep; | ||
| import com.mr.domain.learning.entity.UserLearningProgress; | ||
| import com.mr.domain.learning.exception.LearningErrorStatus; | ||
| import com.mr.domain.learning.repository.LearningRepository; | ||
| import com.mr.domain.learning.repository.LearningStepRepository; | ||
| import com.mr.domain.learning.repository.UserLearningProgressRepository; | ||
| import com.mr.domain.user.entity.User; | ||
| import com.mr.domain.user.exception.UserErrorStatus; | ||
| import com.mr.domain.user.repository.UserRepository; | ||
| import com.mr.global.apipayload.exception.GeneralException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class LearningService { | ||
|
|
||
| private final UserLearningProgressRepository userLearningProgressRepository; | ||
| private final LearningStepRepository learningStepRepository; | ||
| private final LearningRepository learningRepository; | ||
| // 임시 작명 | ||
| private final UserRepository userRepository; | ||
|
|
||
| // 학습 결과 저장 | ||
| @Transactional | ||
| public LearningResultResponseDTO.SaveResultResultDTO saveResult( | ||
|
rkdehdrbs7885-oss marked this conversation as resolved.
|
||
| Long userId, | ||
| Long learningId, | ||
| LearningResultSaveRequestDTO.SaveResultDTO request | ||
| ){ | ||
| User user = userRepository.findById(userId) | ||
| .orElseThrow(()-> new GeneralException(UserErrorStatus.USER_NOT_FOUND)); | ||
|
|
||
| Learning learning = learningRepository.findById(learningId) | ||
| .orElseThrow(() -> new GeneralException(LearningErrorStatus.LEARNING_NOT_FOUND)); | ||
|
|
||
| LearningStep learningStep = learningStepRepository.findById(request.learningStepId()) | ||
| .orElseThrow(() -> new GeneralException(LearningErrorStatus.LEARNING_STEP_NOT_FOUND)); | ||
|
|
||
| learningStep.validateBelongsTo(learning); | ||
|
|
||
| UserLearningProgress progress = userLearningProgressRepository | ||
| .findByUser_UserIdAndLearningStep_Id(userId, request.learningStepId()) | ||
| .map(p -> { | ||
| p.updateProgress(request.score(), LocalDateTime.now()); | ||
| return p; | ||
| }) | ||
| .orElseGet(() -> { | ||
| UserLearningProgress newProgress = UserLearningProgress.create(user, learning, learningStep); | ||
| newProgress.updateProgress(request.score(), LocalDateTime.now()); | ||
| return userLearningProgressRepository.save(newProgress); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| return LearningResultResponseDTO.SaveResultResultDTO.from(progress); | ||
| } | ||
|
|
||
| // 학습 진행률 조회 로직 | ||
| public LearningProgressResponseDTO.ProgressResultDTO getLearningProgress( | ||
| Long userId, | ||
| Long learningId | ||
| ) { | ||
| // 학습 존재 여부 확인 | ||
| if (!learningRepository.existsById(learningId)) { | ||
| throw new GeneralException(LearningErrorStatus.LEARNING_NOT_FOUND); | ||
| } | ||
| // 전체 학습 단계 수 조회 | ||
| long totalStepCount = learningStepRepository.countByLearningId(learningId); | ||
|
|
||
| // 완료한 학습 단계 수 조회 | ||
| long completedStepCount = userLearningProgressRepository.countCompletedStepsByUserIdAndLearningId(userId, learningId); | ||
|
|
||
| // 진행률 계산 (0으로 나누기 예외 방지) | ||
| int progressRate = totalStepCount == 0 ? 0 : (int) Math.round((double) completedStepCount / totalStepCount * 100); | ||
|
|
||
| return LearningProgressResponseDTO.ProgressResultDTO.of(learningId, progressRate); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/mr/domain/user/repository/UserRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.mr.domain.user.repository; | ||
|
|
||
| import com.mr.domain.user.entity.User; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface UserRepository extends JpaRepository<User, Long> { | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.