using System.Security.Cryptography.X509Certificates; using Demo.Data.Repository; using Demo.Domain.Models; namespace Demo.Domain.UseCase { public class PresenceUseCase : IPresenceUseCase { private readonly IUserRepository _repositoryUserImpl; private readonly IPresenceRepository _repositoryPresenceImpl; private readonly IGroupRepository _repositoryGroupImpl; public PresenceUseCase(IPresenceRepository repositoryPresenceImpl, IUserRepository repositoryUserImpl, IGroupRepository repositoryGroupImpl) { _repositoryPresenceImpl = repositoryPresenceImpl; _repositoryUserImpl = repositoryUserImpl; _repositoryGroupImpl = repositoryGroupImpl; } public List GetPresenceByGroup(int groupID){ var usersByGroup = _repositoryUserImpl.GetAllUser() .Where(user => user.GroupID == groupID).ToList(); var presenceByGroup = _repositoryPresenceImpl.GetAllPresences() .Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid)) .Select(presence => new Presence{ User = new User{ Guid = presence.UserGuid, Group = new Group{ ID = groupID, Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name }, FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO, }, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttedance = presence.IsAttedance }).ToList(); return presenceByGroup; } public List GetFalsePresenceByGroup(int groupID){ var usersByGroup = _repositoryUserImpl.GetAllUser() .Where(user => user.GroupID == groupID).ToList(); var presenceByGroup = _repositoryPresenceImpl.GetAllPresences() .Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid) && x.IsAttedance == false) .Select(presence => new Presence{ User = new User{ Guid = presence.UserGuid, Group = new Group{ ID = groupID, Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name }, FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO, }, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttedance = presence.IsAttedance }).ToList(); return presenceByGroup; } public List GetPresenceByUser(Guid userGuid){ var user = _repositoryUserImpl.GetUserByGuid(userGuid); var presenceByUser = _repositoryPresenceImpl.GetAllPresences() .Where(user => userGuid == user.UserGuid) .Select(presence => new Presence{ User = new User{ Guid = user.Guid, Group = new Group{ ID = user.GroupID, Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == user.GroupID).Name }, FIO = user.FIO }, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttedance = presence.IsAttedance }).ToList(); return presenceByUser; } public List GetFalsePresenceByUser(Guid userGuid){ var user = _repositoryUserImpl.GetUserByGuid(userGuid); var presenceByUser = _repositoryPresenceImpl.GetAllPresences() .Where(user => userGuid == user.UserGuid && user.IsAttedance == false) .Select(presence => new Presence{ User = new User{ Guid = user.Guid, Group = new Group{ ID = user.GroupID, Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == user.GroupID).Name }, FIO = user.FIO }, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttedance = presence.IsAttedance }).ToList(); return presenceByUser; } public List GetPresenceByGroupByTime(int groupID, DateOnly date){ var usersByGroup = _repositoryUserImpl.GetAllUser() .Where(user => user.GroupID == groupID).ToList(); var presenceByGroup = _repositoryPresenceImpl.GetAllPresences() .Where(x => usersByGroup.Any(user => user.Guid == x.UserGuid && x.Date == date)) .Select(presence => new Presence{ User = new User{ Guid = presence.UserGuid, Group = new Group{ ID = groupID, Name = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name }, FIO = usersByGroup.First(user => user.Guid == presence.UserGuid).FIO, }, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttedance = presence.IsAttedance }).ToList(); return presenceByGroup; } public PresenceResponse GetPresencebyAll(int GroupID, DateOnly? start = null, DateOnly? end = null, Guid userGuid = default){ IEnumerable? users = null; if (userGuid == default) { users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == GroupID); } else { users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == GroupID && x.Guid == userGuid); } var usersFor = new List(); foreach(var user in users){ IEnumerable presences; if (userGuid != default){ presences = _repositoryPresenceImpl.GetAllPresences().Where(x => x.UserGuid == userGuid).ToList(); }else{ presences = _repositoryPresenceImpl.GetAllPresences().ToList(); } foreach(var presence in presences) { if (start.HasValue && end.HasValue) { if (presence.Date >= start && presence.Date <= end) { usersFor.Add(new UserPresenceInfo{ FIO = user.FIO, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttendance = presence.IsAttedance }); } } else { usersFor.Add(new UserPresenceInfo{ FIO = user.FIO, LessonNumber = presence.LessonNumber, Date = presence.Date, IsAttendance = presence.IsAttedance }); } } } var presenceGet = new PresenceResponse{ GroupName = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(x => x.ID == GroupID).Name, Users = usersFor }; return presenceGet; } public bool IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ _repositoryPresenceImpl.IsAttedance(firstLesson, lastLesson, date, UserGuid); return true; } public bool GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date) { var usersByGroup = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupID).ToList(); var groupName = _repositoryGroupImpl.GetAllGroup().First(group => group.ID == groupID).Name; List presenceList = new List(); for (int i = firstLesson; i <= lastLesson; i++) { foreach (UserLocalEntity user in usersByGroup) { presenceList.Add(new Presence { User = new User { Group = new Group { ID = groupID, Name = groupName }, FIO = user.FIO, Guid = user.Guid }, IsAttedance = true, Date = date, LessonNumber = i }); } } var presenceLocalEntity = presenceList.Select(x => new PresenceLocalEntity { UserGuid = x.User.Guid, IsAttedance = x.IsAttedance, LessonNumber = x.LessonNumber, Date = x.Date }).ToList(); _repositoryPresenceImpl.GeneratePresence(presenceLocalEntity); return true; } public bool DeletePresence(){ return _repositoryPresenceImpl.DeletePresence(); } public bool DeletePresenceByUser(Guid userGuid) { return _repositoryPresenceImpl.DeletePresenceByUserGuid(userGuid); } public bool DeletePresenceByGroup(int groupID) { return _repositoryPresenceImpl.DeletePresenceByGroup(groupID); } public bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate) { return _repositoryPresenceImpl.DeletePresenceByDateRange(startDate, endDate); } public bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){ for (int i = 0; i < 8; i++){ GeneratePresence(firstLesson, lastLesson, groupID, date.AddDays(i)); } return true; } } }