using Demo.Data.RemoteData.RemoteDataBase.DAO; using Demo.Data.RemoteData.RemoteDataBase; using Demo.Data.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace data.Repository { public class SQLPresenceRepositoryImpl : IPresenceRepository { private readonly RemoteDatabaseContext _remoteDatabaseContext; public SQLPresenceRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext) { _remoteDatabaseContext = remoteDatabaseContext; } public bool AddPresence(PresenceDao presence) { var presenceDao = new PresenceDao { Date = presence.Date, ClassNumber = presence.ClassNumber, UserId = presence.UserId, GroupId = presence.GroupId }; _remoteDatabaseContext.PresenceDaos.Add(presenceDao); _remoteDatabaseContext.SaveChanges(); return true; } public List GetPresence(int GroupId, DateOnly startData, DateOnly endData, int UserId) { var presenceList = _remoteDatabaseContext.PresenceDaos.Where(presence => presence.GroupId == GroupId && presence.UserId == UserId && presence.Date >= startData && presence.Date <= endData).ToList(); return presenceList; } public List GetPresenceByGroup(int groupId) { var listPresences = _remoteDatabaseContext.PresenceDaos .Where(x => x.GroupId == groupId).ToList(); return listPresences; } public List GetPresenceByGroupAndDate(int groupId, DateOnly date) { var listPresences = _remoteDatabaseContext.PresenceDaos .Where(x => x.GroupId == groupId && x.Date == date).ToList(); return listPresences; } public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId) { var presToUpdate = _remoteDatabaseContext.PresenceDaos .Where(x => x.UserId == userId && x.ClassNumber >= firstClass && x.ClassNumber <= lastClass && x.Date == date).ToList(); foreach (var pres in presToUpdate) { pres.IsAttendence = false; } _remoteDatabaseContext.SaveChanges(); return true; } public bool DeletePresenceByGroup(int groupId) { var presenceToDelete = _remoteDatabaseContext.PresenceDaos.Where(x => x.GroupId == groupId).ToList(); if (presenceToDelete.Count > 0) { foreach (var presence in presenceToDelete) { _remoteDatabaseContext.PresenceDaos.Remove(presence); } _remoteDatabaseContext.SaveChanges(); return true; } return false; } public bool DeletePresenceByUser(int userId) { var presenceToDelete = _remoteDatabaseContext.PresenceDaos.Where(x => x.UserId == userId).ToList(); if (presenceToDelete.Count > 0) { foreach (var presence in presenceToDelete) { _remoteDatabaseContext.PresenceDaos.Remove(presence); } _remoteDatabaseContext.SaveChanges(); return true; } return false; } public bool DeletePresenceByDate(DateOnly startData, DateOnly endData) { var PresenceToDelete = _remoteDatabaseContext.PresenceDaos.Where(x => x.Date == startData).ToList(); for (var i = startData; i < endData; i = i.AddDays(1)) { PresenceToDelete.AddRange(_remoteDatabaseContext.PresenceDaos.Where(x => x.Date == i).ToList()); } if (PresenceToDelete.Count > 0) { foreach (var presence in PresenceToDelete) { _remoteDatabaseContext.PresenceDaos.Remove(presence); } _remoteDatabaseContext.SaveChanges(); return true; } return false; } } }