using data.DAO; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; namespace data.Repository { public class SQLPresenceRepository : IPresenceRepository { private readonly DatabaseContext _context; public SQLPresenceRepository(DatabaseContext context) { _context = context; } public IEnumerable GetPresence( int groupId, int? subjectId = null, DateOnly? date = null, int? studentId = null) { var query = _context.Diaries .Include(p => p.Student) .Include(p => p.StudentGroupSubject) .ThenInclude(p => p.Subject) .Include(p => p.Traffic) .Where(p => p.Student.GroupId == groupId); if (subjectId.HasValue) query = query.Where(p => p.StudentGroupSubject.Subject.Id == subjectId.Value); if (date.HasValue) query = query.Where(p => p.Date == date.Value); if (studentId.HasValue) query = query.Where(p => p.Student.Id == studentId.Value); return query.ToList(); } public void DeleteAllPresence() { _context.Diaries.RemoveRange(_context.Diaries); _context.SaveChanges(); } public void DeleteGroupPresence(int groupId) { var presencesToDelete = _context.Diaries .Include(p => p.Student) .Where(p => p.Student.GroupId == groupId); _context.Diaries.RemoveRange(presencesToDelete); _context.SaveChanges(); } public void AddPresenceRecords(List items) { _context.Diaries.AddRange(items); _context.SaveChanges(); } public void UpdatePresenceRecords(List items) { foreach (var item in items) { var existingRecord = _context.Diaries .FirstOrDefault(p => p.Date == item.Date && p.NumberSubject == item.NumberSubject && p.StudentId == item.StudentId); if (existingRecord != null) { existingRecord.TrafficId = item.TrafficId; } } _context.SaveChanges(); } } }