Skip to content

Commit d65550c

Browse files
authored
Merge pull request #2 from enjoy-hack/jiwoo
Jiwoo
2 parents 7ae1086 + def0c5b commit d65550c

10 files changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.smartair.controller.homecontroller;
2+
3+
import ch.qos.logback.core.model.Model;
4+
import com.example.smartair.dto.TrackProgressDto;
5+
import com.example.smartair.service.TrackService;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
10+
import java.util.List;
11+
12+
@Controller
13+
@RequiredArgsConstructor
14+
public class HomeController {
15+
16+
private final TrackService trackService;
17+
18+
@GetMapping("/home")
19+
public String showMyProgress(Model model) { // 1. Model 객체를 파라미터로 추가
20+
// TODO: 추후 Spring Security 등과 연동하여 실제 로그인한 사용자 ID를 가져와야 함
21+
Long currentStudentId = 1L; // 2. 테스트용 임시 학생 ID 사용
22+
23+
// 3. 학생의 이수 현황을 계산하는 새로운 서비스 메서드 호출
24+
List<TrackProgressDto> progressData = trackService.calculateTrackProgress(currentStudentId);
25+
26+
// 4. 조회된 데이터를 "progressData"라는 이름으로 모델에 추가
27+
//model.addAttribute("progressData", progressData);
28+
29+
// 5. 데이터를 표시할 뷰(html)의 이름을 반환
30+
return "home";
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.smartair.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
/**
8+
* 교과목 정보를 담는 DTO
9+
*/
10+
@Getter
11+
@Setter
12+
@AllArgsConstructor // 모든 필드를 인자로 받는 생성자 자동 생성
13+
public class CourseDto {
14+
15+
private String courseName; // 현재 과목명
16+
private String courseAlias; // 과거 과목명 (없으면 null)
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.smartair.dto;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import java.util.List;
6+
7+
/**
8+
* 하나의 트랙에 대한 학생의 이수 진행 현황 정보를 담는 DTO
9+
*/
10+
@Getter
11+
@Setter
12+
public class TrackProgressDto {
13+
14+
private String trackName; // 트랙 이름 (예: "AI 콘텐츠")
15+
private String department; // 소속 학과
16+
private int completedCount; // 이수한 과목 수
17+
private int requiredCount; // 이수 필요 과목 수 (예: 6)
18+
private boolean isCompleted; // 트랙 이수 완료 여부
19+
20+
private List<CourseDto> completedCourses; // 이수한 과목 목록
21+
private List<CourseDto> remainingCourses; // 이수해야 할 남은 과목 목록
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.smartair.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import lombok.Getter;
8+
9+
@Entity
10+
@Getter
11+
public class StudentCourse {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
private Long studentId; // 학생 ID (로그인 연동)
16+
private String courseName;
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.smartair.entity;
2+
3+
import jakarta.persistence.*;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
@Entity
11+
@Getter @Setter
12+
public class Track {
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private Long id;
16+
private String name;
17+
private String department;
18+
19+
@OneToMany(mappedBy = "track", cascade = CascadeType.ALL)
20+
private List<TrackCourse> courses = new ArrayList<>();
21+
// public void addCourse(TrackCourse course) {
22+
// courses.add(course);
23+
// course.setTrack(this);
24+
// }
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.smartair.entity;
2+
3+
import jakarta.persistence.*;
4+
import lombok.Getter;
5+
6+
@Entity
7+
@Getter
8+
public class TrackCourse {
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
private Long id;
12+
private String courseName; // 현재 과목명
13+
private String courseAlias; // 구(과거) 과목명
14+
15+
@ManyToOne
16+
@JoinColumn(name = "track_id")
17+
private Track track;
18+
// Getters and Setters
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.smartair.repository;
2+
3+
import com.example.smartair.entity.StudentCourse;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
import java.util.List;
8+
9+
public interface StudentCourseRepository extends JpaRepository<StudentCourse, Long> {
10+
11+
List<StudentCourse> findByStudentId(Long studentId);
12+
13+
@Transactional
14+
void deleteByStudentId(Long studentId); // 학생 ID로 이수과목 한번에 삭제
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.smartair.repository;
2+
3+
import com.example.smartair.entity.TrackCourse;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface TrackCourseRepository extends JpaRepository<TrackCourse, Long> {
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.smartair.repository;
2+
3+
import com.example.smartair.entity.Track;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import java.util.List;
7+
8+
public interface TrackRepository extends JpaRepository<Track, Long> {
9+
10+
@Query("SELECT DISTINCT t FROM Track t LEFT JOIN FETCH t.courses")
11+
List<Track> findAllWithCourses();
12+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.example.smartair.service;
2+
3+
import com.example.smartair.dto.CourseDto;
4+
import com.example.smartair.dto.TrackProgressDto;
5+
import com.example.smartair.entity.StudentCourse;
6+
import com.example.smartair.entity.Track;
7+
import com.example.smartair.entity.TrackCourse;
8+
import com.example.smartair.repository.StudentCourseRepository;
9+
import com.example.smartair.repository.TrackRepository;
10+
import org.springframework.stereotype.Service;
11+
import lombok.RequiredArgsConstructor;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Set;
17+
import java.util.stream.Collectors;
18+
19+
@Service
20+
@RequiredArgsConstructor
21+
public class TrackService {
22+
23+
private final TrackRepository trackRepository;
24+
private final StudentCourseRepository studentCourseRepository; // 기존 기능
25+
26+
// ... (기존의 saveStudentCoursesFromExcel, calculateTrackProgress 메서드) ...
27+
28+
/**
29+
* 모든 트랙 정보를 학과별로 그룹화하여 반환하는 메서드
30+
*/
31+
public Map<String, List<Track>> getAllTracksGroupedByDepartment() {
32+
// 1. DB에서 모든 트랙과 관련 과목들을 한번에 조회
33+
List<Track> allTracks = trackRepository.findAllWithCourses();
34+
35+
// 2. 조회된 트랙 리스트를 '학과' 이름으로 그룹화하여 Map으로 변환 후 반환
36+
return allTracks.stream()
37+
.collect(Collectors.groupingBy(Track::getDepartment));
38+
}
39+
40+
public List<TrackProgressDto> calculateTrackProgress(Long studentId) {
41+
// 1. 학생의 이수 과목 목록 조회
42+
Set<String> completedCourseNames = studentCourseRepository.findByStudentId(studentId)
43+
.stream()
44+
.map(StudentCourse::getCourseName)
45+
.collect(Collectors.toSet());
46+
47+
// 2. 모든 트랙 정보 조회
48+
List<Track> allTracks = trackRepository.findAllWithCourses();
49+
50+
List<TrackProgressDto> progressList = new ArrayList<>();
51+
52+
// 3. 각 트랙별로 진행 현황 계산
53+
for (Track track : allTracks) {
54+
55+
// 현재 트랙에서 완료한 과목과 남은 과목을 담을 리스트 초기화
56+
List<CourseDto> completedInThisTrack = new ArrayList<>();
57+
List<CourseDto> remainingInThisTrack = new ArrayList<>();
58+
59+
// 현재 트랙에 속한 모든 교과목을 하나씩 확인
60+
for (TrackCourse trackCourse : track.getCourses()) {
61+
62+
// 학생이 해당 과목을 이수했는지 확인 (현재 이름 또는 과거 이름으로 체크)
63+
if (completedCourseNames.contains(trackCourse.getCourseName()) ||
64+
(trackCourse.getCourseAlias() != null && completedCourseNames.contains(trackCourse.getCourseAlias()))) {
65+
// 이수한 경우: 완료 리스트에 추가
66+
completedInThisTrack.add(new CourseDto(trackCourse.getCourseName(), trackCourse.getCourseAlias()));
67+
} else {
68+
// 이수하지 않은 경우: 남은 과목 리스트에 추가
69+
remainingInThisTrack.add(new CourseDto(trackCourse.getCourseName(), trackCourse.getCourseAlias()));
70+
}
71+
}
72+
73+
// 최종 결과를 담을 DTO 객체 생성 및 데이터 세팅
74+
TrackProgressDto progressDto = new TrackProgressDto();
75+
progressDto.setTrackName(track.getName());
76+
progressDto.setDepartment(track.getDepartment());
77+
78+
int completedCount = completedInThisTrack.size();
79+
progressDto.setCompletedCount(completedCount);
80+
progressDto.setRequiredCount(6); // 트랙 이수 요구 과목 수는 6개
81+
progressDto.setCompleted(completedCount >= 6); // 6개 이상이면 true
82+
83+
progressDto.setCompletedCourses(completedInThisTrack);
84+
progressDto.setRemainingCourses(remainingInThisTrack);
85+
86+
// 완성된 DTO를 최종 결과 리스트에 추가
87+
progressList.add(progressDto);
88+
}
89+
90+
return progressList;
91+
}
92+
}

0 commit comments

Comments
 (0)