From 5e5477477be7ce6f63d0c3d806e60a4392bfbbe4 Mon Sep 17 00:00:00 2001 From: Class_Student Date: Wed, 23 Oct 2024 12:42:17 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/Data/LocalData/Entity/Presence.cs | 2 +- Demo/Data/LocalData/LocalStaticData.cs | 44 +++++++- Demo/Data/Repository/GroupRepositoryImpl.cs | 8 +- Demo/Data/Repository/IGroupRepository.cs | 2 +- Demo/Domain/UseCase/GroupUseCase.cs | 2 +- .../Domain/UseCase/UseCaseGeneratePresence.cs | 50 +++++++++ Demo/Domain/UseCase/UseCasePresence.cs | 62 +++++++++++ Demo/Program.cs | 3 +- Demo/UI/MainMenu.cs | 66 ++++++++++-- Demo/UI/PresenceConcole.cs | 102 ++++++++++++++++++ 10 files changed, 321 insertions(+), 20 deletions(-) create mode 100644 Demo/Domain/UseCase/UseCaseGeneratePresence.cs create mode 100644 Demo/Domain/UseCase/UseCasePresence.cs create mode 100644 Demo/UI/PresenceConcole.cs diff --git a/Demo/Data/LocalData/Entity/Presence.cs b/Demo/Data/LocalData/Entity/Presence.cs index 70b8d1e..71c2474 100644 --- a/Demo/Data/LocalData/Entity/Presence.cs +++ b/Demo/Data/LocalData/Entity/Presence.cs @@ -6,7 +6,7 @@ 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; diff --git a/Demo/Data/LocalData/LocalStaticData.cs b/Demo/Data/LocalData/LocalStaticData.cs index 1b904f3..5fd9fd4 100644 --- a/Demo/Data/LocalData/LocalStaticData.cs +++ b/Demo/Data/LocalData/LocalStaticData.cs @@ -11,14 +11,14 @@ namespace Demo.Data.LocalData public static class LocalStaticData { public static List groups => new List - + { new GroupLocalEntity{ Id = 1, Name = "ИП1-21" }, new GroupLocalEntity{ Id = 2, Name = "ИП1-22" }, new GroupLocalEntity{ Id = 3, Name = "ИП1-23" }, }; - - public static List users => new List + + public static List users => new List { new UserLocalEnity{Guid=Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), FIO = "RandomFio", GroupID = 1 }, new UserLocalEnity{Guid=Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), FIO = "RandomFio1", GroupID = 2 }, @@ -27,5 +27,43 @@ namespace Demo.Data.LocalData new UserLocalEnity{Guid=Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), FIO = "RandomFio4", GroupID = 2 }, new UserLocalEnity{Guid=Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), FIO = "RandomFio5", GroupID = 3 }, }; + public static List presences = new List + { + new PresenceLocalEntity { UserGuid = Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 1 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 2 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 3 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 4 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 5 }, + + new PresenceLocalEntity { UserGuid = Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 1 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 2 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 3 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 4 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 5 }, + + new PresenceLocalEntity { UserGuid = Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 1 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 2 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 3 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 4 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 5 }, + + new PresenceLocalEntity { UserGuid = Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 1 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 2 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 3 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 4 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 5 }, + + new PresenceLocalEntity { UserGuid = Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 1 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 2 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 3 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 4 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 5 }, + + new PresenceLocalEntity { UserGuid = Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 1 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 2 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 3 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 4 }, + new PresenceLocalEntity { UserGuid = Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), IsAttedance = true, Date = DateOnly.FromDateTime(DateTime.Now), LessonNumber = 5 }, + }; } } diff --git a/Demo/Data/Repository/GroupRepositoryImpl.cs b/Demo/Data/Repository/GroupRepositoryImpl.cs index a9e3818..e02cc8d 100644 --- a/Demo/Data/Repository/GroupRepositoryImpl.cs +++ b/Demo/Data/Repository/GroupRepositoryImpl.cs @@ -48,14 +48,14 @@ namespace Demo.Data.Repository public bool AddGroup(GroupLocalEntity newGroup) { - // Проверяем, существует ли группа с таким же ID + if (GetAllGroups.Any(g => g.Id == newGroup.Id)) { - return false; // Группа с таким ID уже существует + return false; } - GetAllGroups.Add(newGroup); // Добавляем новую группу - return true; // Успешное добавление + GetAllGroups.Add(newGroup); + return true; } } } diff --git a/Demo/Data/Repository/IGroupRepository.cs b/Demo/Data/Repository/IGroupRepository.cs index e18c017..fb1d75a 100644 --- a/Demo/Data/Repository/IGroupRepository.cs +++ b/Demo/Data/Repository/IGroupRepository.cs @@ -9,7 +9,7 @@ namespace Demo.Data.Repository { public interface IGroupRepository { - bool AddGroup(GroupLocalEntity newGroup); // Убедитесь, что здесь есть только один метод + bool AddGroup(GroupLocalEntity newGroup); List GetAllGroup(); GroupLocalEntity GetGroupById(int groupID); bool RemoveGroupById(int groupID); diff --git a/Demo/Domain/UseCase/GroupUseCase.cs b/Demo/Domain/UseCase/GroupUseCase.cs index 11eddef..47e68f2 100644 --- a/Demo/Domain/UseCase/GroupUseCase.cs +++ b/Demo/Domain/UseCase/GroupUseCase.cs @@ -30,7 +30,7 @@ namespace Demo.Domain.UseCase public bool AddGroup(GroupLocalEntity group) { - // Если метод возвращает true, то группа добавлена успешно + return _repositoryGroupImpl.AddGroup(group); } diff --git a/Demo/Domain/UseCase/UseCaseGeneratePresence.cs b/Demo/Domain/UseCase/UseCaseGeneratePresence.cs new file mode 100644 index 0000000..28bd9fe --- /dev/null +++ b/Demo/Domain/UseCase/UseCaseGeneratePresence.cs @@ -0,0 +1,50 @@ +using Demo.Data.LocalData; +using Demo.domain.Models; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Demo.Domain.UseCase +{ + public class UseCaseGeneratePresence + { + public List GenerateDailyPresence(int startLesson, int endLesson, int groupId, DateTime currentDate) + { + var users = LocalStaticData.users.Where(u => u.GroupID == groupId).ToList(); + var presences = new List(); + + for (int lesson = startLesson; lesson <= endLesson; lesson++) + { + foreach (var user in users) + { + presences.Add(new Presence + { + User = new User + { + Guid = user.Guid, + FIO = user.FIO, + Group = new Group { Id = groupId, Name = LocalStaticData.groups.First(g => g.Id == groupId).Name } + }, + Date = DateOnly.FromDateTime(currentDate), + LessonNumber = lesson, + IsAttedance = true + }); + } + } + return presences; + } + + public List GenerateWeeklyPresence(int startLesson, int endLesson, int groupId, DateTime startDate) + { + var presences = new List(); + + for (int i = 0; i < 7; i++) + { + DateTime currentDate = startDate.AddDays(i); + var dailyPresences = GenerateDailyPresence(startLesson, endLesson, groupId, currentDate); + presences.AddRange(dailyPresences); + } + return presences; + } + } +} diff --git a/Demo/Domain/UseCase/UseCasePresence.cs b/Demo/Domain/UseCase/UseCasePresence.cs new file mode 100644 index 0000000..5a762fa --- /dev/null +++ b/Demo/Domain/UseCase/UseCasePresence.cs @@ -0,0 +1,62 @@ +using Demo.Data.LocalData; +using Demo.domain.Models; +using System.Collections.Generic; +using System.Linq; + +namespace Demo.Domain.UseCase +{ + public class UseCasePresence + { + public List GetPresencesByGroupId(int groupId) + { + return LocalStaticData.presences + .Where(p => LocalStaticData.users.Any(u => u.Guid == p.UserGuid && u.GroupID == groupId)) + .ToList(); + } + + public List GetPresencesByGroupIdAndDate(int groupId, DateOnly date) + { + return LocalStaticData.presences + .Where(p => LocalStaticData.users.Any(u => u.Guid == p.UserGuid && u.GroupID == groupId) + && p.Date == date) + .ToList(); + } + public void MarkUserAsAbsent(Guid userGuid, int startLesson, int endLesson) + { + var presences = LocalStaticData.presences + .Where(p => p.UserGuid == userGuid && p.LessonNumber >= startLesson && p.LessonNumber <= endLesson) + .ToList(); + + foreach (var presence in presences) + { + presence.IsAttedance = false; + } + } + public void GenerateDailySchedule(int groupId, int startLesson, int endLesson) + { + var usersInGroup = LocalStaticData.users + .Where(u => u.GroupID == groupId) + .ToList(); + + Console.WriteLine($"Расписание для группы {groupId}:"); + + foreach (var user in usersInGroup) + { + for (int lesson = startLesson; lesson <= endLesson; lesson++) + { + var presence = new PresenceLocalEntity + { + UserGuid = user.Guid, + IsAttedance = true, + Date = DateOnly.FromDateTime(DateTime.Now), + LessonNumber = lesson + }; + + LocalStaticData.presences.Add(presence); + + Console.WriteLine($"User: {user.Guid}, Attended: {presence.IsAttedance}, Date: {presence.Date}, Lesson: {presence.LessonNumber}"); + } + } + } + } +} diff --git a/Demo/Program.cs b/Demo/Program.cs index 42eff79..6fe09f9 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -10,7 +10,8 @@ class Program UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl); + UseCasePresence presenceUseCase = new UseCasePresence(); - MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase); + MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase, presenceUseCase); } } diff --git a/Demo/UI/MainMenu.cs b/Demo/UI/MainMenu.cs index b8c21c4..089190f 100644 --- a/Demo/UI/MainMenu.cs +++ b/Demo/UI/MainMenu.cs @@ -5,16 +5,21 @@ using System; namespace Demo.UI { public class MainMenuUI - { - private UserConsoleUI _userConsoleUI; - private GroupConsoleUI _groupConsoleUI; +{ + private UserConsoleUI _userConsoleUI; + private GroupConsoleUI _groupConsoleUI; + private UseCasePresence _presenceUseCase; + private PresenceConsoleUI _presenceConsoleUI; + + public MainMenuUI(UserUseCase userUseCase, GroupUseCase groupUseCase, UseCasePresence presenceUseCase) + { + _userConsoleUI = new UserConsoleUI(userUseCase); + _groupConsoleUI = new GroupConsoleUI(groupUseCase); + _presenceUseCase = presenceUseCase; + _presenceConsoleUI = new PresenceConsoleUI(presenceUseCase); + DisplayMenu(); + } - public MainMenuUI(UserUseCase userUseCase, GroupUseCase groupUseCase) - { - _userConsoleUI = new UserConsoleUI(userUseCase); - _groupConsoleUI = new GroupConsoleUI(groupUseCase); - DisplayMenu(); - } private void DisplayMenu() { @@ -29,6 +34,10 @@ namespace Demo.UI Console.WriteLine("6 - Добавить группу"); Console.WriteLine("7 - Обновить группу по ID"); Console.WriteLine("8 - Удалить группу по ID"); + Console.WriteLine("9 - Вывести посещаемость по группе"); + Console.WriteLine("10 - Вывести посещаемость по группе и дате"); + Console.WriteLine("11 - Отметить пользователя как отсутствующего"); + Console.WriteLine("12 - Генерация расписания по диапозону"); Console.WriteLine("0 - Выход"); var input = Console.ReadLine(); @@ -108,6 +117,45 @@ namespace Demo.UI Console.WriteLine("Некорректный ID группы."); } break; + case "9": + Console.WriteLine("Введите ID группы для отображения посещаемости:"); + string inputGroupId = Console.ReadLine(); + if (int.TryParse(inputGroupId, out int parsedGroupId)) + { + _presenceConsoleUI.DisplayPresencesByGroupId(parsedGroupId); + } + else + { + Console.WriteLine("Некорректный ID группы."); + } + break; + case "10": + Console.WriteLine("Введите ID группы для отображения посещаемости:"); + inputGroupId = Console.ReadLine(); + if (int.TryParse(inputGroupId, out parsedGroupId)) + { + Console.WriteLine("Введите дату (yyyy-MM-dd):"); + string inputDate = Console.ReadLine(); + if (DateOnly.TryParse(inputDate, out DateOnly parsedDate)) + { + _presenceConsoleUI.DisplayPresencesByGroupIdAndDate(parsedGroupId, parsedDate); + } + else + { + Console.WriteLine("Некорректная дата."); + } + } + else + { + Console.WriteLine("Некорректный ID группы."); + } + break; + case "11": + _presenceConsoleUI.MarkUserAsAbsent(); + break; + case "12": + _presenceConsoleUI.GenerateDailySchedule(); + break; case "0": return; default: diff --git a/Demo/UI/PresenceConcole.cs b/Demo/UI/PresenceConcole.cs new file mode 100644 index 0000000..ea08e58 --- /dev/null +++ b/Demo/UI/PresenceConcole.cs @@ -0,0 +1,102 @@ +using Demo.Domain.UseCase; +using System; + +namespace Demo.UI +{ + public class PresenceConsoleUI + { + private UseCasePresence _useCasePresence; + + public PresenceConsoleUI(UseCasePresence useCasePresence) + { + _useCasePresence = useCasePresence; + } + + public void DisplayPresencesByGroupId(int groupId) + { + var presences = _useCasePresence.GetPresencesByGroupId(groupId); + Console.WriteLine($"Посещаемость для группы {groupId}:"); + foreach (var presence in presences) + { + Console.WriteLine($"User: {presence.UserGuid}, Attended: {presence.IsAttedance}, Date: {presence.Date}, Lesson: {presence.LessonNumber}"); + } + } + public void DisplayPresencesByGroupIdAndDate(int groupId, DateOnly date) + { + var presences = _useCasePresence.GetPresencesByGroupId(groupId) + .Where(p => p.Date == date) + .ToList(); + + if (presences.Count == 0) + { + Console.WriteLine($"Посещаемость отсутствует для группы {groupId} на {date.ToShortDateString()}."); + return; + } + + Console.WriteLine($"Посещаемость для группы {groupId} на {date.ToShortDateString()}:"); + foreach (var presence in presences) + { + Console.WriteLine($"User: {presence.UserGuid}, Attended: {presence.IsAttedance}, Lesson: {presence.LessonNumber}"); + } + } + public void MarkUserAsAbsent() + { + Console.WriteLine("Введите GUID пользователя:"); + if (Guid.TryParse(Console.ReadLine(), out var userGuid)) + { + Console.WriteLine("Введите номер первого занятия:"); + if (int.TryParse(Console.ReadLine(), out int startLesson)) + { + Console.WriteLine("Введите номер последнего занятия:"); + if (int.TryParse(Console.ReadLine(), out int endLesson)) + { + _useCasePresence.MarkUserAsAbsent(userGuid, startLesson, endLesson); + Console.WriteLine("Пользователь отмечен как отсутствующий."); + } + else + { + Console.WriteLine("Некорректный номер последнего занятия."); + } + } + else + { + Console.WriteLine("Некорректный номер первого занятия."); + } + } + else + { + Console.WriteLine("Некорректный GUID."); + } + } + public void GenerateDailySchedule() + { + Console.WriteLine("Введите ID группы:"); + if (int.TryParse(Console.ReadLine(), out int groupId)) + { + Console.WriteLine("Введите номер первого урока:"); + if (int.TryParse(Console.ReadLine(), out int startLesson)) + { + Console.WriteLine("Введите номер последнего урока:"); + if (int.TryParse(Console.ReadLine(), out int endLesson)) + { + _useCasePresence.GenerateDailySchedule(groupId, startLesson, endLesson); + } + else + { + Console.WriteLine("Некорректный номер последнего урока."); + } + } + else + { + Console.WriteLine("Некорректный номер первого урока."); + } + } + else + { + Console.WriteLine("Некорректный ID группы."); + } + } + + + } +} -- 2.45.2 From 6b9b5a424f9041a7c5d28c43faad3ed2f15bf9a0 Mon Sep 17 00:00:00 2001 From: Class_Student Date: Wed, 30 Oct 2024 12:11:19 +0300 Subject: [PATCH 2/2] init --- Demo/Data/{LocalData => }/Entity/Group.cs | 2 +- Demo/Data/{LocalData => }/Entity/Presence.cs | 4 +- Demo/Data/{LocalData => }/Entity/User.cs | 6 +- Demo/Data/LocalData/LocalStaticData.cs | 2 +- .../RemoteData/RemoteDataBase/DAO/Group.cs | 12 ++ .../RemoteData/RemoteDataBase/DAO/Presence.cs | 18 +++ .../RemoteData/RemoteDataBase/DAO/User.cs | 16 +++ .../RemoteDataBase/RemoteDatabaseContex.cs | 39 ++++++ Demo/Data/Repository/GroupRepositoryImpl.cs | 3 +- Demo/Data/Repository/IGroupRepository.cs | 16 +-- Demo/Data/Repository/IPresenceRepository.cs | 11 ++ Demo/Data/Repository/IUserRepository.cs | 14 +++ .../Data/Repository/SQLGroupRepositoryLmpl.cs | 90 +++++++++++++ .../Repository/SQLPresenceRepositoryLmpl.cs | 45 +++++++ Demo/Data/Repository/SQLUserRepositoeyLmpl.cs | 94 ++++++++++++++ Demo/Data/Repository/UserRepositoryImpl.cs | 4 +- Demo/Demo.csproj | 12 +- Demo/Domain/UseCase/GroupUseCase.cs | 9 +- Demo/Domain/UseCase/UseCasePresence.cs | 22 +++- Demo/Domain/UseCase/UserUseCase.cs | 21 ++-- .../20241028123800_InitialCreate.Designer.cs | 118 ++++++++++++++++++ .../20241028123800_InitialCreate.cs | 92 ++++++++++++++ .../RemoteDatabaseContexModelSnapshot.cs | 115 +++++++++++++++++ Demo/Program.cs | 28 +++-- Demo/UI/GroupConsole.cs | 3 +- Demo/UI/MainMenu.cs | 48 +++++-- Demo/UI/PresenceConcole.cs | 72 +++++++---- Demo/UI/UserConsole.cs | 4 +- 28 files changed, 831 insertions(+), 89 deletions(-) rename Demo/Data/{LocalData => }/Entity/Group.cs (90%) rename Demo/Data/{LocalData => }/Entity/Presence.cs (80%) rename Demo/Data/{LocalData => }/Entity/User.cs (84%) create mode 100644 Demo/Data/RemoteData/RemoteDataBase/DAO/Group.cs create mode 100644 Demo/Data/RemoteData/RemoteDataBase/DAO/Presence.cs create mode 100644 Demo/Data/RemoteData/RemoteDataBase/DAO/User.cs create mode 100644 Demo/Data/RemoteData/RemoteDataBase/RemoteDatabaseContex.cs create mode 100644 Demo/Data/Repository/IPresenceRepository.cs create mode 100644 Demo/Data/Repository/IUserRepository.cs create mode 100644 Demo/Data/Repository/SQLGroupRepositoryLmpl.cs create mode 100644 Demo/Data/Repository/SQLPresenceRepositoryLmpl.cs create mode 100644 Demo/Data/Repository/SQLUserRepositoeyLmpl.cs create mode 100644 Demo/Migrations/20241028123800_InitialCreate.Designer.cs create mode 100644 Demo/Migrations/20241028123800_InitialCreate.cs create mode 100644 Demo/Migrations/RemoteDatabaseContexModelSnapshot.cs diff --git a/Demo/Data/LocalData/Entity/Group.cs b/Demo/Data/Entity/Group.cs similarity index 90% rename from Demo/Data/LocalData/Entity/Group.cs rename to Demo/Data/Entity/Group.cs index 6041311..3ade4dc 100644 --- a/Demo/Data/LocalData/Entity/Group.cs +++ b/Demo/Data/Entity/Group.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Demo.domain.Models +namespace Demo.Data.Entity { public class GroupLocalEntity { diff --git a/Demo/Data/LocalData/Entity/Presence.cs b/Demo/Data/Entity/Presence.cs similarity index 80% rename from Demo/Data/LocalData/Entity/Presence.cs rename to Demo/Data/Entity/Presence.cs index 71c2474..834adc9 100644 --- a/Demo/Data/LocalData/Entity/Presence.cs +++ b/Demo/Data/Entity/Presence.cs @@ -4,12 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Demo.domain.Models +namespace Demo.Data.Entity { public class PresenceLocalEntity { public required Guid UserGuid { get; set; } - public bool IsAttedance { get; set; } = true; + public bool IsAttedance { get; set; } public required DateOnly Date { get; set; } public required int LessonNumber { get; set; } diff --git a/Demo/Data/LocalData/Entity/User.cs b/Demo/Data/Entity/User.cs similarity index 84% rename from Demo/Data/LocalData/Entity/User.cs rename to Demo/Data/Entity/User.cs index 5af4881..01ff20d 100644 --- a/Demo/Data/LocalData/Entity/User.cs +++ b/Demo/Data/Entity/User.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Demo.domain.Models +namespace Demo.Data.Entity { public class UserLocalEnity : IEquatable { @@ -13,12 +13,12 @@ namespace Demo.domain.Models public required int GroupID { get; set; } - + public bool Equals(UserLocalEnity? other) { if (other == null) return false; - return this.Guid.Equals(other.Guid); + return Guid.Equals(other.Guid); } } } diff --git a/Demo/Data/LocalData/LocalStaticData.cs b/Demo/Data/LocalData/LocalStaticData.cs index 5fd9fd4..491156e 100644 --- a/Demo/Data/LocalData/LocalStaticData.cs +++ b/Demo/Data/LocalData/LocalStaticData.cs @@ -1,4 +1,4 @@ -using Demo.domain.Models; +using Demo.Data.Entity; using System; using System.Collections.Generic; using System.Linq; diff --git a/Demo/Data/RemoteData/RemoteDataBase/DAO/Group.cs b/Demo/Data/RemoteData/RemoteDataBase/DAO/Group.cs new file mode 100644 index 0000000..818fcc7 --- /dev/null +++ b/Demo/Data/RemoteData/RemoteDataBase/DAO/Group.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; + +namespace Demo.Data.RemoteData.RemoteDataBase.DAO +{ + public class GroupDao + { + public int Id { get; set; } // Обратите внимание на заглавную букву + public required string Name { get; set; } + public IEnumerable Users { get; set; } + } +} diff --git a/Demo/Data/RemoteData/RemoteDataBase/DAO/Presence.cs b/Demo/Data/RemoteData/RemoteDataBase/DAO/Presence.cs new file mode 100644 index 0000000..d2f9b32 --- /dev/null +++ b/Demo/Data/RemoteData/RemoteDataBase/DAO/Presence.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Demo.Data.RemoteData.RemoteDataBase.DAO +{ + public class PresenceDao + { + public Guid UserGuid { get; set; } + public bool IsAttedance { get; set; } + public DateOnly Date { get; set; } + public int LessonNumber { get; set; } + public UserDao UserDao { get; set; } + + } +} diff --git a/Demo/Data/RemoteData/RemoteDataBase/DAO/User.cs b/Demo/Data/RemoteData/RemoteDataBase/DAO/User.cs new file mode 100644 index 0000000..d2f7cc5 --- /dev/null +++ b/Demo/Data/RemoteData/RemoteDataBase/DAO/User.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Demo.Data.RemoteData.RemoteDataBase.DAO +{ + public class UserDao + { + public required string F10 { get; set; } + public Guid Guid { get; set; } + public required int GroupId { get; set; } + public GroupDao Group { get; set; } + } +} diff --git a/Demo/Data/RemoteData/RemoteDataBase/RemoteDatabaseContex.cs b/Demo/Data/RemoteData/RemoteDataBase/RemoteDatabaseContex.cs new file mode 100644 index 0000000..317c8d7 --- /dev/null +++ b/Demo/Data/RemoteData/RemoteDataBase/RemoteDatabaseContex.cs @@ -0,0 +1,39 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Npgsql; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; +using Demo.Data.RemoteData.RemoteDataBase.DAO; +using Demo.domain.Models; + +namespace Demo.Data.RemoteData.RemoteDataBase +{ + public class RemoteDatabaseContex : DbContext + { + public DbSet Groups {get; set;} + public DbSet Users {get; set;} + public DbSet PresenceDaos {get; set;} + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseNpgsql("Host=45.67.56.214;Port=5421;Username=user9;Database=user9;Password=X8C8NTnD;"); + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(group => group.Id); + modelBuilder.Entity().Property(group => group.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().HasKey(user => user.Guid); + modelBuilder.Entity().Property(user => user.Guid).ValueGeneratedOnAdd(); + modelBuilder.Entity ().HasKey(presence =>new + { + presence.UserGuid, + presence.Date, + presence.IsAttedance, + presence.LessonNumber + }); + } + } +} diff --git a/Demo/Data/Repository/GroupRepositoryImpl.cs b/Demo/Data/Repository/GroupRepositoryImpl.cs index e02cc8d..0b78e50 100644 --- a/Demo/Data/Repository/GroupRepositoryImpl.cs +++ b/Demo/Data/Repository/GroupRepositoryImpl.cs @@ -1,4 +1,5 @@ -using Demo.Data.LocalData; +using Demo.Data.Entity; +using Demo.Data.LocalData; using Demo.domain.Models; using System.Collections.Generic; using System.Linq; diff --git a/Demo/Data/Repository/IGroupRepository.cs b/Demo/Data/Repository/IGroupRepository.cs index fb1d75a..97e0f59 100644 --- a/Demo/Data/Repository/IGroupRepository.cs +++ b/Demo/Data/Repository/IGroupRepository.cs @@ -1,19 +1,13 @@ -using Demo.domain.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Demo.Data.Entity; namespace Demo.Data.Repository { public interface IGroupRepository { - bool AddGroup(GroupLocalEntity newGroup); + bool AddGroup(GroupLocalEntity newGroup); List GetAllGroup(); - GroupLocalEntity GetGroupById(int groupID); - bool RemoveGroupById(int groupID); - bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup); + GroupLocalEntity GetGroupById(int groupId); + bool UpdateGroupById(int groupId, GroupLocalEntity updatedGroup); + bool RemoveGroupById(int groupId); } - } diff --git a/Demo/Data/Repository/IPresenceRepository.cs b/Demo/Data/Repository/IPresenceRepository.cs new file mode 100644 index 0000000..433174d --- /dev/null +++ b/Demo/Data/Repository/IPresenceRepository.cs @@ -0,0 +1,11 @@ +using Demo.Data.Entity; +using System.Collections.Generic; + +namespace Demo.Data.Repository +{ + public interface IPresenceRepository + { + bool AddPresence(PresenceLocalEntity newPresence); + List GetAllPresences(); + } +} diff --git a/Demo/Data/Repository/IUserRepository.cs b/Demo/Data/Repository/IUserRepository.cs new file mode 100644 index 0000000..d508279 --- /dev/null +++ b/Demo/Data/Repository/IUserRepository.cs @@ -0,0 +1,14 @@ +using Demo.Data.Entity; +using System.Collections.Generic; + +namespace Demo.Data.Repository +{ + public interface IUserRepository + { + bool AddUser(UserLocalEnity newUser); + List GetAllUsers(); + UserLocalEnity GetUserById(Guid userID); + bool RemoveUserById(Guid userID); + bool UpdateUserById(Guid userID, UserLocalEnity updatedUser); + } +} \ No newline at end of file diff --git a/Demo/Data/Repository/SQLGroupRepositoryLmpl.cs b/Demo/Data/Repository/SQLGroupRepositoryLmpl.cs new file mode 100644 index 0000000..5d39495 --- /dev/null +++ b/Demo/Data/Repository/SQLGroupRepositoryLmpl.cs @@ -0,0 +1,90 @@ +using Demo.Data.Entity; +using Demo.Data.RemoteData.RemoteDataBase; +using Demo.Data.RemoteData.RemoteDataBase.DAO; +using System.Collections.Generic; +using System.Linq; + +namespace Demo.Data.Repository +{ + class SQLGroupRepositoryLmpl : IGroupRepository + { + private readonly RemoteDatabaseContex _remoteDatabaseContext; + + public SQLGroupRepositoryLmpl(RemoteDatabaseContex remoteDatabaseContext) + { + _remoteDatabaseContext = remoteDatabaseContext; + } + + public bool AddGroup(GroupLocalEntity newGroup) + { + GroupDao groupDao = new GroupDao { Name = newGroup.Name }; + var result = _remoteDatabaseContext.Groups.Add(groupDao); + _remoteDatabaseContext.SaveChanges(); + if (result != null) return true; + return false; + } + + public List GetAllGroup() + { + var groups = _remoteDatabaseContext.Groups.ToList(); + + return groups.Select(g => new GroupLocalEntity + { + Id = g.Id, + Name = g.Name + }).ToList(); + } + + + public GroupLocalEntity GetGroupById(int groupID) + { + var group = _remoteDatabaseContext.Groups + .FirstOrDefault(g => g.Id == groupID); + + if (group == null) + { + return null; + } + + return new GroupLocalEntity + { + Id = group.Id, + Name = group.Name + }; + } + + public bool RemoveGroupById(int groupID) + { + var group = _remoteDatabaseContext.Groups + .FirstOrDefault(g => g.Id == groupID); + + if (group == null) + { + return false; + } + + _remoteDatabaseContext.Groups.Remove(group); + _remoteDatabaseContext.SaveChanges(); + + return true; + } + + public bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup) + { + var existingGroup = _remoteDatabaseContext.Groups + .FirstOrDefault(g => g.Id == groupID); + + if (existingGroup == null) + { + return false; + } + + existingGroup.Name = updatedGroup.Name; + + _remoteDatabaseContext.SaveChanges(); + + return true; + } + + } +} diff --git a/Demo/Data/Repository/SQLPresenceRepositoryLmpl.cs b/Demo/Data/Repository/SQLPresenceRepositoryLmpl.cs new file mode 100644 index 0000000..4a5e026 --- /dev/null +++ b/Demo/Data/Repository/SQLPresenceRepositoryLmpl.cs @@ -0,0 +1,45 @@ +using Demo.Data.Entity; +using Demo.Data.RemoteData.RemoteDataBase; +using Demo.Data.RemoteData.RemoteDataBase.DAO; +using System.Collections.Generic; +using System.Linq; + +namespace Demo.Data.Repository +{ + public class SQLPresenceRepositoryImpl : IPresenceRepository + { + private readonly RemoteDatabaseContex _remoteDatabaseContext; + + public SQLPresenceRepositoryImpl(RemoteDatabaseContex remoteDatabaseContext) + { + _remoteDatabaseContext = remoteDatabaseContext; + } + + public bool AddPresence(PresenceLocalEntity newPresence) + { + var presenceDao = new PresenceDao + { + UserGuid = newPresence.UserGuid, + IsAttedance = newPresence.IsAttedance, // Используем IsAttendance + Date = newPresence.Date, + LessonNumber = newPresence.LessonNumber + }; + + _remoteDatabaseContext.PresenceDaos.Add(presenceDao); + return _remoteDatabaseContext.SaveChanges() > 0; // Возвращаем true, если изменения сохранены + } + + public List GetAllPresences() + { + return _remoteDatabaseContext.PresenceDaos + .Select(p => new PresenceLocalEntity + { + UserGuid = p.UserGuid, + IsAttedance = p.IsAttedance, // Обратите внимание на правильное название свойства + Date = p.Date, + LessonNumber = p.LessonNumber + }) + .ToList(); + } + } +} diff --git a/Demo/Data/Repository/SQLUserRepositoeyLmpl.cs b/Demo/Data/Repository/SQLUserRepositoeyLmpl.cs new file mode 100644 index 0000000..728f981 --- /dev/null +++ b/Demo/Data/Repository/SQLUserRepositoeyLmpl.cs @@ -0,0 +1,94 @@ +using Demo.Data.Entity; +using Demo.Data.RemoteData.RemoteDataBase; +using Demo.Data.RemoteData.RemoteDataBase.DAO; +using System.Collections.Generic; +using System.Linq; + +namespace Demo.Data.Repository +{ + public class SQLUserRepositoryImpl : IUserRepository + { + private readonly RemoteDatabaseContex _remoteDatabaseContext; + + public SQLUserRepositoryImpl(RemoteDatabaseContex remoteDatabaseContext) + { + _remoteDatabaseContext = remoteDatabaseContext; + } + + public bool AddUser(UserLocalEnity newUser) + { + var userDao = new UserDao + { + Guid = Guid.NewGuid(), + F10 = newUser.FIO, + GroupId = newUser.GroupID + }; + + _remoteDatabaseContext.Users.Add(userDao); + + return _remoteDatabaseContext.SaveChanges() > 0; + } + + + public List GetAllUsers() + { + return _remoteDatabaseContext.Users + .Select(user => new UserLocalEnity + { + Guid = user.Guid, + FIO = user.F10, + GroupID = user.GroupId + }) + .ToList(); + } + + public UserLocalEnity GetUserById(Guid userID) + { + var user = _remoteDatabaseContext.Users.Find(userID); + + if (user == null) + { + return null; + } + return new UserLocalEnity + { + Guid = user.Guid, + GroupID = user.GroupId, + FIO = user.F10 + }; + } + + + + public bool RemoveUserById(Guid userID) + { + var user = _remoteDatabaseContext.Users.Find(userID); + + if (user == null) + { + return false; + } + + _remoteDatabaseContext.Users.Remove(user); + _remoteDatabaseContext.SaveChanges(); + + return true; + } + + + public bool UpdateUserById(Guid userID, UserLocalEnity updatedUser) + { + var user = _remoteDatabaseContext.Users.Find(userID); + + if (user == null) + { + return false; + } + user.GroupId = updatedUser.GroupID; + user.Guid = updatedUser.Guid; + _remoteDatabaseContext.SaveChanges(); + + return true; + } + } +} diff --git a/Demo/Data/Repository/UserRepositoryImpl.cs b/Demo/Data/Repository/UserRepositoryImpl.cs index bf9ae8c..ebafa42 100644 --- a/Demo/Data/Repository/UserRepositoryImpl.cs +++ b/Demo/Data/Repository/UserRepositoryImpl.cs @@ -1,5 +1,5 @@ -using Demo.Data.LocalData; -using Demo.domain.Models; +using Demo.Data.Entity; +using Demo.Data.LocalData; using System; using System.Collections.Generic; using System.Linq; diff --git a/Demo/Demo.csproj b/Demo/Demo.csproj index 1ea2759..ad196e3 100644 --- a/Demo/Demo.csproj +++ b/Demo/Demo.csproj @@ -8,7 +8,17 @@ - + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Demo/Domain/UseCase/GroupUseCase.cs b/Demo/Domain/UseCase/GroupUseCase.cs index 47e68f2..59e9507 100644 --- a/Demo/Domain/UseCase/GroupUseCase.cs +++ b/Demo/Domain/UseCase/GroupUseCase.cs @@ -1,4 +1,5 @@ -using Demo.Data.LocalData; +using Demo.Data.Entity; +using Demo.Data.LocalData; using Demo.Data.Repository; using Demo.domain.Models; using System.Collections.Generic; @@ -8,15 +9,15 @@ namespace Demo.Domain.UseCase { public class GroupUseCase { - private GroupRepositoryImpl _repositoryGroupImpl; + private IGroupRepository _repositoryGroupImpl; - public GroupUseCase(GroupRepositoryImpl repositoryGroupImpl) + public GroupUseCase(IGroupRepository repositoryGroupImpl) { _repositoryGroupImpl = repositoryGroupImpl; } public bool RemoveGroupById(int groupID) { - var group = _repositoryGroupImpl.GetAllGroups.FirstOrDefault(g => g.Id == groupID); + var group = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == groupID); if (group != null) { _repositoryGroupImpl.RemoveGroupById(groupID); diff --git a/Demo/Domain/UseCase/UseCasePresence.cs b/Demo/Domain/UseCase/UseCasePresence.cs index 5a762fa..9edb158 100644 --- a/Demo/Domain/UseCase/UseCasePresence.cs +++ b/Demo/Domain/UseCase/UseCasePresence.cs @@ -1,5 +1,5 @@ -using Demo.Data.LocalData; -using Demo.domain.Models; +using Demo.Data.Entity; +using Demo.Data.LocalData; using System.Collections.Generic; using System.Linq; @@ -32,23 +32,25 @@ namespace Demo.Domain.UseCase presence.IsAttedance = false; } } - public void GenerateDailySchedule(int groupId, int startLesson, int endLesson) + public void GenerateDailySchedule(int groupId, int startLesson, int endLesson, DateTime date) { var usersInGroup = LocalStaticData.users .Where(u => u.GroupID == groupId) .ToList(); - + + LocalStaticData.presences.RemoveAll(p => usersInGroup.Select(users => users.Guid).Contains(p.UserGuid) && p.Date == DateOnly.FromDateTime(date)); Console.WriteLine($"Расписание для группы {groupId}:"); - + foreach (var user in usersInGroup) { + for (int lesson = startLesson; lesson <= endLesson; lesson++) { var presence = new PresenceLocalEntity { UserGuid = user.Guid, IsAttedance = true, - Date = DateOnly.FromDateTime(DateTime.Now), + Date = DateOnly.FromDateTime(date), LessonNumber = lesson }; @@ -57,6 +59,14 @@ namespace Demo.Domain.UseCase Console.WriteLine($"User: {user.Guid}, Attended: {presence.IsAttedance}, Date: {presence.Date}, Lesson: {presence.LessonNumber}"); } } + + } + public void GenerateWeeklySchedule(int groupId, int startLesson, int endLesson, DateTime date) + { + for(int i = 0; i < 7;i++) + { + GenerateDailySchedule(groupId, startLesson, endLesson, date.AddDays(i)); + } } } } diff --git a/Demo/Domain/UseCase/UserUseCase.cs b/Demo/Domain/UseCase/UserUseCase.cs index 7c841c8..36dc736 100644 --- a/Demo/Domain/UseCase/UserUseCase.cs +++ b/Demo/Domain/UseCase/UserUseCase.cs @@ -1,4 +1,5 @@ -using Demo.Data.Repository; +using Demo.Data.Entity; +using Demo.Data.Repository; using Demo.domain.Models; using System; using System.Collections.Generic; @@ -8,11 +9,11 @@ namespace Demo.Domain.UseCase { public class UserUseCase { - private readonly UserRepositoryImpl _repositoryUserImpl; + private readonly IUserRepository _repositoryUserImpl; private readonly IGroupRepository _repositoryGroupImpl; - public UserUseCase(UserRepositoryImpl repositoryImpl, IGroupRepository repositoryGroupImpl) + public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl) { _repositoryUserImpl = repositoryImpl; _repositoryGroupImpl = repositoryGroupImpl; @@ -21,7 +22,7 @@ namespace Demo.Domain.UseCase private List GetAllGroups() => _repositoryGroupImpl.GetAllGroup() .Select(it => new Group { Id = it.Id, Name = it.Name }).ToList(); - public List GetAllUsers() => _repositoryUserImpl.GetAllUsers + public List GetAllUsers() => _repositoryUserImpl.GetAllUsers() .Join(_repositoryGroupImpl.GetAllGroup(), user => user.GroupID, group => group.Id, @@ -31,22 +32,22 @@ namespace Demo.Domain.UseCase public bool RemoveUserByGuid(Guid userGuid) { - return _repositoryUserImpl.RemoveUserByGuid(userGuid); + return _repositoryUserImpl.RemoveUserById(userGuid); } - public User UpdateUser(User user) + public User UpdateUser(Guid userGuid,User user) { UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; - UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); - if (result == null) throw new Exception(""); - Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID); + bool result = _repositoryUserImpl.UpdateUserById(userGuid, userLocalEnity); + if (!result) throw new Exception(""); + Group? group = GetAllGroups().FirstOrDefault(it => it.Id == user.Group.Id); if (group == null) throw new Exception(""); return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; } public User? FindUserByGuid(Guid userGuid) { - var userLocal = _repositoryUserImpl.GetUserByGuid(userGuid); + var userLocal = _repositoryUserImpl.GetUserById(userGuid); if (userLocal == null) return null; var group = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == userLocal.GroupID); diff --git a/Demo/Migrations/20241028123800_InitialCreate.Designer.cs b/Demo/Migrations/20241028123800_InitialCreate.Designer.cs new file mode 100644 index 0000000..bff793a --- /dev/null +++ b/Demo/Migrations/20241028123800_InitialCreate.Designer.cs @@ -0,0 +1,118 @@ +// +using System; +using Demo.Data.RemoteData.RemoteDataBase; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Demo.Migrations +{ + [DbContext(typeof(RemoteDatabaseContex))] + [Migration("20241028123800_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.PresenceDao", b => + { + b.Property("UserGuid") + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("date"); + + b.Property("IsAttedance") + .HasColumnType("boolean"); + + b.Property("LessonNumber") + .HasColumnType("integer"); + + b.Property("UserDaoGuid") + .HasColumnType("uuid"); + + b.HasKey("UserGuid", "Date", "IsAttedance", "LessonNumber"); + + b.HasIndex("UserDaoGuid"); + + b.ToTable("PresenceDaos"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.UserDao", b => + { + b.Property("Guid") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("F10") + .IsRequired() + .HasColumnType("text"); + + b.Property("GroupId") + .HasColumnType("integer"); + + b.HasKey("Guid"); + + b.HasIndex("GroupId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.PresenceDao", b => + { + b.HasOne("Demo.Data.RemoteData.RemoteDataBase.DAO.UserDao", "UserDao") + .WithMany() + .HasForeignKey("UserDaoGuid") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("UserDao"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.UserDao", b => + { + b.HasOne("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", b => + { + b.Navigation("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Demo/Migrations/20241028123800_InitialCreate.cs b/Demo/Migrations/20241028123800_InitialCreate.cs new file mode 100644 index 0000000..cb9335f --- /dev/null +++ b/Demo/Migrations/20241028123800_InitialCreate.cs @@ -0,0 +1,92 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Demo.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Groups", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Groups", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Guid = table.Column(type: "uuid", nullable: false), + F10 = table.Column(type: "text", nullable: false), + GroupId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Guid); + table.ForeignKey( + name: "FK_Users_Groups_GroupId", + column: x => x.GroupId, + principalTable: "Groups", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PresenceDaos", + columns: table => new + { + UserGuid = table.Column(type: "uuid", nullable: false), + IsAttedance = table.Column(type: "boolean", nullable: false), + Date = table.Column(type: "date", nullable: false), + LessonNumber = table.Column(type: "integer", nullable: false), + UserDaoGuid = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PresenceDaos", x => new { x.UserGuid, x.Date, x.IsAttedance, x.LessonNumber }); + table.ForeignKey( + name: "FK_PresenceDaos_Users_UserDaoGuid", + column: x => x.UserDaoGuid, + principalTable: "Users", + principalColumn: "Guid", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_PresenceDaos_UserDaoGuid", + table: "PresenceDaos", + column: "UserDaoGuid"); + + migrationBuilder.CreateIndex( + name: "IX_Users_GroupId", + table: "Users", + column: "GroupId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "PresenceDaos"); + + migrationBuilder.DropTable( + name: "Users"); + + migrationBuilder.DropTable( + name: "Groups"); + } + } +} diff --git a/Demo/Migrations/RemoteDatabaseContexModelSnapshot.cs b/Demo/Migrations/RemoteDatabaseContexModelSnapshot.cs new file mode 100644 index 0000000..fa613ed --- /dev/null +++ b/Demo/Migrations/RemoteDatabaseContexModelSnapshot.cs @@ -0,0 +1,115 @@ +// +using System; +using Demo.Data.RemoteData.RemoteDataBase; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Demo.Migrations +{ + [DbContext(typeof(RemoteDatabaseContex))] + partial class RemoteDatabaseContexModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.PresenceDao", b => + { + b.Property("UserGuid") + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("date"); + + b.Property("IsAttedance") + .HasColumnType("boolean"); + + b.Property("LessonNumber") + .HasColumnType("integer"); + + b.Property("UserDaoGuid") + .HasColumnType("uuid"); + + b.HasKey("UserGuid", "Date", "IsAttedance", "LessonNumber"); + + b.HasIndex("UserDaoGuid"); + + b.ToTable("PresenceDaos"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.UserDao", b => + { + b.Property("Guid") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("F10") + .IsRequired() + .HasColumnType("text"); + + b.Property("GroupId") + .HasColumnType("integer"); + + b.HasKey("Guid"); + + b.HasIndex("GroupId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.PresenceDao", b => + { + b.HasOne("Demo.Data.RemoteData.RemoteDataBase.DAO.UserDao", "UserDao") + .WithMany() + .HasForeignKey("UserDaoGuid") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("UserDao"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.UserDao", b => + { + b.HasOne("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", b => + { + b.Navigation("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Demo/Program.cs b/Demo/Program.cs index 6fe09f9..d00b47c 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -1,17 +1,29 @@ -using Demo.Data.Repository; +using Demo.Data.RemoteData.RemoteDataBase; +using Demo.Data.Repository; using Demo.Domain.UseCase; using Demo.UI; +using Microsoft.Extensions.DependencyInjection; class Program { static void Main(string[] args) { - GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); - UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); - UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); - GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl); - UseCasePresence presenceUseCase = new UseCasePresence(); - - MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase, presenceUseCase); + + IServiceCollection services = new ServiceCollection(); + services.AddDbContext() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + var serviceProvaider = services.BuildServiceProvider(); + MainMenuUI mainMenuUI= serviceProvaider.GetService(); + mainMenuUI.DisplayMenu(); } } diff --git a/Demo/UI/GroupConsole.cs b/Demo/UI/GroupConsole.cs index 23a035c..e289e3e 100644 --- a/Demo/UI/GroupConsole.cs +++ b/Demo/UI/GroupConsole.cs @@ -1,4 +1,5 @@ -using Demo.Data.LocalData; +using Demo.Data.Entity; +using Demo.Data.LocalData; using Demo.domain.Models; using Demo.Domain.UseCase; using System; diff --git a/Demo/UI/MainMenu.cs b/Demo/UI/MainMenu.cs index 089190f..78c0920 100644 --- a/Demo/UI/MainMenu.cs +++ b/Demo/UI/MainMenu.cs @@ -11,17 +11,15 @@ namespace Demo.UI private UseCasePresence _presenceUseCase; private PresenceConsoleUI _presenceConsoleUI; - public MainMenuUI(UserUseCase userUseCase, GroupUseCase groupUseCase, UseCasePresence presenceUseCase) + public MainMenuUI(UserConsoleUI userConsoleUI, GroupConsoleUI groupConsoleUI, PresenceConsoleUI presenceConsoleUI) { - _userConsoleUI = new UserConsoleUI(userUseCase); - _groupConsoleUI = new GroupConsoleUI(groupUseCase); - _presenceUseCase = presenceUseCase; - _presenceConsoleUI = new PresenceConsoleUI(presenceUseCase); - DisplayMenu(); + _userConsoleUI = userConsoleUI; + _groupConsoleUI = groupConsoleUI; + _presenceConsoleUI = presenceConsoleUI; } - private void DisplayMenu() + public void DisplayMenu() { while (true) { @@ -38,6 +36,7 @@ namespace Demo.UI Console.WriteLine("10 - Вывести посещаемость по группе и дате"); Console.WriteLine("11 - Отметить пользователя как отсутствующего"); Console.WriteLine("12 - Генерация расписания по диапозону"); + Console.WriteLine("13 - Генерация расписания по диапозону на неделю"); Console.WriteLine("0 - Выход"); var input = Console.ReadLine(); @@ -74,7 +73,7 @@ namespace Demo.UI Group = new Group { Id = newGroupId, Name = newGroupName } }; - _userConsoleUI.UpdateUser(updatedUser); + _userConsoleUI.UpdateUser(updateGuid, updatedUser); } else { @@ -151,11 +150,42 @@ namespace Demo.UI } break; case "11": - _presenceConsoleUI.MarkUserAsAbsent(); + Console.WriteLine("Введите GUID пользователя для отметки отсутствия:"); + if (Guid.TryParse(Console.ReadLine(), out var absenceUserGuid)) + { + Console.WriteLine("Введите дату (в формате ГГГГ-ММ-ДД):"); + if (DateOnly.TryParse(Console.ReadLine(), out var absenceDate)) + { + Console.WriteLine("Введите диапазон уроков (например, 2-4):"); + var lessonRange = Console.ReadLine().Split('-'); + if (lessonRange.Length == 2 && + int.TryParse(lessonRange[0], out var startLesson) && + int.TryParse(lessonRange[1], out var endLesson)) + { + // Вызов метода для отметки отсутствия + _presenceConsoleUI.MarkUserAsAbsent(absenceUserGuid, absenceDate, startLesson, endLesson); + } + else + { + Console.WriteLine("Некорректный диапазон уроков."); + } + } + else + { + Console.WriteLine("Некорректная дата."); + } + } + else + { + Console.WriteLine("Некорректный GUID."); + } break; case "12": _presenceConsoleUI.GenerateDailySchedule(); break; + case "13": + _presenceConsoleUI.GenerateWeeklySchedule(); + break; case "0": return; default: diff --git a/Demo/UI/PresenceConcole.cs b/Demo/UI/PresenceConcole.cs index ea08e58..4870011 100644 --- a/Demo/UI/PresenceConcole.cs +++ b/Demo/UI/PresenceConcole.cs @@ -1,4 +1,7 @@ -using Demo.Domain.UseCase; +using Demo.Data.Entity; +using Demo.Data.LocalData; +using Demo.domain.Models; +using Demo.Domain.UseCase; using System; namespace Demo.UI @@ -39,33 +42,21 @@ namespace Demo.UI Console.WriteLine($"User: {presence.UserGuid}, Attended: {presence.IsAttedance}, Lesson: {presence.LessonNumber}"); } } - public void MarkUserAsAbsent() + public void MarkUserAsAbsent(Guid userGuid, DateOnly date, int startLesson, int endLesson) { - Console.WriteLine("Введите GUID пользователя:"); - if (Guid.TryParse(Console.ReadLine(), out var userGuid)) + _useCasePresence.MarkUserAsAbsent(userGuid, startLesson, endLesson); + for (int lesson = startLesson; lesson <= endLesson; lesson++) { - Console.WriteLine("Введите номер первого занятия:"); - if (int.TryParse(Console.ReadLine(), out int startLesson)) + var presence = new PresenceLocalEntity { - Console.WriteLine("Введите номер последнего занятия:"); - if (int.TryParse(Console.ReadLine(), out int endLesson)) - { - _useCasePresence.MarkUserAsAbsent(userGuid, startLesson, endLesson); - Console.WriteLine("Пользователь отмечен как отсутствующий."); - } - else - { - Console.WriteLine("Некорректный номер последнего занятия."); - } - } - else - { - Console.WriteLine("Некорректный номер первого занятия."); - } - } - else - { - Console.WriteLine("Некорректный GUID."); + UserGuid = userGuid, + IsAttedance = false, + Date = date, + LessonNumber = lesson + }; + + // Вывод информации о отметке + Console.WriteLine($"User: {userGuid}, Attended: {presence.IsAttedance}, Date: {presence.Date}, Lesson: {presence.LessonNumber}"); } } public void GenerateDailySchedule() @@ -79,7 +70,35 @@ namespace Demo.UI Console.WriteLine("Введите номер последнего урока:"); if (int.TryParse(Console.ReadLine(), out int endLesson)) { - _useCasePresence.GenerateDailySchedule(groupId, startLesson, endLesson); + _useCasePresence.GenerateDailySchedule(groupId, startLesson, endLesson,DateTime.Now); + } + else + { + Console.WriteLine("Некорректный номер последнего урока."); + } + } + else + { + Console.WriteLine("Некорректный номер первого урока."); + } + } + else + { + Console.WriteLine("Некорректный ID группы."); + } + } + public void GenerateWeeklySchedule() + { + Console.WriteLine("Введите ID группы:"); + if (int.TryParse(Console.ReadLine(), out int groupId)) + { + Console.WriteLine("Введите номер первого урока:"); + if (int.TryParse(Console.ReadLine(), out int startLesson)) + { + Console.WriteLine("Введите номер последнего урока:"); + if (int.TryParse(Console.ReadLine(), out int endLesson)) + { + _useCasePresence.GenerateWeeklySchedule(groupId, startLesson, endLesson, DateTime.Now); } else { @@ -97,6 +116,5 @@ namespace Demo.UI } } - } } diff --git a/Demo/UI/UserConsole.cs b/Demo/UI/UserConsole.cs index 562b890..f3233b1 100644 --- a/Demo/UI/UserConsole.cs +++ b/Demo/UI/UserConsole.cs @@ -43,9 +43,9 @@ namespace Demo.UI } } - public void UpdateUser(User updatedUser) + public void UpdateUser(Guid userGuid, User updatedUser) { - var result = _userUseCase.UpdateUser(updatedUser); + var result = _userUseCase.UpdateUser(userGuid, updatedUser); if (result != null) { Console.WriteLine("Пользователь обновлён."); -- 2.45.2