From 48479e25a53a2e128d358370fb3f2167427e1032 Mon Sep 17 00:00:00 2001 From: 1billy17 Date: Thu, 24 Oct 2024 23:41:31 +0300 Subject: [PATCH] Db --- Data/{LocalData => }/Entity/Group.cs | 0 Data/{LocalData => }/Entity/Presence.cs | 0 Data/{LocalData => }/Entity/User.cs | 0 Data/RemoteData/RemoteDataBase/DAO/Group.cs | 10 ++ .../RemoteData/RemoteDataBase/DAO/Presence.cs | 12 ++ Data/RemoteData/RemoteDataBase/DAO/User.cs | 12 ++ .../RemoteDataBase/RemoteDatabaseContext.cs | 35 ++++++ Data/Repository/GroupRepositoryImpl.cs | 54 ++++---- Data/Repository/IGroupRepository.cs | 2 +- Data/Repository/IPresenceRepository.cs | 2 +- Data/Repository/PresenceRepositoryImpl.cs | 51 ++++++-- Data/Repository/UserRepositoryImpl.cs | 42 ++++--- Demo.csproj | 10 +- Domain/UseCase/GroupUseCase.cs | 14 +-- Domain/UseCase/IGroupUseCase.cs | 4 +- Domain/UseCase/PresenceUseCase.cs | 2 +- Domain/UseCase/UserUseCase.cs | 67 ++++++---- .../20241024202926_InitialCreate.Designer.cs | 118 ++++++++++++++++++ Migrations/20241024202926_InitialCreate.cs | 92 ++++++++++++++ .../RemoteDatabaseContextModelSnapshot.cs | 115 +++++++++++++++++ Program.cs | 12 +- UI/GroupConsole.cs | 26 +++- UI/MainMenu.cs | 22 ++-- UI/PresenceConsole.cs | 2 +- 24 files changed, 591 insertions(+), 113 deletions(-) rename Data/{LocalData => }/Entity/Group.cs (100%) rename Data/{LocalData => }/Entity/Presence.cs (100%) rename Data/{LocalData => }/Entity/User.cs (100%) create mode 100644 Data/RemoteData/RemoteDataBase/DAO/Group.cs create mode 100644 Data/RemoteData/RemoteDataBase/DAO/Presence.cs create mode 100644 Data/RemoteData/RemoteDataBase/DAO/User.cs create mode 100644 Data/RemoteData/RemoteDataBase/RemoteDatabaseContext.cs create mode 100644 Migrations/20241024202926_InitialCreate.Designer.cs create mode 100644 Migrations/20241024202926_InitialCreate.cs create mode 100644 Migrations/RemoteDatabaseContextModelSnapshot.cs diff --git a/Data/LocalData/Entity/Group.cs b/Data/Entity/Group.cs similarity index 100% rename from Data/LocalData/Entity/Group.cs rename to Data/Entity/Group.cs diff --git a/Data/LocalData/Entity/Presence.cs b/Data/Entity/Presence.cs similarity index 100% rename from Data/LocalData/Entity/Presence.cs rename to Data/Entity/Presence.cs diff --git a/Data/LocalData/Entity/User.cs b/Data/Entity/User.cs similarity index 100% rename from Data/LocalData/Entity/User.cs rename to Data/Entity/User.cs diff --git a/Data/RemoteData/RemoteDataBase/DAO/Group.cs b/Data/RemoteData/RemoteDataBase/DAO/Group.cs new file mode 100644 index 0000000..abd6eb9 --- /dev/null +++ b/Data/RemoteData/RemoteDataBase/DAO/Group.cs @@ -0,0 +1,10 @@ + +namespace Demo.Data.RemoteData.RemoteDataBase.DAO +{ + public class GroupDao { + + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable User { get; set; } + } +} \ No newline at end of file diff --git a/Data/RemoteData/RemoteDataBase/DAO/Presence.cs b/Data/RemoteData/RemoteDataBase/DAO/Presence.cs new file mode 100644 index 0000000..62b1ab6 --- /dev/null +++ b/Data/RemoteData/RemoteDataBase/DAO/Presence.cs @@ -0,0 +1,12 @@ + +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; } + } +} \ No newline at end of file diff --git a/Data/RemoteData/RemoteDataBase/DAO/User.cs b/Data/RemoteData/RemoteDataBase/DAO/User.cs new file mode 100644 index 0000000..b636c33 --- /dev/null +++ b/Data/RemoteData/RemoteDataBase/DAO/User.cs @@ -0,0 +1,12 @@ +using System.Text.RegularExpressions; + +namespace Demo.Data.RemoteData.RemoteDataBase.DAO +{ + public class UserDao + { + public string FIO { get; set; } + public Guid Guid {get; set; } + public int GroupID {get; set;} + public GroupDao Group {get; set; } + } +} \ No newline at end of file diff --git a/Data/RemoteData/RemoteDataBase/RemoteDatabaseContext.cs b/Data/RemoteData/RemoteDataBase/RemoteDatabaseContext.cs new file mode 100644 index 0000000..438762c --- /dev/null +++ b/Data/RemoteData/RemoteDataBase/RemoteDatabaseContext.cs @@ -0,0 +1,35 @@ +using Demo.Data.RemoteData.RemoteDataBase.DAO; +using Microsoft.EntityFrameworkCore; + +namespace Data.RemoteData.RemoteDataBase.DAO +{ + + public class RemoteDatabaseContext: 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=localhost;" + + "Port=5432;" + + "Username=postgres;" + + "Password=1234;" + + "Database=postgres"); + } + + 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 + }); + } + } +} \ No newline at end of file diff --git a/Data/Repository/GroupRepositoryImpl.cs b/Data/Repository/GroupRepositoryImpl.cs index a15ec08..b71de89 100644 --- a/Data/Repository/GroupRepositoryImpl.cs +++ b/Data/Repository/GroupRepositoryImpl.cs @@ -5,54 +5,60 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Data.RemoteData.RemoteDataBase.DAO; +using Demo.Data.RemoteData.RemoteDataBase.DAO; namespace Demo.Data.Repository { - public class GroupRepositoryImpl:IGroupRepository + public class SQLGroupRepositoryImpl : IGroupRepository { - public GroupRepositoryImpl() { - GetAllGroups = LocalStaticData.groups; + private readonly RemoteDatabaseContext _remoteDatabaseContext; + + public SQLGroupRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext) { + + _remoteDatabaseContext = remoteDatabaseContext; + GetAllGroups = _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, Id = x.Id}).ToList(); } public List GetAllGroups { get; set; } public List GetAllGroup() { - return GetAllGroups; + return _remoteDatabaseContext.Groups.Select(x => new GroupLocalEntity{Name = x.Name, Id = x.Id}).ToList(); } public GroupLocalEntity? UpdateGroupName(GroupLocalEntity groupUpdateLocalEntity) { - int index = GetAllGroups.FindIndex(x => x.Id == groupUpdateLocalEntity.Id); - if (index == -1) return null; - GetAllGroups[index].Name = groupUpdateLocalEntity.Name; - return GetAllGroups[index]; + var group = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Id == groupUpdateLocalEntity.Id); + if (group == null){ + return null; + } + + group.Name = groupUpdateLocalEntity.Name; + _remoteDatabaseContext.SaveChanges(); + return new GroupLocalEntity{Name = group.Name, Id = group.Id}; } - public GroupLocalEntity? CreateGroup(GroupLocalEntity groupCreateLocalEntity) { + public GroupLocalEntity? CreateGroup(string Name) { - if (GetAllGroups.Any(x => x.Id == groupCreateLocalEntity.Id)) return null; - - GetAllGroups.Add(groupCreateLocalEntity); - return groupCreateLocalEntity; + GroupDao groupDAO = new GroupDao{Name = Name}; + var result = _remoteDatabaseContext.Groups.Add(groupDAO); + _remoteDatabaseContext.SaveChanges(); + return new GroupLocalEntity{Name = groupDAO.Name, Id = groupDAO.Id}; } public bool RemoveGroupById(int groupID) { - GroupLocalEntity? groupLocal = GetAllGroups - .Where(x => x.Id == groupID).FirstOrDefault(); - if (groupLocal == null) return false; - - return GetAllGroups.Remove(groupLocal); + var groupDao = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Id == groupID); + _remoteDatabaseContext.Groups.Remove(groupDao); + _remoteDatabaseContext.SaveChanges(); + return true; } - public GroupLocalEntity? GetGroupById(int groupID) { - GroupLocalEntity? groupLocal = GetAllGroups - .Where(x => x.Id == groupID).FirstOrDefault(); - if (groupLocal == null) return null; - - return groupLocal; + public GroupLocalEntity? GetGroupById(int groupId) { + var groupDao = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Id == groupId); + return new GroupLocalEntity{Name = groupDao.Name, Id = groupDao.Id}; } } } diff --git a/Data/Repository/IGroupRepository.cs b/Data/Repository/IGroupRepository.cs index 58f990b..0ba2c67 100644 --- a/Data/Repository/IGroupRepository.cs +++ b/Data/Repository/IGroupRepository.cs @@ -13,6 +13,6 @@ namespace Demo.Data.Repository bool RemoveGroupById(int groupID); GroupLocalEntity? UpdateGroupName(GroupLocalEntity updatedGroup); GroupLocalEntity? GetGroupById(int groupID); - GroupLocalEntity? CreateGroup(GroupLocalEntity newGroup); + GroupLocalEntity? CreateGroup(string Name); } } \ No newline at end of file diff --git a/Data/Repository/IPresenceRepository.cs b/Data/Repository/IPresenceRepository.cs index 8594a6c..fd187d2 100644 --- a/Data/Repository/IPresenceRepository.cs +++ b/Data/Repository/IPresenceRepository.cs @@ -7,7 +7,7 @@ namespace Demo.Data.Repository List GetPresences(); List GetPresencesByGroup(); List GetPresencesByGroupAndDate(); - public List SavePresence(List presenceLocalEntities); + List GeneratePresence(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 index e60f6b9..7690638 100644 --- a/Data/Repository/PresenceRepositoryImpl.cs +++ b/Data/Repository/PresenceRepositoryImpl.cs @@ -1,37 +1,66 @@ +using Data.RemoteData.RemoteDataBase.DAO; +using Demo.Data.RemoteData.RemoteDataBase.DAO; using Demo.domain.Models; namespace Demo.Data.Repository { - public class PresenceRepositoryImpl : IPresenceRepository + public class SQLPresenceRepositoryImpl : IPresenceRepository { - public List GetAllPresences = new List(); + + private readonly RemoteDatabaseContext _remoteDatabaseContext; + + public SQLPresenceRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){ + _remoteDatabaseContext = remoteDatabaseContext; + GetAllPresence = _remoteDatabaseContext.PresenceDaos.Select(x => new PresenceLocalEntity{UserGuid = x.UserGuid, Date = x.Date, LessonNumber = x.LessonNumber, IsAttedance = x.IsAttedance}).ToList(); + } + + public List GetAllPresence = new List{}; public List GetPresences() { - return GetAllPresences; + return _remoteDatabaseContext.PresenceDaos.Select(x => new PresenceLocalEntity{UserGuid = x.UserGuid, Date = x.Date, LessonNumber = x.LessonNumber, IsAttedance = x.IsAttedance}).ToList(); } public List GetPresencesByGroup() { - return GetAllPresences; + return _remoteDatabaseContext.PresenceDaos.Select(x => new PresenceLocalEntity{UserGuid = x.UserGuid, Date = x.Date, LessonNumber = x.LessonNumber, IsAttedance = x.IsAttedance}).ToList(); } public List GetPresencesByGroupAndDate() { - return GetAllPresences; + return _remoteDatabaseContext.PresenceDaos.Select(x => new PresenceLocalEntity{UserGuid = x.UserGuid, Date = x.Date, LessonNumber = x.LessonNumber, IsAttedance = x.IsAttedance}).ToList(); } - public List SavePresence(List presenceLocalEntities){ - GetAllPresences.AddRange(presenceLocalEntities); + public List GeneratePresence(List presenceLocalEntities) + { + var presences = presenceLocalEntities.Select(x => new PresenceDao + { + UserGuid = x.UserGuid, + IsAttedance = x.IsAttedance, + LessonNumber = x.LessonNumber, + Date = x.Date, + userDao = _remoteDatabaseContext.Users.FirstOrDefault(u => u.Guid == x.UserGuid) + }).ToList(); + + _remoteDatabaseContext.PresenceDaos.AddRange(presences); + _remoteDatabaseContext.SaveChanges(); + 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; - } + var presencesToUpdate = _remoteDatabaseContext.PresenceDaos + .Where(x => x.LessonNumber >= firstLesson + && x.LessonNumber <= lastLesson + && x.Date == date + && x.UserGuid == UserGuid) + .ToList(); + + foreach(var presence in presencesToUpdate){ + presence.IsAttedance = false; } + + _remoteDatabaseContext.SaveChanges(); } } } \ No newline at end of file diff --git a/Data/Repository/UserRepositoryImpl.cs b/Data/Repository/UserRepositoryImpl.cs index 7b46f98..2e7a302 100644 --- a/Data/Repository/UserRepositoryImpl.cs +++ b/Data/Repository/UserRepositoryImpl.cs @@ -1,3 +1,4 @@ +using Data.RemoteData.RemoteDataBase.DAO; using Demo.Data.LocalData; using Demo.domain.Models; using System; @@ -8,43 +9,46 @@ using System.Threading.Tasks; namespace Demo.Data.Repository { - public class UserRepositoryImpl:IUserRepository + public class SQLUserRepositoryImpl:IUserRepository { - public UserRepositoryImpl() { - GetAllUsers = LocalStaticData.users; + private readonly RemoteDatabaseContext _remoteDatabaseContext; + + public SQLUserRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext) { + _remoteDatabaseContext = remoteDatabaseContext; + GetAllUsers = _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList(); } public List GetAllUsers { get; set; } public List GetAllUser() { - return GetAllUsers; + return _remoteDatabaseContext.Users.Select(x => new UserLocalEntity{FIO = x.FIO, Guid = x.Guid, GroupID = x.GroupID}).ToList(); } public bool RemoveUserByGuid(Guid userGuid) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userGuid).FirstOrDefault(); - if (userLocal == null) return false; - - return GetAllUsers.Remove(userLocal); + var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == userGuid); + _remoteDatabaseContext.Users.Remove(userDAO); + _remoteDatabaseContext.SaveChanges(); + return true; } public UserLocalEntity? GetUserByGuid(Guid userGuid) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userGuid).FirstOrDefault(); - if (userLocal == null) return null; - - return userLocal; + var userDAO = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == userGuid); + return new UserLocalEntity{FIO = userDAO.FIO, GroupID = userDAO.GroupID, Guid = userGuid}; } public UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEntity) { - int index = GetAllUsers.FindIndex(x => x.Guid == userUpdateLocalEntity.Guid); - if (index == -1) return null; - GetAllUsers[index].FIO = userUpdateLocalEntity.FIO; - GetAllUsers[index].GroupID = userUpdateLocalEntity.GroupID; - return GetAllUsers[index]; + var user = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == userUpdateLocalEntity.Guid); + if (user == null){ + return null; + } + + user.FIO = userUpdateLocalEntity.FIO; + user.GroupID = userUpdateLocalEntity.GroupID; + _remoteDatabaseContext.SaveChanges(); + return new UserLocalEntity{FIO = user.FIO, Guid = user.Guid, GroupID = user.GroupID}; } } } \ No newline at end of file diff --git a/Demo.csproj b/Demo.csproj index 28a3ec0..6f34ba1 100644 --- a/Demo.csproj +++ b/Demo.csproj @@ -1,18 +1,20 @@ - Exe net8.0 enable enable - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + - \ No newline at end of file diff --git a/Domain/UseCase/GroupUseCase.cs b/Domain/UseCase/GroupUseCase.cs index dedaadc..9a50619 100644 --- a/Domain/UseCase/GroupUseCase.cs +++ b/Domain/UseCase/GroupUseCase.cs @@ -11,9 +11,9 @@ namespace Demo.Domain.UseCase public class GroupUseCase:IGroupUseCase { - private IGroupRepository _repositoryGroupImpl; + private readonly IGroupRepository _repositoryGroupImpl; - public GroupUseCase(IGroupRepository groupRepository) + public GroupUseCase(IGroupRepository groupRepository, IUserRepository repositoryUserImpl) { _repositoryGroupImpl = groupRepository; } @@ -46,19 +46,17 @@ namespace Demo.Domain.UseCase return _repositoryGroupImpl.RemoveGroupById(groupId); } - public bool CreateGroup(Group group) { + public bool CreateGroup(string Name) { - GroupLocalEntity? groupLocalEntity = new GroupLocalEntity{Id = group.Id, Name = group.Name}; - _repositoryGroupImpl.CreateGroup(groupLocalEntity); - if (groupLocalEntity != null) return false; + _repositoryGroupImpl.CreateGroup(Name); return true; } public Group GetGroupById(int groupId) { GroupLocalEntity? groupLocalEntity = _repositoryGroupImpl.GetGroupById(groupId); - if (groupLocalEntity == null) throw new Exception(""); - return new Group { Id = groupLocalEntity.Id, Name = groupLocalEntity.Name }; + if (groupLocalEntity == null) throw new Exception("bello"); + return new Group{Id = groupId, Name = groupLocalEntity.Name}; } } } diff --git a/Domain/UseCase/IGroupUseCase.cs b/Domain/UseCase/IGroupUseCase.cs index bf53584..2f086f1 100644 --- a/Domain/UseCase/IGroupUseCase.cs +++ b/Domain/UseCase/IGroupUseCase.cs @@ -10,9 +10,9 @@ namespace Demo.Domain.UseCase public interface IGroupUseCase { List GetAllGroups(); - bool RemoveGroupById(int groupID); + bool RemoveGroupById(int groupId); Group UpdateGroupName(Group group); Group GetGroupById(int groupID); - bool CreateGroup(Group group); + bool CreateGroup(string Name); } } \ No newline at end of file diff --git a/Domain/UseCase/PresenceUseCase.cs b/Domain/UseCase/PresenceUseCase.cs index 797e39e..b9d2044 100644 --- a/Domain/UseCase/PresenceUseCase.cs +++ b/Domain/UseCase/PresenceUseCase.cs @@ -96,7 +96,7 @@ namespace Demo.Domain.UseCase Date = x.Date }).ToList(); - _repositoryPresenceImpl.SavePresence(presenceLocalEntity); + _repositoryPresenceImpl.GeneratePresence(presenceLocalEntity); return true; } diff --git a/Domain/UseCase/UserUseCase.cs b/Domain/UseCase/UserUseCase.cs index 91591cc..182fae5 100644 --- a/Domain/UseCase/UserUseCase.cs +++ b/Domain/UseCase/UserUseCase.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Demo.Domain.UseCase { - public class UserUseCase:IUserUseCase + public class UserUseCase : IUserUseCase { private readonly IUserRepository _repositoryUserImpl; private readonly IGroupRepository _repositoryGroupImpl; @@ -20,36 +20,61 @@ namespace Demo.Domain.UseCase } public List GetAllGroups() => _repositoryGroupImpl.GetAllGroup() - .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); + .Select(it => new Group{Id = it.Id, Name = it.Name}).ToList(); + public List GetAllUsers() => _repositoryUserImpl.GetAllUser() - .Join(_repositoryGroupImpl.GetAllGroup(), - user => user.GroupID, - group => group.Id, - (user, group) => - new User { FIO = user.FIO, - Guid = user.Guid, - Group = new Group {Id = group.Id, Name = group.Name } } - ).ToList(); + .Join(_repositoryGroupImpl.GetAllGroup(), + user => user.GroupID, + group => group.Id, + (user, group) => new User{ + FIO = user.FIO, + Guid = user.Guid, + Group = new Group{ + Id = group.Id, + Name = group.Name} + } + ).ToList(); public User GetUserByGuid(Guid userGuid) { UserLocalEntity? userLocalEntity = _repositoryUserImpl.GetUserByGuid(userGuid); - if (userLocalEntity == null) throw new Exception(""); - Group? group = GetAllGroups().FirstOrDefault(it => it.Id == userLocalEntity!.GroupID); - if (group == null) throw new Exception(""); - return new User { FIO = userLocalEntity.FIO, Guid = userLocalEntity.Guid, Group = new Group { Id = group.Id, Name = group.Name }}; - + if (userLocalEntity == null) throw new Exception("bello"); + Group? group = GetAllGroups().FirstOrDefault(it => userLocalEntity.GroupID == it.Id); + if (group == null) throw new Exception("bello"); + return new User{ + FIO = userLocalEntity.FIO, + Guid = userLocalEntity.Guid, + Group = new Group{ + Id = group.Id, + Name = group.Name} + }; } public bool RemoveUserByGuid(Guid userGuid) { return _repositoryUserImpl.RemoveUserByGuid(userGuid); } public User UpdateUser(User user) { - UserLocalEntity userLocalEnity = new UserLocalEntity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; - UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); - if (result == null) throw new Exception(""); - Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID); - if (group == null) throw new Exception(""); - return new User { FIO = user.FIO, Guid = user.Guid, Group = group}; + UserLocalEntity userLocalEntity = new UserLocalEntity + { + FIO = user.FIO, + Guid = user.Guid, + GroupID = user.Group.Id + }; + UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEntity); + if (result == null) + { + throw new Exception(""); + } + Group? group = GetAllGroups().FirstOrDefault(it => result.GroupID == it.Id); + if (group == null) + { + throw new Exception(""); + } + return new User + { + FIO = result.FIO, + Guid = result.Guid, + Group = group + }; } } } \ No newline at end of file diff --git a/Migrations/20241024202926_InitialCreate.Designer.cs b/Migrations/20241024202926_InitialCreate.Designer.cs new file mode 100644 index 0000000..36f45d2 --- /dev/null +++ b/Migrations/20241024202926_InitialCreate.Designer.cs @@ -0,0 +1,118 @@ +// +using System; +using Data.RemoteData.RemoteDataBase.DAO; +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(RemoteDatabaseContext))] + [Migration("20241024202926_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("FIO") + .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("User") + .HasForeignKey("GroupID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", b => + { + b.Navigation("User"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20241024202926_InitialCreate.cs b/Migrations/20241024202926_InitialCreate.cs new file mode 100644 index 0000000..0690743 --- /dev/null +++ b/Migrations/20241024202926_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), + FIO = 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/Migrations/RemoteDatabaseContextModelSnapshot.cs b/Migrations/RemoteDatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..9a23270 --- /dev/null +++ b/Migrations/RemoteDatabaseContextModelSnapshot.cs @@ -0,0 +1,115 @@ +// +using System; +using Data.RemoteData.RemoteDataBase.DAO; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Demo.Migrations +{ + [DbContext(typeof(RemoteDatabaseContext))] + partial class RemoteDatabaseContextModelSnapshot : 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("FIO") + .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("User") + .HasForeignKey("GroupID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("Demo.Data.RemoteData.RemoteDataBase.DAO.GroupDao", b => + { + b.Navigation("User"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Program.cs b/Program.cs index e314dce..023de35 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ +using Data.RemoteData.RemoteDataBase.DAO; using Demo.Data.Repository; using Demo.Domain.UseCase; using Demo.UI; @@ -7,17 +8,16 @@ using Microsoft.Extensions.DependencyInjection; IServiceCollection services = new ServiceCollection(); services - .AddSingleton() - .AddSingleton() - .AddSingleton() + .AddDbContext() + .AddSingleton() + .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() - .AddSingleton() .AddSingleton(); var serviceProvider = services.BuildServiceProvider(); -MainMenuUI mainMenuUI = serviceProvider.GetService(); +MainMenuUI? mainMenuUI = serviceProvider.GetService(); diff --git a/UI/GroupConsole.cs b/UI/GroupConsole.cs index 3d4bafd..e0acab9 100644 --- a/UI/GroupConsole.cs +++ b/UI/GroupConsole.cs @@ -28,19 +28,33 @@ namespace Demo.UI } } - public void CreateNewGroup(Group group){ - string output = _groupUseCase.CreateGroup(group) ? "Пользователь создан" : "Пользователь не создан"; + public void CreateNewGroup(string Name){ + string output = _groupUseCase.CreateGroup(Name) ? "Группа создана" : "Группа не создана"; + Console.WriteLine(output); } public void DisplayAllGroups() { - StringBuilder groupOutput = new StringBuilder(); + StringBuilder userOutput = new StringBuilder(); foreach (var group in _groupUseCase.GetAllGroups()) { - groupOutput.AppendLine($"{group.Id}\t {group.Name}\t"); + userOutput.AppendLine($"{group.Id}\t{group.Name}"); } - Console.WriteLine(groupOutput); + Console.WriteLine(userOutput); + } + + public void RemoveGroupById(int ID){ + string output = _groupUseCase.RemoveGroupById(ID) ? "Группа удалена" : "Группа не удалена"; + Console.WriteLine(output); + } + + public void DisplayGroupByID(int ID){ + StringBuilder userOutput = new StringBuilder(); + var group = _groupUseCase.GetGroupById(ID); + { + userOutput.AppendLine($"{group.Id}\t{group.Name}"); + } + Console.WriteLine(userOutput); } } - } diff --git a/UI/MainMenu.cs b/UI/MainMenu.cs index a97f076..cc86ec8 100644 --- a/UI/MainMenu.cs +++ b/UI/MainMenu.cs @@ -28,18 +28,24 @@ namespace Demo.UI { switch (Console.ReadLine()) { + case "1": _userConsoleUI.DisplayAllUsers(); break; - case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; + case "2": _userConsoleUI.GetUserByGuid(Guid.Parse(Console.ReadLine())); break; case "3": _userConsoleUI.UpdateUser(User.Parse(Console.ReadLine())); break; - case "4": _userConsoleUI.GetUserByGuid(Guid.Parse(Console.ReadLine())); break; + case "4": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; + case "5": _groupConsoleUI.DisplayAllGroups(); break; - case "6": _groupConsoleUI.CreateNewGroup(Group.Parse(Console.ReadLine())); break; + case "6": _groupConsoleUI.CreateNewGroup(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; + case "8": _groupConsoleUI.DisplayGroupByID(Convert.ToInt32(Console.ReadLine())); break; + case "9": _groupConsoleUI.RemoveGroupById(Convert.ToInt32(Console.ReadLine())); break; + + + case "10": _presenceConsoleUI.DisplayPresenceByGroup(Convert.ToInt32(Console.ReadLine())); break; + case "11": _presenceConsoleUI.DisplayPresenceByGroupAndDate(Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break; + case "12": _presenceConsoleUI.GeneratePresence(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break; + case "13": _presenceConsoleUI.GeneratePresenceWeek(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()), DateOnly.FromDateTime(Convert.ToDateTime(Console.ReadLine()))); break; + case "14": _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 index cc3fd53..2f34252 100644 --- a/UI/PresenceConsole.cs +++ b/UI/PresenceConsole.cs @@ -20,7 +20,7 @@ namespace Demo.UI Console.WriteLine(stringBuilder); } - public void DisplayPresenceByGroupByTime(int groupID, DateOnly date){ + public void DisplayPresenceByGroupAndDate(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}");