Skip to content

Commit 150692f

Browse files
committed
Add functionality to return student remixes with unread feedback count
1 parent c8018a9 commit 150692f

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

app/controllers/api/school_classes_controller.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def index
1414
if current_user&.school_teacher?(@school) || current_user&.school_owner?(@school)
1515
render :teacher_index, formats: [:json], status: :ok
1616
else
17-
render :student_index, formats: [:json], status: :ok
17+
render_student_index(school_classes)
1818
end
1919
end
2020

@@ -83,6 +83,43 @@ def destroy
8383

8484
private
8585

86+
def render_student_index(school_classes)
87+
unread_counts = calculate_unread_counts(school_classes)
88+
@school_classes_with_teachers_and_unread_counts = @school_classes_with_teachers.zip(unread_counts)
89+
render :student_index, formats: [:json], status: :ok
90+
end
91+
92+
def calculate_unread_counts(school_classes)
93+
school_classes.map do |school_class|
94+
lessons = school_class.lessons.accessible_by(current_ability)
95+
remixes = user_remixes_for_lessons(lessons)
96+
97+
remixes.count do |remix|
98+
remix&.school_project&.feedback&.exists?(read_at: nil)
99+
end
100+
end
101+
end
102+
103+
def user_remixes_for_lessons(lessons)
104+
lessons.filter_map do |lesson|
105+
next nil unless lesson&.project&.remixes&.any?
106+
107+
user_remix_for_lesson(lesson)
108+
end
109+
end
110+
111+
def user_remix_for_lesson(lesson)
112+
remixes = lesson.project&.remixes
113+
return unless remixes
114+
115+
remixes
116+
.where(user_id: current_user.id)
117+
.accessible_by(current_ability)
118+
.order(created_at: :asc)
119+
.includes(school_project: :feedback)
120+
.first
121+
end
122+
86123
def find_or_create_school_class(school_class_params)
87124
# First try and find the class (in case we're re-importing)
88125
existing_school_class = SchoolClass.find_by(
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3-
json.array!(@school_classes_with_teachers) do |school_class, teachers|
3+
json.array!(@school_classes_with_teachers_and_unread_counts) do |class_with_teachers, unread_count|
4+
school_class, teachers = class_with_teachers
5+
46
json.partial! 'school_class', school_class: school_class, teachers: teachers
7+
json.unread_feedback_count unread_count
58
end

0 commit comments

Comments
 (0)