Skip to content

Commit fc52775

Browse files
committed
Lab6 - done
1 parent 63f2098 commit fc52775

File tree

7 files changed

+480
-5
lines changed

7 files changed

+480
-5
lines changed
Binary file not shown.

lab06/school/school.mv.db

16 KB
Binary file not shown.

lab06/school/school.trace.db

+431
Large diffs are not rendered by default.

lab06/school/src/main/java/pl/edu/agh/to/school/course/CourseController.java

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import java.util.ArrayList;
1111
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collector;
14+
import java.util.stream.Collectors;
1215

1316
@RestController
1417
@RequestMapping(path = "courses")
@@ -35,4 +38,14 @@ public List<Student> getAssignedStudents(@PathVariable String id) {
3538
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "course not found");
3639
}
3740
}
41+
42+
@GetMapping(path = "{id}/allMean")
43+
public Map<Student, Float> getMeanForAllStudents(@PathVariable String id) {
44+
try {
45+
Long idLong = Long.parseLong(id);
46+
return this.courseService.getMeanForAllStudents(idLong);
47+
} catch (NumberFormatException e) {
48+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "course not found");
49+
}
50+
}
3851
}

lab06/school/src/main/java/pl/edu/agh/to/school/course/CourseRepository.java

+7
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ public interface CourseRepository extends JpaRepository<Course, Long> {
1414
@Query("SELECT s FROM Student s " +
1515
"WHERE s.id IN (SELECT student.id FROM Course c, IN(c.students) student WHERE c.id = :courseId)")
1616
List<Student> getAssignedStudents(@Param("courseId") Long courseId);
17+
18+
@Query(value = "SELECT STUDENT.ID, BIRTH_DATE, FIRST_NAME, INDEX_NUMBER, LAST_NAME, AVG(GRADE_VALUE) FROM STUDENT_GRADES \n" +
19+
"JOIN STUDENT ON STUDENT.ID = STUDENT_GRADES.STUDENT_ID\n" +
20+
"JOIN GRADE ON GRADE.ID = STUDENT_GRADES.GRADES_ID\n" +
21+
"WHERE COURSE_ID = 4\n" +
22+
"GROUP BY STUDENT.ID", nativeQuery = true)
23+
List<List<Object>> getMeanForAllStudents(@Param("courseId") Long courseId);
1724
}

lab06/school/src/main/java/pl/edu/agh/to/school/course/CourseService.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import org.springframework.stereotype.Service;
44
import pl.edu.agh.to.school.student.Student;
5+
import pl.edu.agh.to.school.student.StudentRepository;
6+
import pl.edu.agh.to.school.student.StudentService;
57

6-
import java.util.ArrayList;
7-
import java.util.List;
8-
import java.util.Optional;
8+
import java.math.BigDecimal;
9+
import java.math.BigInteger;
10+
import java.util.*;
911

1012
@Service
1113
public class CourseService {
1214

1315
private final CourseRepository courseRepository;
1416

15-
public CourseService(CourseRepository courseRepository) {
17+
private final StudentRepository studentRepository;
18+
19+
public CourseService(CourseRepository courseRepository, StudentRepository studentRepository) {
1620
this.courseRepository = courseRepository;
21+
this.studentRepository = studentRepository;
1722
}
1823

1924
public List<Course> getAllCourses() {
@@ -27,4 +32,20 @@ public List<Student> getAssignedStudents(Long courseId) {
2732
public Optional<Course> getCourseById(Long id) {
2833
return this.courseRepository.findById(id);
2934
}
35+
36+
public Map<Student, Float> getMeanForAllStudents(Long courseId) {
37+
List<List<Object>> resultList = this.courseRepository.getMeanForAllStudents(courseId);
38+
Student student;
39+
Float meanGrade;
40+
Map<Student, Float> meanGradesMap = new HashMap<>();
41+
42+
for (List<Object> innerResult : resultList) {
43+
meanGrade = ((BigDecimal) innerResult.get(innerResult.size() - 1)).floatValue();
44+
student = this.studentRepository.getReferenceById(((BigInteger) innerResult.get(0)).longValue());
45+
46+
meanGradesMap.put(student, meanGrade);
47+
}
48+
49+
return meanGradesMap;
50+
}
3051
}

lab06/school/src/main/java/pl/edu/agh/to/school/student/StudentService.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class StudentService {
1818

1919
private final StudentRepository studentRepository;
2020
private final CourseService courseService;
21-
2221
private final GradeService gradeService;
2322

2423
public StudentService(StudentRepository studentRepository, CourseService courseService, GradeService gradeService) {
@@ -27,6 +26,10 @@ public StudentService(StudentRepository studentRepository, CourseService courseS
2726
this.gradeService = gradeService;
2827
}
2928

29+
public Student getStudentById(Long id) {
30+
return this.studentRepository.getReferenceById(id);
31+
}
32+
3033
public List<Student> getStudents() {
3134
return studentRepository.findAll();
3235
}

0 commit comments

Comments
 (0)