Skip to content
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

[Feat] 푸시 알람 변경 기능, 레벨업, 목록 조회 api Getmapping으로 수정 #48

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -33,7 +34,7 @@ public ResponseEntity<ApiResponse<?>> saveLearningSetsByExcel() {
}

@Operation(summary = "레벨별 학습 세트 조회", description = "레벨별 전체 학습 세트를 조회합니다.")
@PostMapping
@GetMapping
public ResponseEntity<ApiResponse<Object>> getLearningSets(
final @AuthenticationPrincipal CustomUserDetails currentUser) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.ripple.BE.learning.service.learningset.LearningSetService;
import com.ripple.BE.user.domain.User;
import com.ripple.BE.user.domain.type.Level;
import com.ripple.BE.user.service.UserProgressService;
import com.ripple.BE.user.service.UserService;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,6 +30,7 @@ public class ConceptService {

private final LearningSetService learningSetService;
private final UserService userService;
private final UserProgressService userProgressService;

private final UserLearningSetRepository userLearningSetRepository;
private final ConceptRepository conceptRepository;
Expand Down Expand Up @@ -69,6 +71,8 @@ public void completeConceptLearning(
if (!userLearningSet.isConceptCompleted()) {
userLearningSet.setConceptCompleted();
userService.updateCompletedCountByLevel(user, level);

userProgressService.updateLevel(user);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.ripple.BE.learning.service.learningset.LearningSetService;
import com.ripple.BE.user.domain.User;
import com.ripple.BE.user.domain.type.Level;
import com.ripple.BE.user.service.UserProgressService;
import com.ripple.BE.user.service.UserService;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -44,6 +45,7 @@ public class QuizService {
private final QuizRedisService quizRedisService;
private final LearningSetService learningSetService;
private final UserService userService;
private final UserProgressService userProgressService;

private static final String QUESTION_TYPE = "questions";
private static final String WRONG_ANSWER_TYPE = "wrongAnswer";
Expand Down Expand Up @@ -138,6 +140,8 @@ public void finishQuiz(final long userId, final long learningSetId, final Level
userLearningSet.setQuizCompleted(); // 퀴즈 완료 처리
userService.updateUserStatsAfterQuiz(
user, level, failQuizList, quizCount, correctCount); // 사용자 통계 업데이트

userProgressService.updateLevel(user); // 레벨 업데이트
}

quizRedisService.clearRedisKeys(userId); // 퀴즈 진행 관련 데이터 삭제
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/ripple/BE/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public ResponseEntity<ApiResponse<?>> profile(
return ResponseEntity.status(HttpStatus.OK).body(ApiResponse.EMPTY_RESPONSE);
}

@Operation(summary = "푸시 알림 설정", description = "로그인 후 유저의 푸시 알림 설정을 변경합니다.")
@PostMapping("/alarm")
public ResponseEntity<ApiResponse<?>> alarm(
@RequestParam boolean alarm, @AuthenticationPrincipal CustomUserDetails customUserDetails) {

userService.updateAlarm(alarm, customUserDetails.getId());

return ResponseEntity.status(HttpStatus.OK).body(ApiResponse.EMPTY_RESPONSE);
}

@Operation(summary = "내가 쓴 게시물 조회", description = "로그인한 유저가 작성한 게시물을 조회합니다.")
@GetMapping("/posts")
public ResponseEntity<ApiResponse<Object>> getMyPosts(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/ripple/BE/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Table(name = "users")
@Getter
Expand Down Expand Up @@ -99,6 +100,7 @@ public class User extends BaseEntity {
@Column(name = "is_learning_alarm_allowed")
private boolean isLearningAlarmAllowed = false; // 학습 푸시 알람 여부

@Setter
@Column(name = "is_community_alarm_allowed")
private boolean isCoummunityAlarmAllowed = false; // 커뮤니티 푸시 알람 여부

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/ripple/BE/user/service/UserProgressService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.ripple.BE.user.service;

import com.ripple.BE.learning.domain.learningset.UserLearningSet;
import com.ripple.BE.learning.repository.concept.ConceptRepository;
import com.ripple.BE.learning.repository.learningSet.UserLearningSetRepository;
import com.ripple.BE.learning.repository.quiz.QuizRepository;
import com.ripple.BE.user.domain.User;
import com.ripple.BE.user.domain.type.Level;
import com.ripple.BE.user.dto.ProgressDTO;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,6 +23,7 @@ public class UserProgressService {

private final UserService userService;

private final UserLearningSetRepository userLearningSetRepository;
private final ConceptRepository conceptRepository;
private final QuizRepository quizRepository;

Expand Down Expand Up @@ -47,4 +51,20 @@ public ProgressDTO getLearningSetCompletionRate(final long userId) {
level ->
(double) user.getCompletedCountByLevel(level) / totalSets.get(level))));
}

@Transactional
public void updateLevel(final User user) {
List<UserLearningSet> userLearningSets =
userLearningSetRepository.findByUserIdAndLevel(user.getId(), user.getCurrentLevel());

if (!userLearningSets.isEmpty()) {
if (userLearningSets.stream().allMatch(UserLearningSet::isLearningSetCompleted)) {
if (user.getCurrentLevel() == Level.BEGINNER) {
user.updateLevel(Level.INTERMEDIATE);
} else if (user.getCurrentLevel() == Level.INTERMEDIATE) {
user.updateLevel(Level.ADVANCED);
}
}
}
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/ripple/BE/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class UserService {

private final UserRepository userRepository;

private final PasswordEncoder passwordEncoder;

@Transactional
Expand Down Expand Up @@ -97,7 +98,12 @@ public void updateUserStatsAfterQuiz(
@Transactional
public void updateCompletedCountByLevel(User user, Level level) {
user.increaseCompletedCountByLevel(level);
}

@Transactional
public void updateAlarm(final boolean alarm, final long userId) {

/** TODO: 레벨 업 조건 확인 후 레벨 업 처리 만약 상위 조건의 학습 세트를 완료한 경우 하위 레벨의 학습 세트가 완료되어야지 레벨 업이 가능하다. */
User user = findUserById(userId);
user.setCoummunityAlarmAllowed(alarm);
}
}