presence/data/Repository/SQLAdminRepository.cs
2024-12-05 10:31:49 +03:00

59 lines
1.8 KiB
C#

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<int> 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<int> 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();
}
}
}