Skip to content

Commit efedae4

Browse files
committed
wip
1 parent 5d339e9 commit efedae4

24 files changed

Lines changed: 141 additions & 443 deletions

File tree

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CourseGroupsController.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ public async Task<IActionResult> GetAllCourseGroups(long courseId)
3030
: Ok(result);
3131
}
3232

33-
[HttpGet("{courseId}/getAllWithNames")]
34-
[ProducesResponseType(typeof(NamedGroupViewModel[]), (int)HttpStatusCode.OK)]
35-
public async Task<IActionResult> GetAllCourseGroupsWithNames(long courseId)
36-
{
37-
var result = await _coursesClient.GetAllCourseGroupsWithNames(courseId);
38-
return result == null
39-
? NotFound()
40-
: Ok(result);
41-
}
42-
4333
[HttpPost("{courseId}/create")]
4434
[Authorize(Roles = Roles.LecturerRole)]
4535
[ProducesResponseType(typeof(long), (int)HttpStatusCode.OK)]
@@ -84,14 +74,6 @@ public async Task<IActionResult> AddStudentInGroup(long courseId, long groupId,
8474
return Ok();
8575
}
8676

87-
[HttpPost("{courseId}/removeStudentFromGroup/{groupId}")]
88-
[Authorize(Roles = Roles.LecturerRole)]
89-
public async Task<IActionResult> RemoveStudentFromGroup(long courseId, long groupId, [FromQuery] string userId)
90-
{
91-
await _coursesClient.RemoveStudentFromGroup(courseId, groupId, userId);
92-
return Ok();
93-
}
94-
9577
[HttpGet("get/{groupId}")]
9678
[ProducesResponseType(typeof(GroupViewModel), (int)HttpStatusCode.OK)]
9779
public async Task<IActionResult> GetGroup(long groupId)

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CoursesController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ private async Task<CourseViewModel> ToCourseViewModel(CourseDTO course)
308308
AcceptedStudents = acceptedStudents.ToArray(),
309309
NewStudents = newStudents.ToArray(),
310310
Homeworks = course.Homeworks,
311+
Groups = course.Groups,
311312
IsCompleted = course.IsCompleted,
312313
IsOpen = course.IsOpen
313314
};

HwProj.Common/HwProj.Models/CoursesService/ViewModels/CourseViewModels.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class CourseViewModel
5252
public bool IsOpen { get; set; }
5353
public bool IsCompleted { get; set; }
5454

55+
public GroupViewModel[] Groups { get; set; }
5556
public AccountDataDto[] Mentors { get; set; }
5657
public AccountDataDto[] AcceptedStudents { get; set; }
5758
public AccountDataDto[] NewStudents { get; set; }

HwProj.Common/HwProj.Models/CoursesService/ViewModels/GroupViewModel.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
namespace HwProj.Models.CoursesService.ViewModels
55
{
66
public class GroupViewModel
7-
{
8-
public long Id { get; set; }
9-
public string[] StudentsIds { get; set; }
10-
}
11-
12-
public class NamedGroupViewModel
137
{
148
public long Id { get; set; }
159
public string Name { get; set; }

HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CourseGroupsController.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,12 @@ public async Task<GroupViewModel[]> GetAll(long courseId)
2828
var groups = await _groupsService.GetAllAsync(courseId);
2929

3030
var result = groups.Select(t => new GroupViewModel
31-
{
32-
Id = t.Id,
33-
StudentsIds = t.GroupMates.Select(s => s.StudentId).ToArray()
34-
}).ToArray();
35-
36-
return result;
37-
}
38-
39-
[HttpGet("{courseId}/getAllWithNames")]
40-
public async Task<NamedGroupViewModel[]> GetAllWithNames(long courseId)
41-
{
42-
var groups = await _groupsService.GetAllAsync(courseId);
43-
var result = groups.Select(t => new NamedGroupViewModel
4431
{
4532
Id = t.Id,
4633
Name = t.Name,
4734
StudentsIds = t.GroupMates.Select(s => s.StudentId).ToArray()
4835
}).ToArray();
36+
4937
return result;
5038
}
5139

@@ -93,22 +81,14 @@ public async Task<IActionResult> AddStudentInGroup(long groupId, [FromQuery] str
9381
return Ok();
9482
}
9583

96-
[HttpPost("{courseId}/removeStudentFromGroup/{groupId}")]
97-
[ServiceFilter(typeof(CourseMentorOnlyAttribute))]
98-
public async Task<IActionResult> RemoveStudentFromGroup(long groupId, [FromQuery] string userId)
99-
{
100-
return await _groupsService.DeleteGroupMateAsync(groupId, userId)
101-
? Ok()
102-
: NotFound() as IActionResult;
103-
}
104-
10584
[HttpGet]
10685
public async Task<IActionResult> Get([FromBody] long[] groupIds)
10786
{
10887
var groups = await _groupsService.GetGroupsAsync(groupIds);
10988
var result = groups.Select(group => new GroupViewModel
11089
{
11190
Id = group.Id,
91+
Name = group.Name,
11292
StudentsIds = group.GroupMates.Select(g => g.StudentId).ToArray()
11393
}).ToArray();
11494
return Ok(result) as IActionResult;

HwProj.CoursesService/HwProj.CoursesService.API/Repositories/Groups/IGroupsRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace HwProj.CoursesService.API.Repositories.Groups
77
{
88
public interface IGroupsRepository : ICrudRepository<Group, long>
99
{
10-
Task<Group[]> GetGroupsWithGroupMatesAsync(long[] ids);
10+
Task<Group[]> GetGroupsWithGroupMatesAsync(params long[] ids);
1111
IQueryable<Group> GetGroupsWithGroupMatesByCourse(long courseId);
1212
}
1313
}

HwProj.CoursesService/HwProj.CoursesService.API/Repositories/HomeworksRepository.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ public async Task<Homework[]> GetAllWithTasksByCourseAsync(long courseId)
2727
.ToArrayAsync();
2828
}
2929

30-
public async Task<Homework[]> GetWithTasksAsync(long[] homeworkIds, bool withCriteria = false)
31-
{
32-
var query = Context.Set<Homework>().AsNoTracking().Include(h => h.Tasks);
33-
return withCriteria
34-
? await query.ThenInclude(x => x.Criteria).Where(h => homeworkIds.Contains(h.Id)).ToArrayAsync()
35-
: await query.Where(h => homeworkIds.Contains(h.Id)).ToArrayAsync();
36-
}
37-
3830
public async Task<Homework> GetWithTasksAsync(long id, bool withCriteria = false)
3931
{
4032
var query = Context.Set<Homework>().AsNoTracking().Include(h => h.Tasks);

HwProj.CoursesService/HwProj.CoursesService.API/Repositories/IHomeworksRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public interface IHomeworksRepository : ICrudRepository<Homework, long>
88
{
99
Task<Homework[]> GetAllWithTasksAsync();
1010
Task<Homework[]> GetAllWithTasksByCourseAsync(long courseId);
11-
Task<Homework[]> GetWithTasksAsync(long[] homeworkIds, bool withCriteria = false);
1211
Task<Homework> GetWithTasksAsync(long id, bool withCriteria = false);
1312
}
1413
}

HwProj.CoursesService/HwProj.CoursesService.API/Services/CourseFilterService.cs

Lines changed: 68 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum ApplyFilterOperation
1616
Union,
1717
Subtract
1818
}
19+
1920
public class CourseFilterService : ICourseFilterService
2021
{
2122
private const string GlobalFilterUserId = "";
@@ -65,56 +66,56 @@ await _courseFilterRepository.UpdateAsync(courseFilterId, f =>
6566

6667
public async Task<CourseDTO[]> ApplyFiltersToCourses(string userId, CourseDTO[] courses)
6768
{
68-
var courseIds = courses.Select(c => c.Id).ToArray();
69-
7069
var result = new List<CourseDTO>();
7170
foreach (var course in courses)
7271
{
7372
result.Add(await ApplyFilter(course, userId));
7473
}
74+
7575
return result.ToArray();
7676
}
7777

78-
public async Task<CourseDTO> ApplyFilter(CourseDTO courseDto, string userId)
78+
public async Task<CourseDTO> ApplyFilter(CourseDTO course, string userId)
7979
{
80-
var isMentor = courseDto.MentorIds.Contains(userId);
81-
var isCourseStudent = courseDto.AcceptedStudents.Any(t => t.StudentId == userId);
80+
var isMentor = course.MentorIds.Contains(userId);
81+
var isCourseStudent = course.AcceptedStudents.Any(t => t.StudentId == userId);
82+
8283
var findFiltersFor = isMentor || !isCourseStudent
8384
? new[] { userId, GlobalFilterUserId }
84-
: courseDto.MentorIds.Concat(new[] { userId, GlobalFilterUserId }).ToArray();
85+
: course.MentorIds.Concat(new[] { userId, GlobalFilterUserId }).ToArray();
8586

8687
var courseFilters =
87-
(await _courseFilterRepository.GetAsync(findFiltersFor, courseDto.Id))
88+
(await _courseFilterRepository.GetAsync(findFiltersFor, course.Id))
8889
.ToDictionary(x => x.UserId, x => x.CourseFilter);
8990

9091
if (!isMentor)
9192
{
92-
var studentCourse = courseDto;
93+
var studentCourse = course;
9394
if (courseFilters.TryGetValue(GlobalFilterUserId, out var groupFilter))
94-
studentCourse = ApplyFilterInternal(courseDto, studentCourse, groupFilter, ApplyFilterOperation.Subtract);
95-
96-
return courseFilters.TryGetValue(userId, out var studentFilter)
97-
? ApplyFilterInternal(courseDto, studentCourse, studentFilter, ApplyFilterOperation.Union)
98-
: studentCourse;
95+
studentCourse = ApplyFilterInternal(course, studentCourse, groupFilter,
96+
ApplyFilterOperation.Subtract);
97+
98+
studentCourse = courseFilters.TryGetValue(userId, out var studentFilter)
99+
? ApplyFilterInternal(course, studentCourse, studentFilter, ApplyFilterOperation.Union)
100+
: studentCourse;
101+
102+
var mentorIds = course.MentorIds
103+
.Where(u =>
104+
// Фильтрация не настроена вообще
105+
!courseFilters.TryGetValue(u, out var courseFilter) ||
106+
// Не отфильтрованы студенты
107+
!courseFilter.Filter.StudentIds.Any() ||
108+
// Фильтр содержит студента
109+
courseFilter.Filter.StudentIds.Contains(userId))
110+
.ToArray();
111+
112+
studentCourse.MentorIds = mentorIds;
113+
return studentCourse;
99114
}
100115

101-
var course = courseFilters.TryGetValue(userId, out var userFilter)
102-
? ApplyFilterInternal(courseDto, courseDto, userFilter, ApplyFilterOperation.Intersect)
103-
: courseDto;
104-
if (isMentor || !isCourseStudent) return course;
105-
106-
var mentorIds = course.MentorIds
107-
.Where(u =>
108-
// Фильтрация не настроена вообще
109-
!courseFilters.TryGetValue(u, out var courseFilter) ||
110-
// Не отфильтрованы студенты
111-
!courseFilter.Filter.StudentIds.Any() ||
112-
// Фильтр содержит студента
113-
courseFilter.Filter.StudentIds.Contains(userId))
114-
.ToArray();
115-
116-
courseDto.MentorIds = mentorIds;
117-
return course;
116+
return courseFilters.TryGetValue(userId, out var userFilter)
117+
? ApplyFilterInternal(course, course, userFilter, ApplyFilterOperation.Intersect)
118+
: course;
118119
}
119120

120121
public async Task<MentorToAssignedStudentsDTO[]> GetAssignedStudentsIds(long courseId, string[] mentorsIds)
@@ -138,14 +139,15 @@ private async Task<long> AddCourseFilter(Filter filter, long courseId, string us
138139
return courseFilterId;
139140
}
140141

141-
private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO editingCourseDto, CourseFilter? courseFilter, ApplyFilterOperation filterType)
142+
private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO editingCourseDto,
143+
CourseFilter? courseFilter, ApplyFilterOperation filterType)
142144
{
143145
var filter = courseFilter?.Filter;
144146

145147
if (filter == null)
146148
return editingCourseDto;
147149

148-
var homeworks = filter.HomeworkIds.Any()
150+
var homeworks = filter.HomeworkIds.Count != 0
149151
? filterType switch
150152
{
151153
ApplyFilterOperation.Intersect => editingCourseDto.Homeworks
@@ -182,6 +184,7 @@ private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO edit
182184
? new GroupViewModel
183185
{
184186
Id = gs.Id,
187+
Name = gs.Name,
185188
StudentsIds = filteredStudentsIds
186189
}
187190
: null;
@@ -201,71 +204,50 @@ private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO edit
201204
};
202205
}
203206

204-
public async Task UpdateGroupFilters(long courseId, long homeworkId, GroupMate[] groupMates)
207+
public async Task UpdateGroupFilters(long courseId, long homeworkId, IEnumerable<string> studentIds)
205208
{
206-
// Добавление группового домашнего задания в глобальный фильтр курса
207-
var globalFilter = await _courseFilterRepository.GetAsync(GlobalFilterUserId, courseId);
209+
var filterIds = studentIds.Union(new[] { GlobalFilterUserId }).ToArray();
210+
var filters = (await _courseFilterRepository.GetAsync(filterIds, courseId))
211+
.ToDictionary(x => x.UserId, x => x.CourseFilter);
208212

209-
if (globalFilter != null)
213+
foreach (var filterId in filterIds)
210214
{
211-
var filter = globalFilter.Filter;
212-
213-
if (!filter.HomeworkIds.Contains(homeworkId))
214-
filter.HomeworkIds.Add(homeworkId);
215+
await AddOrUpdateHomeworkToFilter(filters.GetValueOrDefault(filterId), filterId, courseId, homeworkId);
216+
}
217+
}
215218

216-
await _courseFilterRepository.UpdateAsync(globalFilter.Id, f =>
217-
new CourseFilter
218-
{
219-
FilterJson = new CourseFilter { Filter = filter }.FilterJson
220-
});
219+
private async Task AddOrUpdateHomeworkToFilter(CourseFilter filter, string userId, long courseId,
220+
long homeworkId)
221+
{
222+
if (filter != null)
223+
{
224+
await UpdateFilterWithHomework(filter, homeworkId);
221225
}
222226
else
223227
{
224-
var newFilter = new Filter
225-
{
226-
StudentIds = new List<string>(),
227-
HomeworkIds = new List<long> { homeworkId },
228-
MentorIds = new List<string>(),
229-
};
230-
231-
await _courseFilterRepository.AddAsync(new CourseFilter { Filter = newFilter }, GlobalFilterUserId, courseId);
228+
await CreateFilterWithHomework(userId, courseId, homeworkId);
232229
}
230+
}
233231

234-
// Добавление группового домашнего задания в персональные фильтры участников группы
235-
var studentIds = groupMates.Select(gm => gm.StudentId).ToArray();
236-
var studentFilters = (await _courseFilterRepository.GetAsync(studentIds, courseId))
237-
.ToDictionary(x => x.UserId, x => x.CourseFilter);
238-
239-
foreach (var groupMate in groupMates)
232+
private async Task UpdateFilterWithHomework(CourseFilter courseFilter, long homeworkId)
233+
{
234+
if (!courseFilter.Filter.HomeworkIds.Contains(homeworkId))
240235
{
241-
if (studentFilters.TryGetValue(groupMate.StudentId, out var studentFilter))
242-
{
243-
var filter = studentFilter.Filter;
244-
if (!filter.HomeworkIds.Contains(homeworkId))
245-
filter.HomeworkIds.Add(homeworkId);
246-
247-
await _courseFilterRepository.UpdateAsync(studentFilter.Id, f =>
248-
new CourseFilter
249-
{
250-
FilterJson = new CourseFilter { Filter = filter }.FilterJson
251-
});
252-
}
253-
else
254-
{
255-
var newFilter = new Filter
256-
{
257-
StudentIds = new List<string>(),
258-
HomeworkIds = new List<long> { homeworkId },
259-
MentorIds = new List<string>()
260-
};
261-
262-
await _courseFilterRepository.AddAsync(
263-
new CourseFilter { Filter = newFilter },
264-
groupMate.StudentId,
265-
courseId
266-
);
267-
}
236+
courseFilter.Filter.HomeworkIds.Add(homeworkId);
237+
await UpdateAsync(courseFilter.Id, courseFilter.Filter);
268238
}
269239
}
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+
}
270252
}
271253
}

HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ public async Task<Course[]> GetAllAsync()
7474

7575
CourseDomain.FillTasksInCourses(course);
7676

77-
var groups = await _groupsRepository.GetGroupsWithGroupMatesByCourse(course.Id).ToArrayAsync();
77+
var groups = await _groupsRepository.GetGroupsWithGroupMatesByCourse(course.Id).ToListAsync();
7878
var courseDto = course.ToCourseDto();
7979
courseDto.Groups = groups.Select(g =>
8080
new GroupViewModel
8181
{
8282
Id = g.Id,
83+
Name = g.Name,
8384
StudentsIds = g.GroupMates.Select(t => t.StudentId).ToArray()
8485
}).ToArray();
8586

0 commit comments

Comments
 (0)