using data.Repository; using data.DAO; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; namespace data.Repository { public class SQLAdminRepository : IAdminRepository { private readonly DatabaseContext _context; public SQLAdminRepository(DatabaseContext context) { _context = context; } public void AddStudentsToGroup(int groupId, IEnumerable studentIds) { var group = _context.Groups.Include(g => g.Students).FirstOrDefault(g => g.Id == groupId); if (group == null) return; var studentsToAdd = _context.Students .Where(u => studentIds.Contains(u.Id)) .ToList(); foreach (var student in studentsToAdd) { if (!group.Students.Any(u => u.Id == student.Id)) { group.Students.Add(student); } } _context.SaveChanges(); } public void AddSubjectsToGroup(int groupId, IEnumerable subjectIds) { var group = _context.Groups.Include(g => g.StudentGroupsSubjects).ThenInclude(s => s.Subject).FirstOrDefault(g => g.Id == groupId); if (group == null) return; var subjectsToAdd = _context.Subjects .Where(s => subjectIds.Contains(s.Id)) .ToList(); foreach (var subject in subjectsToAdd) { if (!group.StudentGroupsSubjects.Any(s => s.Subject.Id == subject.Id)) { group.StudentGroupsSubjects.Add(new StudentGroupSubject { Subject = subject }); } } _context.SaveChanges(); } } }