Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ public async Task<IActionResult> GetMentorWorkspace(long courseId, string mentor
var workspace = new WorkspaceViewModel
{
Homeworks = mentorCourseView.Value.Homeworks,
Students = students.OrderBy(x => x.Surname).ThenBy(x => x.Name).ToArray()
Students = students.OrderBy(x => x.Surname).ThenBy(x => x.Name).ToArray(),
Groups = mentorCourseView.Value.Groups,
};
return Ok(workspace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class InviteExpertViewModel

public long CourseId { get; set; }

public List<long> GroupIds { get; set; } = new List<long>();

public List<string> StudentIds { get; set; } = new List<string>();

public List<long> HomeworkIds { get; set; } = new List<long>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class CreateCourseFilterModel

public long CourseId { get; set; }

public List<long> GroupIds { get; set; } = new List<long>();

public List<string> StudentIds { get; set; } = new List<string>();

public List<long> HomeworkIds { get; set; } = new List<long>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace HwProj.Models.CoursesService.DTO
{
public class CourseFilterDTO
{
public List<long> GroupIds { get; set; } = new List<long>();

public List<string> StudentIds { get; set; } = new List<string>();

public List<long> HomeworkIds { get; set; } = new List<long>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class CreateCourseFilterDTO
{
public string Id { get; set; }

public List<long> GroupIds { get; set; } = new List<long>();

public List<string> StudentIds { get; set; } = new List<string>();

public List<long> HomeworkIds { get; set; } = new List<long>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace HwProj.Models.CoursesService.DTO
{
public class EditMentorWorkspaceDTO
{
public List<long> GroupIds { get; set; } = new List<long>();

public List<string> StudentIds { get; set; } = new List<string>();

public List<long> HomeworkIds { get; set; } = new List<long>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace HwProj.Models.CoursesService.ViewModels
{
public class WorkspaceViewModel
{
public GroupViewModel[] Groups { get; set; }

public AccountDataDto[] Students { get; set; }

public HomeworkViewModel[] Homeworks { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HwProj.Common.Net8;
using HwProj.CoursesService.API.Filters;
using HwProj.CoursesService.API.Models;
using HwProj.CoursesService.API.Services;
Expand All @@ -15,11 +17,19 @@ public class CourseGroupsController : Controller
{
private readonly IGroupsService _groupsService;
private readonly IMapper _mapper;
private readonly ICourseFilterService _courseFilterService;
private readonly ICoursesService _coursesService;

public CourseGroupsController(IMapper mapper, IGroupsService groupsService)
public CourseGroupsController(
IMapper mapper,
IGroupsService groupsService,
ICourseFilterService courseFilterService,
ICoursesService coursesService)
{
_mapper = mapper;
_groupsService = groupsService;
_courseFilterService = courseFilterService;
_coursesService = coursesService;
}

[HttpGet("{courseId}/getAll")]
Expand Down Expand Up @@ -47,6 +57,15 @@ public async Task<IActionResult> CreateGroup([FromBody] CreateGroupViewModel gro
GroupMates = groupViewModel.GroupMatesIds.Select(t => new GroupMate() { StudentId = t }).ToList()
};
var id = await _groupsService.AddGroupAsync(group);

var userId = Request.GetUserIdFromHeader();
var courseMentorIds = await _coursesService.GetCourseLecturers(groupViewModel.CourseId);
if (userId != null && courseMentorIds.Contains(userId))
await _courseFilterService.AddToFilter(groupViewModel.CourseId, userId, new Filter
{
GroupIds = new List<long> { id }
});

return Ok(id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using HwProj.CoursesService.API.Domains;
using HwProj.CoursesService.API.Filters;
using HwProj.CoursesService.API.Models;
using HwProj.CoursesService.API.Services;
using HwProj.Common.Net8;
using HwProj.Models.CoursesService.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
Expand All @@ -13,10 +15,12 @@ namespace HwProj.CoursesService.API.Controllers
public class HomeworksController : Controller
{
private readonly IHomeworksService _homeworksService;
private readonly ICourseFilterService _courseFilterService;

public HomeworksController(IHomeworksService homeworksService)
public HomeworksController(IHomeworksService homeworksService, ICourseFilterService courseFilterService)
{
_homeworksService = homeworksService;
_courseFilterService = courseFilterService;
}

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

var newHomework = await _homeworksService.AddHomeworkAsync(courseId, homeworkViewModel);
var mentorId = Request.GetUserIdFromHeader();
if (mentorId != null)
await _courseFilterService.AddToFilter(courseId, mentorId, new Filter
{
HomeworkIds = new System.Collections.Generic.List<long> { newHomework.Id }
});

return Ok(newHomework.ToHomeworkViewModel());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace HwProj.CoursesService.API.Models
{
public class Filter
{
[JsonProperty(PropertyName = "GRP")]
public List<long> GroupIds { get; set; }

[JsonProperty(PropertyName = "STUD")]
public List<string> StudentIds { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public class CourseFilterService : ICourseFilterService
{
private const string GlobalFilterId = "";
private readonly ICourseFilterRepository _courseFilterRepository;
private readonly IGroupsService _groupsService;

public CourseFilterService(
ICourseFilterRepository courseFilterRepository)
ICourseFilterRepository courseFilterRepository, IGroupsService groupsService)
{
_courseFilterRepository = courseFilterRepository;
_groupsService = groupsService;
}

public async Task<Result<long>> CreateOrUpdateCourseFilter(CreateCourseFilterModel courseFilterModel)
Expand Down Expand Up @@ -130,15 +132,41 @@ public async Task<CourseDTO> ApplyFilter(CourseDTO course, string userId)
public async Task<MentorToAssignedStudentsDTO[]> GetAssignedStudentsIds(long courseId, string[] mentorsIds)
{
var usersCourseFilters = await _courseFilterRepository.GetAsync(mentorsIds, courseId);
if (usersCourseFilters == null || usersCourseFilters.Count == 0)
return Array.Empty<MentorToAssignedStudentsDTO>();

return usersCourseFilters
.Where(u => u.CourseFilter.Filter.HomeworkIds.Count == 0)
.Select(u => new MentorToAssignedStudentsDTO
var groupIds = usersCourseFilters
.SelectMany(filter => filter.CourseFilter.Filter.GroupIds ?? Enumerable.Empty<long>())
.Distinct()
.ToArray();

var groups = await _groupsService.GetGroupsAsync(groupIds);
var groupToStudentIds = groups
.ToDictionary(
g => g.Id,
g => g.GroupMates?.Select(gm => gm.StudentId).ToArray() ?? Array.Empty<string>()
);

var result = usersCourseFilters
.Select(u =>
{
MentorId = u.Id,
SelectedStudentsIds = u.CourseFilter.Filter.StudentIds
var directStudents = u.CourseFilter.Filter.StudentIds ?? new List<string>() {};
var groupIdsForMentor = u.CourseFilter.Filter.GroupIds ?? Enumerable.Empty<long>();
var studentsFromGroups = groupIdsForMentor
.Where(gid => groupToStudentIds.ContainsKey(gid))
.SelectMany(gid => groupToStudentIds[gid])
.Distinct()
.ToList();

return new MentorToAssignedStudentsDTO
{
MentorId = u.Id,
SelectedStudentsIds = directStudents.Concat(studentsFromGroups).Distinct().ToList()
};
})
.ToArray();

return result;
}

private async Task<long> AddCourseFilter(Filter filter, long courseId, string userId)
Expand Down Expand Up @@ -176,6 +204,34 @@ private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO edit
}
: editingCourseDto.Homeworks;

var groups = filter.GroupIds.Any()
? editingCourseDto.Groups.Where(g => filter.GroupIds.Contains(g.Id))
: editingCourseDto.Groups;

var filteredStudentIds = filter.GroupIds.Any()
? filter.StudentIds.Concat(groups.SelectMany(g => g.StudentsIds))
: filter.StudentIds.Any()
? filter.StudentIds
: editingCourseDto.AcceptedStudents.Select(st => st.StudentId);

var filteredGroups = filteredStudentIds.Any()
? editingCourseDto.Groups
.Select(gs =>
{
var groupStudentsIds = gs.StudentsIds.Intersect(filteredStudentIds).ToArray();
return groupStudentsIds.Any()
? new GroupViewModel
{
Id = gs.Id,
Name = gs.Name,
StudentsIds = groupStudentsIds
}
: null;
})
.Where(t => t != null)
.ToArray()
: editingCourseDto.Groups;

return new CourseDTO
{
Id = editingCourseDto.Id,
Expand All @@ -184,32 +240,21 @@ private CourseDTO ApplyFilterInternal(CourseDTO initialCourseDto, CourseDTO edit
IsCompleted = editingCourseDto.IsCompleted,
IsOpen = editingCourseDto.IsOpen,
InviteCode = editingCourseDto.InviteCode,
Groups =
(filter.StudentIds.Any()
? editingCourseDto.Groups.Select(gs =>
{
var filteredStudentsIds = gs.StudentsIds.Intersect(filter.StudentIds).ToArray();
return filteredStudentsIds.Any()
? new GroupViewModel
{
Id = gs.Id,
Name = gs.Name,
StudentsIds = filteredStudentsIds
}
: null;
})
.Where(t => t != null)
.ToArray()
: editingCourseDto.Groups)!,
Groups = filter.GroupIds.Any()
? filteredGroups
.Where(g => filter.GroupIds.Contains(g.Id) || g.Name == string.Empty)
.ToArray()
: filteredGroups,
MentorIds = filter.MentorIds.Any()
? editingCourseDto.MentorIds.Intersect(filter.MentorIds).ToArray()
: editingCourseDto.MentorIds,
CourseMates =
filter.StudentIds.Any()
? editingCourseDto.CourseMates
.Where(mate => !mate.IsAccepted || filter.StudentIds.Contains(mate.StudentId)).ToArray()
: editingCourseDto.CourseMates,
Homeworks = homeworks.OrderBy(hw => hw.PublicationDate).ToArray()
CourseMates = editingCourseDto.CourseMates
.Where(mate => !mate.IsAccepted || filteredStudentIds.Contains(mate.StudentId))
.ToArray(),
Homeworks = homeworks
.Where(hw => hw.GroupId == null || groups.Any(g => g.Id == hw.GroupId))
.OrderBy(hw => hw.PublicationDate)
.ToArray()
};
}

Expand All @@ -225,7 +270,8 @@ public async Task UpdateGroupFilters(long courseId, long homeworkId, Group group
{
var existingCourseFilter = filters.SingleOrDefault(f => f.Id == filterId)?.CourseFilter;
var newFilter = existingCourseFilter?.Filter
?? new Filter { StudentIds = new List<string>(), HomeworkIds = new List<long>(), MentorIds = new List<string>() };
?? new Filter { GroupIds = new List<long>(), StudentIds = new List<string>(),
HomeworkIds = new List<long>(), MentorIds = new List<string>() };
newFilter.HomeworkIds.Add(homeworkId);

if (existingCourseFilter != null)
Expand All @@ -234,5 +280,42 @@ public async Task UpdateGroupFilters(long courseId, long homeworkId, Group group
await AddCourseFilter(newFilter, courseId, filterId);
}
}

public async Task AddToFilter(long courseId, string userId, Filter filter)
{
var existingCourseFilter = await _courseFilterRepository.GetAsync(userId, courseId);
if (existingCourseFilter?.Filter is null)
return;

var targetFilter = existingCourseFilter.Filter.FillEmptyFields();
filter.FillEmptyFields();

var hasChanges =
AddToFilterList(targetFilter.GroupIds, filter.GroupIds)
| AddToFilterList(targetFilter.StudentIds, filter.StudentIds)
| AddToFilterList(targetFilter.HomeworkIds, filter.HomeworkIds)
| AddToFilterList(targetFilter.MentorIds, filter.MentorIds);

if (hasChanges)
await UpdateAsync(existingCourseFilter.Id, targetFilter);
}

private static bool AddToFilterList<T>(List<T> target, IEnumerable<T> itemsToAdd)
{
if (!target.Any())
return false;

var hasChanges = false;
foreach (var item in itemsToAdd.Distinct())
{
if (target.Contains(item))
continue;

target.Add(item);
hasChanges = true;
}

return hasChanges;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static Filter CreateFilter(CreateCourseFilterModel courseFilterModel)
{
return new Filter
{
GroupIds = courseFilterModel.GroupIds,
HomeworkIds = courseFilterModel.HomeworkIds,
MentorIds = courseFilterModel.MentorIds,
StudentIds = courseFilterModel.StudentIds
Expand All @@ -18,6 +19,7 @@ public static Filter CreateFilter(CreateCourseFilterModel courseFilterModel)

public static Filter FillEmptyFields(this Filter filter)
{
filter.GroupIds ??= new List<long>();
filter.StudentIds ??= new List<string>();
filter.HomeworkIds ??= new List<long>();
filter.MentorIds ??= new List<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using HwProj.CoursesService.API.Models;
using HwProj.Models.CoursesService;
Expand All @@ -16,5 +15,6 @@ public interface ICourseFilterService
Task<CourseDTO> ApplyFilter(CourseDTO courseDto, string userId);
Task<MentorToAssignedStudentsDTO[]> GetAssignedStudentsIds(long courseId, string[] mentorsIds);
Task UpdateGroupFilters(long courseId, long homeworkId, Group group);
Task AddToFilter(long courseId, string userId, Filter filter);
}
}
Loading
Loading