using Demo.Data.RemoteData.RemoteDataBase; using Demo.Domain.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; namespace Demo.Data.Repository { public class SQLPresenceRepository : IPresenceRepository { private readonly DbContext _context; public SQLPresenceRepository(DbContext context) { _context = context; } public void AddPresence(Presence presence) { presence.Date = DateTime.SpecifyKind(presence.Date, DateTimeKind.Utc); _context.Set().Add(new RemoteData.RemoteDataBase.DAO.Presence { Id = presence.Id, Date = presence.Date, LessonNumber = presence.LessonNumber, IsAttendance = presence.IsAttendance, UserId = presence.UserId }); _context.SaveChanges(); } public void AddPresence(RemoteData.RemoteDataBase.DAO.Presence presence) { presence.Date = DateTime.SpecifyKind(presence.Date, DateTimeKind.Utc); _context.Set().Add(presence); _context.SaveChanges(); } public IEnumerable GetPresenceByGroup(int groupId) { return _context.Set() .Where(p => _context.Set().Any(u => u.Id == p.UserId && u.GroupID == groupId)) .Select(p => new Presence { Id = p.Id, Date = p.Date, LessonNumber = p.LessonNumber, IsAttendance = p.IsAttendance, UserId = p.UserId }) .ToList(); } public IEnumerable GetPresenceByGroupAndDate(int groupId, DateTime date) { date = DateTime.SpecifyKind(date, DateTimeKind.Utc); return _context.Set() .Where(p => _context.Set().Any(u => u.Id == p.UserId && u.GroupID == groupId) && p.Date == date) .Select(p => new Presence { Id = p.Id, Date = p.Date, LessonNumber = p.LessonNumber, IsAttendance = p.IsAttendance, UserId = p.UserId }) .ToList(); } public void MarkUserAsAbsent(Guid userId, int lessonNumber, DateTime date) { date = DateTime.SpecifyKind(date, DateTimeKind.Utc); var presence = _context.Set() .FirstOrDefault(p => p.UserId == userId && p.LessonNumber == lessonNumber && p.Date == date); if (presence != null) { presence.IsAttendance = false; _context.SaveChanges(); } } public IEnumerable GetAllPresence() { return _context.Set() .Select(p => new Presence { Id = p.Id, Date = p.Date, LessonNumber = p.LessonNumber, IsAttendance = p.IsAttendance, UserId = p.UserId }) .ToList(); } public void DeletePresence(Guid id) { var presence = _context.Set().Find(id); if (presence != null) { _context.Set().Remove(presence); _context.SaveChanges(); } } public void UpdatePresence(Presence presence) { var existingPresence = _context.Set().Find(presence.Id); if (existingPresence != null) { existingPresence.Date = presence.Date; existingPresence.LessonNumber = presence.LessonNumber; existingPresence.IsAttendance = presence.IsAttendance; existingPresence.UserId = presence.UserId; _context.SaveChanges(); } } } }