|
| 1 | +package com.back.domain.user.service; |
| 2 | + |
| 3 | +import com.back.domain.user.entity.User; |
| 4 | +import com.back.domain.user.repository.UserRepository; |
| 5 | +import com.back.global.exception.ServiceException; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import org.springframework.transaction.annotation.Transactional; |
| 9 | + |
| 10 | +@Service |
| 11 | +@RequiredArgsConstructor |
| 12 | +public class AbvScoreService { |
| 13 | + |
| 14 | + private final UserRepository userRepository; |
| 15 | + |
| 16 | + private static final double POST_SCORE = 0.5; |
| 17 | + private static final double COMMENT_SCORE = 0.2; |
| 18 | + private static final double LIKE_SCORE = 0.1; |
| 19 | + private static final double KEEP_SCORE = 0.1; |
| 20 | + |
| 21 | + @Transactional |
| 22 | + public void awardForPost(Long userId) { |
| 23 | + addScore(userId, POST_SCORE); |
| 24 | + } |
| 25 | + |
| 26 | + @Transactional |
| 27 | + public void revokeForPost(Long userId) { |
| 28 | + addScore(userId, -POST_SCORE); |
| 29 | + } |
| 30 | + |
| 31 | + @Transactional |
| 32 | + public void awardForComment(Long userId) { |
| 33 | + addScore(userId, COMMENT_SCORE); |
| 34 | + } |
| 35 | + |
| 36 | + @Transactional |
| 37 | + public void revokeForComment(Long userId) { |
| 38 | + addScore(userId, -COMMENT_SCORE); |
| 39 | + } |
| 40 | + |
| 41 | + @Transactional |
| 42 | + public void awardForLike(Long userId) { |
| 43 | + addScore(userId, LIKE_SCORE); |
| 44 | + } |
| 45 | + |
| 46 | + @Transactional |
| 47 | + public void revokeForLike(Long userId) { |
| 48 | + addScore(userId, -LIKE_SCORE); |
| 49 | + } |
| 50 | + |
| 51 | + @Transactional |
| 52 | + public void awardForKeep(Long userId) { |
| 53 | + addScore(userId, KEEP_SCORE); |
| 54 | + } |
| 55 | + |
| 56 | + @Transactional |
| 57 | + public void revokeForKeep(Long userId) { |
| 58 | + addScore(userId, -KEEP_SCORE); |
| 59 | + } |
| 60 | + |
| 61 | + private void addScore(Long userId, double delta) { |
| 62 | + User user = userRepository.findById(userId) |
| 63 | + .orElseThrow(() -> new ServiceException(404, "사용자를 찾을 수 없습니다.")); |
| 64 | + |
| 65 | + Double current = user.getAbvDegree(); |
| 66 | + if (current == null) current = 5.0; // 신규 사용자는 5%부터 시작 |
| 67 | + |
| 68 | + double next = clamp(current + delta, 0.0, 100.0); |
| 69 | + user.setAbvDegree(next); |
| 70 | + |
| 71 | + userRepository.save(user); |
| 72 | + } |
| 73 | + |
| 74 | + private static double clamp(double v, double min, double max) { |
| 75 | + return Math.max(min, Math.min(max, v)); |
| 76 | + } |
| 77 | +} |
0 commit comments