Skip to content

Commit

Permalink
use native query to compute total points of a student
Browse files Browse the repository at this point in the history
  • Loading branch information
sealexan committed Dec 14, 2023
1 parent 7af8816 commit c330d7f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/main/kotlin/ch/uzh/ifi/access/repository/CourseRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,23 @@ interface CourseRepository : JpaRepository<Course?, Long?> {
fun getTeamMemberName(email: String?): MemberOverview?
fun findCourseBySlug(courseSlug: String?): CourseSummary?

@Query(
nativeQuery = true, value = """
SELECT sum(e.best_score) AS total_points
FROM evaluation e
JOIN task t ON e.task_id = t.id
JOIN assignment a ON t.assignment_id = a.id
JOIN course c ON a.course_id = c.id
WHERE e.id IN (
SELECT MAX(id)
FROM evaluation
WHERE user_id = :userId
GROUP BY task_id
)
AND c.slug = :courseSlug
AND e.user_id = :userId
"""
)
fun getTotalPoints(courseSlug: String, userId: String): Double?

}
6 changes: 3 additions & 3 deletions src/main/kotlin/ch/uzh/ifi/access/service/CourseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import java.util.stream.Stream
class CourseServiceForCaching(
private val roleService: RoleService,
private val courseService: CourseService,
private val courseRepository: CourseRepository,
) {

fun getStudents(courseSlug: String): List<StudentDTO> {
Expand All @@ -69,9 +70,8 @@ class CourseServiceForCaching(
return course.registeredStudents.map {
val user = roleService.getUserByUsername(it)
if (user != null) {
val studentDTO = courseService.getStudentWithPoints(courseSlug, user)
studentDTO.username = user.username
studentDTO.registrationId = it
val coursePoints = courseRepository.getTotalPoints(courseSlug, user.username) ?: 0.0
val studentDTO = StudentDTO(user.firstName, user.lastName, user.email, coursePoints, user.username, it)
studentDTO
} else {
StudentDTO(registrationId = it)
Expand Down

0 comments on commit c330d7f

Please sign in to comment.