Skip to content

Commit e43ccd9

Browse files
committed
refactor: move filter update to backend
1 parent c013a7f commit e43ccd9

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Linq;
22
using System.Threading.Tasks;
3+
using System.Collections.Generic;
34
using AutoMapper;
5+
using HwProj.Common.Net8;
46
using HwProj.CoursesService.API.Filters;
57
using HwProj.CoursesService.API.Models;
68
using HwProj.CoursesService.API.Services;
@@ -15,11 +17,19 @@ public class CourseGroupsController : Controller
1517
{
1618
private readonly IGroupsService _groupsService;
1719
private readonly IMapper _mapper;
20+
private readonly ICourseFilterService _courseFilterService;
21+
private readonly ICoursesService _coursesService;
1822

19-
public CourseGroupsController(IMapper mapper, IGroupsService groupsService)
23+
public CourseGroupsController(
24+
IMapper mapper,
25+
IGroupsService groupsService,
26+
ICourseFilterService courseFilterService,
27+
ICoursesService coursesService)
2028
{
2129
_mapper = mapper;
2230
_groupsService = groupsService;
31+
_courseFilterService = courseFilterService;
32+
_coursesService = coursesService;
2333
}
2434

2535
[HttpGet("{courseId}/getAll")]
@@ -47,6 +57,15 @@ public async Task<IActionResult> CreateGroup([FromBody] CreateGroupViewModel gro
4757
GroupMates = groupViewModel.GroupMatesIds.Select(t => new GroupMate() { StudentId = t }).ToList()
4858
};
4959
var id = await _groupsService.AddGroupAsync(group);
60+
61+
var userId = Request.GetUserIdFromHeader();
62+
var courseMentorIds = await _coursesService.GetCourseLecturers(groupViewModel.CourseId);
63+
if (userId != null && courseMentorIds.Contains(userId))
64+
await _courseFilterService.AddToFilter(groupViewModel.CourseId, userId, new Filter
65+
{
66+
GroupIds = new List<long> { id }
67+
});
68+
5069
return Ok(id);
5170
}
5271

HwProj.CoursesService/HwProj.CoursesService.API/Controllers/HomeworksController.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Threading.Tasks;
22
using HwProj.CoursesService.API.Domains;
33
using HwProj.CoursesService.API.Filters;
4+
using HwProj.CoursesService.API.Models;
45
using HwProj.CoursesService.API.Services;
6+
using HwProj.Common.Net8;
57
using HwProj.Models.CoursesService.ViewModels;
68
using Microsoft.AspNetCore.Mvc;
79
using System.Linq;
@@ -13,10 +15,12 @@ namespace HwProj.CoursesService.API.Controllers
1315
public class HomeworksController : Controller
1416
{
1517
private readonly IHomeworksService _homeworksService;
18+
private readonly ICourseFilterService _courseFilterService;
1619

17-
public HomeworksController(IHomeworksService homeworksService)
20+
public HomeworksController(IHomeworksService homeworksService, ICourseFilterService courseFilterService)
1821
{
1922
_homeworksService = homeworksService;
23+
_courseFilterService = courseFilterService;
2024
}
2125

2226
[HttpPost("{courseId}/add")]
@@ -28,6 +32,13 @@ public async Task<IActionResult> AddHomework(long courseId,
2832
if (validationResult.Any()) return BadRequest(validationResult);
2933

3034
var newHomework = await _homeworksService.AddHomeworkAsync(courseId, homeworkViewModel);
35+
var mentorId = Request.GetUserIdFromHeader();
36+
if (mentorId != null)
37+
await _courseFilterService.AddToFilter(courseId, mentorId, new Filter
38+
{
39+
HomeworkIds = new System.Collections.Generic.List<long> { newHomework.Id }
40+
});
41+
3142
return Ok(newHomework.ToHomeworkViewModel());
3243
}
3344

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,42 @@ public async Task UpdateGroupFilters(long courseId, long homeworkId, Group group
281281
await AddCourseFilter(newFilter, courseId, filterId);
282282
}
283283
}
284+
285+
public async Task AddToFilter(long courseId, string userId, Filter filter)
286+
{
287+
var existingCourseFilter = await _courseFilterRepository.GetAsync(userId, courseId);
288+
if (existingCourseFilter?.Filter is null)
289+
return;
290+
291+
var targetFilter = existingCourseFilter.Filter.FillEmptyFields();
292+
filter.FillEmptyFields();
293+
294+
var hasChanges =
295+
AddToFilterList(targetFilter.GroupIds, filter.GroupIds)
296+
| AddToFilterList(targetFilter.StudentIds, filter.StudentIds)
297+
| AddToFilterList(targetFilter.HomeworkIds, filter.HomeworkIds)
298+
| AddToFilterList(targetFilter.MentorIds, filter.MentorIds);
299+
300+
if (hasChanges)
301+
await UpdateAsync(existingCourseFilter.Id, targetFilter);
302+
}
303+
304+
private static bool AddToFilterList<T>(List<T> target, IEnumerable<T> itemsToAdd)
305+
{
306+
if (!target.Any())
307+
return false;
308+
309+
var hasChanges = false;
310+
foreach (var item in itemsToAdd.Distinct())
311+
{
312+
if (target.Contains(item))
313+
continue;
314+
315+
target.Add(item);
316+
hasChanges = true;
317+
}
318+
319+
return hasChanges;
320+
}
284321
}
285322
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ public interface ICourseFilterService
1616
Task<CourseDTO> ApplyFilter(CourseDTO courseDto, string userId);
1717
Task<MentorToAssignedStudentsDTO[]> GetAssignedStudentsIds(long courseId, string[] mentorsIds);
1818
Task UpdateGroupFilters(long courseId, long homeworkId, Group group);
19+
Task AddToFilter(long courseId, string filterId, Filter filter);
1920
}
2021
}

0 commit comments

Comments
 (0)