-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
141 lines (114 loc) · 5.77 KB
/
UserService.java
File metadata and controls
141 lines (114 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.example.enjoy.service.userService;
import com.amazonaws.services.cloudformation.model.AlreadyExistsException;
import com.example.enjoy.dto.AddManualCourseRequest;
import com.example.enjoy.dto.StudentCourseStatus;
import com.example.enjoy.dto.loginDto.MemberDto;
import com.example.enjoy.entity.StudentCourse;
import com.example.enjoy.entity.Track;
import com.example.enjoy.entity.TrackCourse;
import com.example.enjoy.entity.user.User;
import com.example.enjoy.repository.StudentCourseRepository;
import com.example.enjoy.repository.TrackCourseRepository;
import com.example.enjoy.repository.TrackRepository;
import com.example.enjoy.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserService {
private final StudentCourseRepository studentCourseRepository;
private final TrackRepository trackRepository;
private final TrackCourseRepository trackCourseRepository;
private final UserRepository userRepository;
@Transactional
public void addManualCourse(AddManualCourseRequest request) { //수동으로 과목 등록
if (studentCourseRepository.existsByStudentIdAndCourseName(
request.getStudentId(),
request.getCourseName())) {
throw new AlreadyExistsException("이미 등록된 과목입니다.");
}
StudentCourse sc = StudentCourse.builder() //수강 여부 설정 가능
.studentId(request.getStudentId())
.courseName(request.getCourseName())
.status(request.getStatus())
.manual(true)
.createdAt(LocalDateTime.now())
.build();
studentCourseRepository.save(sc);
}
public List<StudentCourse> getManualCourses(String studentId) { //수동 등록 과목 조회
List<StudentCourse> manualCourses = studentCourseRepository.findAllByStudentIdAndManualIsTrue(studentId);
if (manualCourses.isEmpty()) {
throw new IllegalArgumentException("수동 등록된 과목이 없습니다.");
}
return manualCourses;
}
@Transactional
public void saveUserInfo(MemberDto memberDto) {
User user = userRepository.findByStudentId(memberDto.getStudentIdString())
.orElse(new User());
user.updateUserInfo(
memberDto.getStudentIdString(),
memberDto.getStudentName(),
memberDto.getMajor(),
memberDto.getGrade(),
memberDto.getCompletedSemester()
);
userRepository.save(user);
}
@Transactional
public void removeManualCourse(String studentId, String courseName) { //수동 등록 과목 삭제
StudentCourse course = studentCourseRepository.findByStudentIdAndCourseNameAndManualIsTrue(studentId, courseName)
.orElseThrow(() -> new IllegalArgumentException("수동 등록된 과목을 찾을 수 없습니다."));
studentCourseRepository.delete(course);
}
public List<StudentCourse> getCompletedCourses(String studentId) { //수강 완료 과목 조회
return studentCourseRepository.findAllByStudentIdAndStatus(studentId, StudentCourseStatus.COMPLETED);
}
public Map<Track, Double> getTrackProgress(String studentId) { //트랙별 진행률 조회
List<Track> allTracks = trackRepository.findAll();
List<StudentCourse> completedCourses = getCompletedCourses(studentId);
return allTracks.stream().collect(Collectors.toMap(
track -> track,
track -> calculateTrackProgress(track, completedCourses)
));
}
public List<StudentCourse> getPlannedCourses(String studentId) { //수강 예정 과목 조회
return studentCourseRepository.findAllByStudentIdAndStatus(studentId, StudentCourseStatus.PLANNED);
}
public List<StudentCourse> getInProgressCourses(String studentId) { //수강 중인 과목 조회
return studentCourseRepository.findAllByStudentIdAndStatus(studentId, StudentCourseStatus.IN_PROGRESS);
}
private double calculateTrackProgress(Track track, List<StudentCourse> completedCourses) {
List<TrackCourse> trackCourses = trackCourseRepository.findAllByTrack(track);
if (trackCourses.isEmpty()) return 0.0;
long completedCount = trackCourses.stream()
.filter(trackCourse -> completedCourses.stream()
.anyMatch(completed -> completed.getCourseName().equals(trackCourse.getCourseName())))
.count();
return (double) completedCount / trackCourses.size() * 100;
}
@Transactional
public void updateCourseStatus(String studentId, String courseName, StudentCourseStatus newStatus) {
StudentCourse course = studentCourseRepository
.findByStudentIdAndCourseName(studentId, courseName)
.orElseThrow(() -> new IllegalArgumentException("등록된 과목을 찾을 수 없습니다."));
course.updateStatus(newStatus);
}
public List<Track> getCompletedTracks (String studentId) {
List<StudentCourse> completedCourses = getCompletedCourses(studentId);
List<TrackCourse> trackCourses = trackCourseRepository.findAll();
return trackCourses.stream()
.filter(trackCourse -> completedCourses.stream()
.anyMatch(course -> course.getCourseName().equals(trackCourse.getCourseName())))
.map(TrackCourse::getTrack)
.distinct()
.collect(Collectors.toList());
}
}