77using HwProj . Models . CoursesService . ViewModels ;
88using HwProj . Models . Result ;
99using System . Collections . Generic ;
10+ using System ;
1011
1112namespace HwProj . CoursesService . API . Services
1213{
@@ -19,13 +20,15 @@ public enum ApplyFilterOperation
1920
2021 public class CourseFilterService : ICourseFilterService
2122 {
22- private const string GlobalFilterUserId = "" ;
23+ private const string GlobalFilterId = "" ;
2324 private readonly ICourseFilterRepository _courseFilterRepository ;
25+ private readonly IGroupsService _groupsService ;
2426
2527 public CourseFilterService (
26- ICourseFilterRepository courseFilterRepository )
28+ ICourseFilterRepository courseFilterRepository , IGroupsService groupsService )
2729 {
2830 _courseFilterRepository = courseFilterRepository ;
31+ _groupsService = groupsService ;
2932 }
3033
3134 public async Task < Result < long > > CreateOrUpdateCourseFilter ( CreateCourseFilterModel courseFilterModel )
@@ -80,9 +83,13 @@ public async Task<CourseDTO> ApplyFilter(CourseDTO course, string userId)
8083 var isMentor = course . MentorIds . Contains ( userId ) ;
8184 var isCourseStudent = course . AcceptedStudents . Any ( t => t . StudentId == userId ) ;
8285
86+ // Получаем группы пользователя, чтобы найти фильтры для них
87+ var studentGroups = await _groupsService . GetStudentGroupsAsync ( course . Id , userId ) ;
88+ var groupIds = studentGroups . Select ( g => g . Id . ToString ( ) ) . ToArray ( ) ;
89+
8390 var findFiltersFor = isMentor || ! isCourseStudent
84- ? new [ ] { userId , GlobalFilterUserId }
85- : course . MentorIds . Concat ( new [ ] { userId , GlobalFilterUserId } ) . ToArray ( ) ;
91+ ? new [ ] { userId , GlobalFilterId }
92+ : course . MentorIds . Concat ( new [ ] { userId , GlobalFilterId } ) . Concat ( groupIds ) . ToArray ( ) ;
8693
8794 var courseFilters =
8895 ( await _courseFilterRepository . GetAsync ( findFiltersFor , course . Id ) )
@@ -91,13 +98,16 @@ public async Task<CourseDTO> ApplyFilter(CourseDTO course, string userId)
9198 if ( ! isMentor )
9299 {
93100 var studentCourse = course ;
94- if ( courseFilters . TryGetValue ( GlobalFilterUserId , out var groupFilter ) )
101+ if ( courseFilters . TryGetValue ( GlobalFilterId , out var groupFilter ) )
95102 studentCourse = ApplyFilterInternal ( course , studentCourse , groupFilter ,
96103 ApplyFilterOperation . Subtract ) ;
97104
98- studentCourse = courseFilters . TryGetValue ( userId , out var studentFilter )
99- ? ApplyFilterInternal ( course , studentCourse , studentFilter , ApplyFilterOperation . Union )
100- : studentCourse ;
105+ // Применяем фильтры всех групп студента
106+ foreach ( var group in studentGroups )
107+ {
108+ if ( courseFilters . TryGetValue ( group . Id . ToString ( ) , out var groupCourseFilter ) )
109+ studentCourse = ApplyFilterInternal ( course , studentCourse , groupCourseFilter , ApplyFilterOperation . Union ) ;
110+ }
101111
102112 var mentorIds = course . MentorIds
103113 . Where ( u =>
@@ -204,50 +214,36 @@ private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO edit
204214 } ;
205215 }
206216
207- public async Task UpdateGroupFilters ( long courseId , long homeworkId , IEnumerable < string > studentIds )
217+ public async Task UpdateGroupFilters ( long courseId , long homeworkId , Group group )
208218 {
209- var filterIds = studentIds . Union ( new [ ] { GlobalFilterUserId } ) . ToArray ( ) ;
210- var filters = ( await _courseFilterRepository . GetAsync ( filterIds , courseId ) )
219+ var groupMates = ( group ? . GroupMates . ToArray ( ) ?? Array . Empty < GroupMate > ( ) ) . Select ( gm => gm . StudentId ) . ToList ( ) ;
220+
221+ var existingFilters = ( await _courseFilterRepository . GetAsync ( new [ ] { GlobalFilterId , group . Id . ToString ( ) } , courseId ) )
211222 . ToDictionary ( x => x . Id , x => x . CourseFilter ) ;
212223
213- foreach ( var filterId in filterIds )
214- {
215- await AddOrUpdateHomeworkToFilter ( filters . GetValueOrDefault ( filterId ) , filterId , courseId , homeworkId ) ;
216- }
224+ await UpdateOrCreateFilter ( GlobalFilterId , courseId , homeworkId , new List < string > ( ) , existingFilters ) ;
225+ await UpdateOrCreateFilter ( group . Id . ToString ( ) , courseId , homeworkId , groupMates , existingFilters ) ;
217226 }
218227
219- private async Task AddOrUpdateHomeworkToFilter ( CourseFilter filter , string userId , long courseId ,
220- long homeworkId )
228+ private async Task UpdateOrCreateFilter ( string id , long courseId , long homeworkId , List < string > studentIds ,
229+ Dictionary < string , CourseFilter > existingFilters )
221230 {
222- if ( filter != null )
231+ if ( existingFilters . TryGetValue ( id , out var courseFilter ) && courseFilter . Filter is { } filter )
223232 {
224- await UpdateFilterWithHomework ( filter , homeworkId ) ;
233+ filter . StudentIds = studentIds ;
234+ filter . HomeworkIds . Add ( homeworkId ) ;
235+ await UpdateAsync ( courseFilter . Id , courseFilter . Filter ) ;
225236 }
226237 else
227238 {
228- await CreateFilterWithHomework ( userId , courseId , homeworkId ) ;
229- }
230- }
231-
232- private async Task UpdateFilterWithHomework ( CourseFilter courseFilter , long homeworkId )
233- {
234- if ( ! courseFilter . Filter . HomeworkIds . Contains ( homeworkId ) )
235- {
236- courseFilter . Filter . HomeworkIds . Add ( homeworkId ) ;
237- await UpdateAsync ( courseFilter . Id , courseFilter . Filter ) ;
239+ var newFilter = new Filter
240+ {
241+ StudentIds = studentIds ,
242+ HomeworkIds = new List < long > { homeworkId } ,
243+ MentorIds = new List < string > ( )
244+ } ;
245+ await AddCourseFilter ( newFilter , courseId , id ) ;
238246 }
239247 }
240-
241- private async Task CreateFilterWithHomework ( string userId , long courseId , long homeworkId )
242- {
243- var newFilter = new Filter
244- {
245- StudentIds = new List < string > ( ) ,
246- HomeworkIds = new List < long > { homeworkId } ,
247- MentorIds = new List < string > ( )
248- } ;
249-
250- await AddCourseFilter ( newFilter , courseId , userId ) ;
251- }
252248 }
253249}
0 commit comments