diff --git a/Data/LocalData/Entity/Presence.cs b/Data/LocalData/Entity/Presence.cs index a296546..d3a5474 100644 --- a/Data/LocalData/Entity/Presence.cs +++ b/Data/LocalData/Entity/Presence.cs @@ -6,12 +6,11 @@ using System.Threading.Tasks; namespace Demo.domain.Models { - internal class PresenceLocalEntity + public class PresenceLocalEntity { public required Guid UserGuid { get; set; } public bool IsAttedance { get; set; } = true; public required DateOnly Date { get; set; } - public required int LessonNumber { get; set; } } } \ No newline at end of file diff --git a/Data/Repository/IPresenceRepository.cs b/Data/Repository/IPresenceRepository.cs new file mode 100644 index 0000000..8594a6c --- /dev/null +++ b/Data/Repository/IPresenceRepository.cs @@ -0,0 +1,13 @@ +using Demo.domain.Models; + +namespace Demo.Data.Repository +{ + public interface IPresenceRepository + { + List GetPresences(); + List GetPresencesByGroup(); + List GetPresencesByGroupAndDate(); + public List SavePresence(List presenceLocalEntities); + void UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid); + } +} \ No newline at end of file diff --git a/Data/Repository/PresenceRepositoryImpl.cs b/Data/Repository/PresenceRepositoryImpl.cs new file mode 100644 index 0000000..e60f6b9 --- /dev/null +++ b/Data/Repository/PresenceRepositoryImpl.cs @@ -0,0 +1,37 @@ +using Demo.domain.Models; + +namespace Demo.Data.Repository +{ + public class PresenceRepositoryImpl : IPresenceRepository + { + public List GetAllPresences = new List(); + + public List GetPresences() + { + return GetAllPresences; + } + + public List GetPresencesByGroup() + { + return GetAllPresences; + } + + public List GetPresencesByGroupAndDate() + { + return GetAllPresences; + } + + public List SavePresence(List presenceLocalEntities){ + GetAllPresences.AddRange(presenceLocalEntities); + return presenceLocalEntities; + } + + public void UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ + foreach(PresenceLocalEntity presence in GetAllPresences.Where(x => x.LessonNumber >= firstLesson && x.LessonNumber <= lastLesson && x.Date == date)){ + if (presence.UserGuid == UserGuid){ + presence.IsAttedance = false; + } + } + } + } +} \ No newline at end of file diff --git a/Domain/UseCase/IPresenceUseCase.cs b/Domain/UseCase/IPresenceUseCase.cs new file mode 100644 index 0000000..79dae8f --- /dev/null +++ b/Domain/UseCase/IPresenceUseCase.cs @@ -0,0 +1,13 @@ +using Demo.domain.Models; + +namespace Demo.Domain.UseCase +{ + public interface IPresenceUseCase + { + List GetPresencesByGroup(int groupId); + List GetPresencesByGroupAndDate(int groupId, DateOnly date); + bool UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid); + bool GeneratePresence(int firstLesson, int lastLesson, int groupId, DateOnly date); + bool GenerateWeekPresence(int firstLesson, int lastLesson, int groupId, DateOnly date); + } +} \ No newline at end of file diff --git a/Domain/UseCase/PresenceUseCase.cs b/Domain/UseCase/PresenceUseCase.cs new file mode 100644 index 0000000..797e39e --- /dev/null +++ b/Domain/UseCase/PresenceUseCase.cs @@ -0,0 +1,111 @@ +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 presenceRepository, IUserRepository repositoryUserImpl, IGroupRepository repositoryGroupImpl) + { + _repositoryPresenceImpl = presenceRepository; + _repositoryGroupImpl = repositoryGroupImpl; + _repositoryUserImpl = repositoryUserImpl; + } + + public List GetPresencesByGroup(int groupId) + { + var usersByGroup = _repositoryUserImpl.GetAllUser() + .Where(user => user.GroupID == groupId).ToList(); + + var presenceByGroup = _repositoryPresenceImpl.GetPresencesByGroup() + .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 GetPresencesByGroupAndDate(int groupId, DateOnly date) + { + var usersByGroup = _repositoryUserImpl.GetAllUser() + .Where(user => user.GroupID == groupId).ToList(); + + var presenceByGroupAndDate = _repositoryPresenceImpl.GetPresencesByGroup() + .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 presenceByGroupAndDate; + } + + public bool UpdateAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ + _repositoryPresenceImpl.UpdateAttedance(firstLesson, lastLesson, date, UserGuid); + return true; + } + + public bool GeneratePresence(int firstLesson, int lastLesson, int groupId, DateOnly date){ + List presenceList = new List{}; + var usersByGroup = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupId); + 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 = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name + }, + FIO = usersByGroup.First(user2 => user2.Guid == user.Guid).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.SavePresence(presenceLocalEntity); + + return true; + } + + public bool GenerateWeekPresence(int firstLesson, int lastLesson, int groupId, DateOnly date){ + for (int i = 0; i < 8; i++){ + GeneratePresence(firstLesson, lastLesson, groupId, date.AddDays(i)); + } + return true; + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index a97ffea..e314dce 100644 --- a/Program.cs +++ b/Program.cs @@ -9,10 +9,13 @@ IServiceCollection services = new ServiceCollection(); services .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton(); var serviceProvider = services.BuildServiceProvider(); diff --git a/UI/MainMenu.cs b/UI/MainMenu.cs index a1543d1..a97f076 100644 --- a/UI/MainMenu.cs +++ b/UI/MainMenu.cs @@ -13,10 +13,12 @@ namespace Demo.UI UserConsoleUI _userConsoleUI; GroupConsoleUI _groupConsoleUI; + PresenceConsoleUI _presenceConsoleUI; - public MainMenuUI(IUserUseCase userUseCase, IGroupUseCase groupUseCase) { + public MainMenuUI(IUserUseCase userUseCase, IGroupUseCase groupUseCase, IPresenceUseCase presenceUseCase) { _userConsoleUI = new UserConsoleUI(userUseCase); _groupConsoleUI = new GroupConsoleUI(groupUseCase); + _presenceConsoleUI = new PresenceConsoleUI(presenceUseCase); DisplayMenu(); } @@ -33,6 +35,11 @@ namespace Demo.UI case "5": _groupConsoleUI.DisplayAllGroups(); break; case "6": _groupConsoleUI.CreateNewGroup(Group.Parse(Console.ReadLine())); break; case "7": _groupConsoleUI.UpdateGroupName(Group.Parse(Console.ReadLine())); break; + case "8": _presenceConsoleUI.DisplayPresenceByGroup(Convert.ToInt32(Console.ReadLine())); break; + case "9": _presenceConsoleUI.DisplayPresenceByGroupByTime(Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break; + case "10": _presenceConsoleUI.GeneratePresence(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break; + case "11": _presenceConsoleUI.GeneratePresenceWeek(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break; + case "12": _presenceConsoleUI.IsAttedance(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine())), Guid.Parse(Console.ReadLine())); break; default: DisplayMenu(); break; diff --git a/UI/PresenceConsole.cs b/UI/PresenceConsole.cs new file mode 100644 index 0000000..cc3fd53 --- /dev/null +++ b/UI/PresenceConsole.cs @@ -0,0 +1,46 @@ +using Demo.Domain.UseCase; +using Demo.domain.Models; +using System.Text; + +namespace Demo.UI +{ + + public class PresenceConsoleUI { + + IPresenceUseCase _presenceUseCase; + public PresenceConsoleUI (IPresenceUseCase presenceUseCase){ + _presenceUseCase = presenceUseCase; + } + + public void DisplayPresenceByGroup(int groupID){ + StringBuilder stringBuilder = new StringBuilder(); + foreach(Presence presence in _presenceUseCase.GetPresencesByGroup(groupID)){ + stringBuilder.AppendLine($"{presence.User.FIO}, {presence.User.Group.Name}, {presence.IsAttedance}, {presence.Date}, {presence.LessonNumber}"); + } + Console.WriteLine(stringBuilder); + } + + public void DisplayPresenceByGroupByTime(int groupID, DateOnly date){ + StringBuilder stringBuilder = new StringBuilder(); + foreach(Presence presence in _presenceUseCase.GetPresencesByGroupAndDate(groupID, date)){ + stringBuilder.AppendLine($"{presence.User.FIO}, {presence.User.Group.Name}, {presence.IsAttedance}, {presence.Date}, {presence.LessonNumber}"); + } + Console.WriteLine(stringBuilder); + } + + public void GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date){ + string output = _presenceUseCase.GeneratePresence(firstLesson, lastLesson, groupID, date) ? "Сгенерированно" : "Не сгенерированно"; + Console.WriteLine(output); + } + + public void GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){ + string output = _presenceUseCase.GenerateWeekPresence(firstLesson, lastLesson, groupID, date) ? "Сгенерированно" : "Не сгенерированно"; + Console.WriteLine(output); + } + + public void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ + string output = _presenceUseCase.UpdateAttedance(firstLesson, lastLesson, date, UserGuid) ? "Обновлено" : "Не обновлено"; + Console.WriteLine(output); + } + } +} \ No newline at end of file