From bc674c9649f39b91a997fa4742af88a219fd99ea Mon Sep 17 00:00:00 2001 From: Dasha06 Date: Wed, 6 Nov 2024 07:10:09 +0300 Subject: [PATCH] changed to sqlrepositories --- Data/RemoteData/RemoteDataBase/DAO/Group.cs | 2 +- .../RemoteData/RemoteDataBase/DAO/Presence.cs | 2 + Data/RemoteData/RemoteDataBase/DAO/User.cs | 6 +- .../RemoteDataBase/RemoteDataBaseContext.cs | 12 +- Data/Repository/GroupRepositoryImpl.cs | 107 ++++++++++-------- Data/Repository/IGroupRepository.cs | 7 +- Data/Repository/IPresenceRepository.cs | 9 +- Data/Repository/IUserRepository.cs | 11 +- Data/Repository/PresenceRepositoryImpl.cs | 53 +++++---- Data/Repository/UserRepositoryImpl.cs | 69 ++++++----- Domain/UseCase/GroupUseCase.cs | 11 +- Domain/UseCase/PresenceUseCase.cs | 67 ++++++----- Domain/UseCase/UserUseCase.cs | 46 ++++---- Program.cs | 16 ++- UI/MainMenu.cs | 10 +- UI/PresenceConsole.cs | 35 +++++- UI/UserConsole.cs | 19 ++-- obj/Debug/net8.0/Posechaemost.AssemblyInfo.cs | 2 +- .../Posechaemost.AssemblyInfoInputs.cache | 2 +- 19 files changed, 281 insertions(+), 205 deletions(-) diff --git a/Data/RemoteData/RemoteDataBase/DAO/Group.cs b/Data/RemoteData/RemoteDataBase/DAO/Group.cs index 2afcb44..5e5abcb 100644 --- a/Data/RemoteData/RemoteDataBase/DAO/Group.cs +++ b/Data/RemoteData/RemoteDataBase/DAO/Group.cs @@ -7,7 +7,7 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase.DAO { public class GroupDao { - public required int Id {get; set;} + public int Id {get; set;} public required string Name {get; set;} public IEnumerable Users {get; set;} diff --git a/Data/RemoteData/RemoteDataBase/DAO/Presence.cs b/Data/RemoteData/RemoteDataBase/DAO/Presence.cs index 5acd3c3..1c7cc7a 100644 --- a/Data/RemoteData/RemoteDataBase/DAO/Presence.cs +++ b/Data/RemoteData/RemoteDataBase/DAO/Presence.cs @@ -10,6 +10,8 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase.DAO public required DateOnly Date {get; set;} public int ClassNumber {get; set;} public bool IsAttendence {get; set;} = true; + public int UserId {get; set;} public required UserDao User {get; set;} + public int GroupId {get; set;} } } \ No newline at end of file diff --git a/Data/RemoteData/RemoteDataBase/DAO/User.cs b/Data/RemoteData/RemoteDataBase/DAO/User.cs index befaf6a..a7d52d6 100644 --- a/Data/RemoteData/RemoteDataBase/DAO/User.cs +++ b/Data/RemoteData/RemoteDataBase/DAO/User.cs @@ -8,7 +8,9 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase.DAO public class UserDao { public required string FIO {get; set; } - public Guid Guid {get; set;} - public required GroupDao Group {get; set;} + + public int UserId { get; set; } + public required 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 index c300a98..7549424 100644 --- a/Data/RemoteData/RemoteDataBase/RemoteDataBaseContext.cs +++ b/Data/RemoteData/RemoteDataBase/RemoteDataBaseContext.cs @@ -10,15 +10,15 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase public class RemoteDataBaseContext: DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ - optionsBuilder.UseNpgsql(); + optionsBuilder.UseNpgsql("Host=localhost; Database=presencedb; Username=postgres; Password=123"); } 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(user => user.UserId); + modelBuilder.Entity().Property(user => user.UserId).ValueGeneratedOnAdd(); modelBuilder.Entity().HasKey(presence => new { presence.User, presence.Date, @@ -28,8 +28,8 @@ namespace Posechaemost.Data.RemoteData.RemoteDataBase } - public DbSet groups{ get; set; } - public DbSet users{ get; set; } - public DbSet presences{ get; set; } + public DbSet Groups{ get; set; } + public DbSet Users{ get; set; } + public DbSet Presences{ get; set; } } } \ No newline at end of file diff --git a/Data/Repository/GroupRepositoryImpl.cs b/Data/Repository/GroupRepositoryImpl.cs index 8b7374c..36c7c2c 100644 --- a/Data/Repository/GroupRepositoryImpl.cs +++ b/Data/Repository/GroupRepositoryImpl.cs @@ -1,77 +1,94 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Text.RegularExpressions; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Posechaemost.Data.LocalData; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; namespace Posechaemost.Data.Repository { - public class GroupRepositoryImpl: IGroupRepository + public class SQLGroupRepositoryImpl : IGroupRepository { + private readonly RemoteDataBaseContext _remoteDatabaseContext; - public bool AddGroup(String name, String id) + public SQLGroupRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext) { - GroupLocalEntity? groupLocal = GetAllGroups().FirstOrDefault(); - // GroupLocalEntity? group = new GroupLocalEntity(); - groupLocal.Name = name; - groupLocal.Id = int.Parse(id); - return true; - } - - public List GetAllGroup() - { - return GetAllGroups(); + _remoteDatabaseContext = remoteDatabaseContext; } public List GetAllGroups() => LocalStaticData.groups; - public GroupLocalEntity GetGroupById(int groupID) + public bool AddGroup(GroupDao group) { - GroupLocalEntity groupLocal = GetAllGroups() - .Where(x => x.Id == groupID).FirstOrDefault(); + var groupDao = new GroupDao + { + Name = group.Name + }; + _remoteDatabaseContext.Groups.Add(groupDao); + _remoteDatabaseContext.SaveChanges(); + return true; + } + + public List GetAllGroup() + { + return _remoteDatabaseContext.Groups + .Include(g => g.Users) + .Select(g => new GroupDao + { + Name = g.Name, + Id = g.Id, + Users = g.Users.Select(u => new UserDao + { + UserId = u.UserId, + FIO = u.FIO, + GroupId = u.GroupId, + }).ToList() + }).ToList(); + } + + public GroupDao GetGroupById(int groupID) + { + var groupLocal = _remoteDatabaseContext.Groups + .Where(g => g.Id == groupID).FirstOrDefault(); if (groupLocal == null) return null; return groupLocal; } - public bool RemoveGroupById(int groupID){ - GroupLocalEntity? groupLocal = GetAllGroups() + public bool RemoveGroupById(int groupID) + { + var groupLocal = _remoteDatabaseContext.Groups .Where(x => x.Id == groupID).FirstOrDefault(); if (groupLocal == null) return false; - return GetAllGroups().Remove(groupLocal); + _remoteDatabaseContext.Groups.Remove(groupLocal); + _remoteDatabaseContext.SaveChanges(); + return true; } - public bool UpdateGroupById(int groupID, String name) + public bool UpdateGroupById(int groupID, string name) { - GroupLocalEntity? groupLocal = GetAllGroups() - .Where(x => x.Id == groupID).FirstOrDefault(); + var groupLocal = _remoteDatabaseContext.Groups + .Include(g => g.Users) + .Where(x => x.Id == groupID).FirstOrDefault(); if (groupLocal == null) return false; + groupLocal.Name = name; + + groupLocal.Users = _remoteDatabaseContext.Users + .Where(x => x.GroupId == groupLocal.Id) + .Select(user => new UserDao + { + UserId = user.UserId, + FIO = user.FIO, + GroupId = user.GroupId + }).ToList(); + + + _remoteDatabaseContext.SaveChanges(); return true; } } -} - // { - // public List GetAllGroups() => LocalStaticData.groups; - // public GroupLocalEntity? UpdateGroup(String name) - // { - // GroupLocalEntity? groupLocal = GetAllGroups() - // .Where(x => x.Name == name).FirstOrDefault(); - // if (groupLocal == null) return null; - // groupLocal.Name = name; - // return groupLocal; - // } - - // public GroupLocalEntity? AddGroup(String name, String id) - // { - // GroupLocalEntity? groupLocal = GetAllGroups().FirstOrDefault(); - // // GroupLocalEntity? group = new GroupLocalEntity(name, id); - // groupLocal.Name = name; - // groupLocal.Id = int.Parse(id); - // return groupLocal; - // } - // } \ No newline at end of file +} \ No newline at end of file diff --git a/Data/Repository/IGroupRepository.cs b/Data/Repository/IGroupRepository.cs index 650f126..635f5e7 100644 --- a/Data/Repository/IGroupRepository.cs +++ b/Data/Repository/IGroupRepository.cs @@ -4,15 +4,16 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; namespace Posechaemost.Data.Repository { public interface IGroupRepository { - List GetAllGroup(); + List GetAllGroup(); bool RemoveGroupById(int groupID); bool UpdateGroupById(int groupID, String name); - GroupLocalEntity GetGroupById(int groupID); - bool AddGroup(String name, String id); + GroupDao GetGroupById(int groupID); + bool AddGroup(GroupDao group); List GetAllGroups(); } } \ No newline at end of file diff --git a/Data/Repository/IPresenceRepository.cs b/Data/Repository/IPresenceRepository.cs index 05791d6..19c53ac 100644 --- a/Data/Repository/IPresenceRepository.cs +++ b/Data/Repository/IPresenceRepository.cs @@ -3,16 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; using Posechaemost.Domain.Models; namespace Posechaemost.Data.Repository { public interface IPresenceRepository { - List GetPresenceByGroup(int groupId); - List GetPresenceByGroupAndDate(int groupId, DateOnly date); - bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid); - void AddPresence(PresenceLocalEntity presence); + List GetPresenceByGroup(int groupId); + List GetPresenceByGroupAndDate(int groupId, DateOnly date); + bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId); + bool AddPresence(PresenceDao presence); } } \ No newline at end of file diff --git a/Data/Repository/IUserRepository.cs b/Data/Repository/IUserRepository.cs index 434d230..d30c703 100644 --- a/Data/Repository/IUserRepository.cs +++ b/Data/Repository/IUserRepository.cs @@ -4,14 +4,15 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; namespace Posechaemost.Data.Repository { public interface IUserRepository { - List GetAllUser(); - bool RemoveUserByGuid(Guid userGuid); - UserLocalEntity? GetUserByGuid(Guid userGuid); - UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEnity); - UserLocalEntity? UpdateUserByGuid(Guid userGuid); + List GetAllUser(); + bool RemoveUserById(int usesId); + UserDao GetUserById(int userId); + bool UpdateUser(UserDao userUpdate); + bool UpdateUserById(int userId, string fio, int groupId); } } \ No newline at end of file diff --git a/Data/Repository/PresenceRepositoryImpl.cs b/Data/Repository/PresenceRepositoryImpl.cs index 7707a6a..789c7b2 100644 --- a/Data/Repository/PresenceRepositoryImpl.cs +++ b/Data/Repository/PresenceRepositoryImpl.cs @@ -2,49 +2,62 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Posechaemost.Data.LocalData; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; using Posechaemost.Domain.Models; namespace Posechaemost.Data.Repository { - public class PresenceRepositoryImpl: IPresenceRepository + public class SQLPresenceRepositoryImpl: IPresenceRepository { - public PresenceRepositoryImpl() { - GetAllPresences = LocalStaticData.presences; - } - public List GetAllPresences - { get; set; } + private readonly RemoteDataBaseContext _remoteDatabaseContext; - public void AddPresence(PresenceLocalEntity presence) + public SQLPresenceRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext) { - PresenceLocalEntity? presenceLocal = GetAllPresences.FirstOrDefault(); - // GroupLocalEntity? group = new GroupLocalEntity(); - presenceLocal.ClassNumber = presence.ClassNumber; - presence.UserGuid = presence.UserGuid; - presenceLocal.Date = presence.Date; - presenceLocal.IsAttendence = presence.IsAttendence; + _remoteDatabaseContext = remoteDatabaseContext; } - public List GetPresenceByGroup(int groupId) + public bool AddPresence(PresenceDao presence) { - return GetAllPresences; + var presenceDao = new PresenceDao + { + Date = presence.Date, + ClassNumber = presence.ClassNumber, + UserId = presence.UserId, + User = presence.User + }; + _remoteDatabaseContext.Presences.Add(presenceDao); + _remoteDatabaseContext.SaveChanges(); + return true; } - public List GetPresenceByGroupAndDate(int groupId, DateOnly date) + public List GetPresenceByGroup(int groupId) { - return GetAllPresences; + var listPresences = _remoteDatabaseContext.Presences + .Where(x => x.GroupId == groupId).ToList(); + return listPresences; } - public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid) + public List GetPresenceByGroupAndDate(int groupId, DateOnly date) { - var presToUpdate = GetAllPresences - .Where(x => x.UserGuid == userGuid && x.ClassNumber >= firstClass + var listPresences = _remoteDatabaseContext.Presences + .Where(x => x.GroupId == groupId && x.Date == date).ToList(); + return listPresences; + } + + public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId) + { + var presToUpdate = _remoteDatabaseContext.Presences + .Where(x => x.UserId == userId && x.ClassNumber >= firstClass && x.ClassNumber <= lastClass && x.Date == date).ToList(); foreach (var pres in presToUpdate) { pres.IsAttendence = false; } + _remoteDatabaseContext.SaveChanges(); return true; } } diff --git a/Data/Repository/UserRepositoryImpl.cs b/Data/Repository/UserRepositoryImpl.cs index 45c8294..3c326ba 100644 --- a/Data/Repository/UserRepositoryImpl.cs +++ b/Data/Repository/UserRepositoryImpl.cs @@ -3,58 +3,73 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Infrastructure; using Posechaemost.Data.LocalData; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; namespace Posechaemost.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; } - public List GetAllUser() + public List GetAllUser() { - return GetAllUsers; + return _remoteDatabaseContext.Users.Select(u => new UserDao + { + FIO = u.FIO, + UserId = u.UserId, + GroupId = u.GroupId + }).ToList(); } - public List GetAllUsers - { get; set; } - public bool RemoveUserByGuid(Guid userGuid) + public bool RemoveUserById(int userId) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userGuid).FirstOrDefault(); + var userLocal = _remoteDatabaseContext.Users + .Where(x => x.UserId== userId).FirstOrDefault(); if (userLocal == null) return false; - return GetAllUsers.Remove(userLocal); + _remoteDatabaseContext.Users.Remove(userLocal); + _remoteDatabaseContext.SaveChanges(); + return true; } - public UserLocalEntity? GetUserByGuid(Guid userGuid) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userGuid).FirstOrDefault(); + public UserDao GetUserById (int userId) { + var userLocal = _remoteDatabaseContext.Users + .Where(x => x.UserId == userId).FirstOrDefault(); if (userLocal == null) return null; return userLocal; } - public UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEnity) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userUpdateLocalEnity.Guid).FirstOrDefault(); - if (userLocal == null) return null; - userLocal.FIO = userUpdateLocalEnity.FIO; - userLocal.GroupID = userUpdateLocalEnity.GroupID; - return userLocal; + public bool UpdateUser(UserDao userUpdate) { + var userLocal = _remoteDatabaseContext.Users + .Where(x => x.UserId == userUpdate.UserId).FirstOrDefault(); + if (userLocal == null) return false; + userLocal.FIO = userUpdate.FIO; + userLocal.GroupId = userUpdate.GroupId; + _remoteDatabaseContext.SaveChanges(); + return true; } - public UserLocalEntity? UpdateUserByGuid(Guid userGuid) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userGuid).FirstOrDefault(); - if (userLocal == null) return null; - return userLocal; + public bool UpdateUserById(int userId, string fio, int groupId) { + var userLocal = _remoteDatabaseContext.Users + .Where(x => x.UserId == userId).FirstOrDefault(); + if (userLocal == null) return false; + + userLocal.FIO = fio; + userLocal.GroupId = groupId; + _remoteDatabaseContext.SaveChanges(); + + return true; } } } \ No newline at end of file diff --git a/Domain/UseCase/GroupUseCase.cs b/Domain/UseCase/GroupUseCase.cs index b50305b..76fda33 100644 --- a/Domain/UseCase/GroupUseCase.cs +++ b/Domain/UseCase/GroupUseCase.cs @@ -1,4 +1,5 @@ using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; using Posechaemost.Data.Repository; using Posechaemost.Domain.Models; using System; @@ -11,22 +12,22 @@ namespace Posechaemost.Domain.UseCase { public class GroupUseCase { - private GroupRepositoryImpl _repositoryGroupImpl; + private SQLGroupRepositoryImpl _repositoryGroupImpl; - public GroupUseCase(GroupRepositoryImpl repositoryGroupImpl) + public GroupUseCase(SQLGroupRepositoryImpl repositoryGroupImpl) { _repositoryGroupImpl = repositoryGroupImpl; } - public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() - .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); + public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() + .Select(it => new GroupDao { Id = it.Id, Name = it.Name}).ToList(); public bool UpdateGroupName(String id, String name1) { return _repositoryGroupImpl.UpdateGroupById(int.Parse(id), name1); } public bool AddGroup(String name, String id) { - return _repositoryGroupImpl.AddGroup(name, id); + return _repositoryGroupImpl.AddGroup(new GroupDao { Name = name, Id = int.Parse(id) }); } } } \ No newline at end of file diff --git a/Domain/UseCase/PresenceUseCase.cs b/Domain/UseCase/PresenceUseCase.cs index 1c95261..555f47c 100644 --- a/Domain/UseCase/PresenceUseCase.cs +++ b/Domain/UseCase/PresenceUseCase.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; using Posechaemost.Data.Repository; using Posechaemost.Domain.Models; @@ -12,29 +13,28 @@ namespace Posechaemost.Domain.UseCase { public class PresenceUseCase { - private readonly PresenceRepositoryImpl _repositoryPresenceImpl; - private readonly UserRepositoryImpl _repositoryUserImpl; - private readonly GroupRepositoryImpl _repositoryGroupImpl; + private readonly SQLPresenceRepositoryImpl _repositoryPresenceImpl; + private readonly SQLUserRepositoryImpl _repositoryUserImpl; + private readonly SQLGroupRepositoryImpl _repositoryGroupImpl; - public PresenceUseCase(PresenceRepositoryImpl repositoryImpl, UserRepositoryImpl userRepositoryImpl, GroupRepositoryImpl groupRepositoryImpl) { + public PresenceUseCase(SQLPresenceRepositoryImpl repositoryImpl, + SQLUserRepositoryImpl userRepositoryImpl, + SQLGroupRepositoryImpl groupRepositoryImpl) { _repositoryPresenceImpl = repositoryImpl; _repositoryUserImpl = userRepositoryImpl; _repositoryGroupImpl = groupRepositoryImpl; } - public List GetPresenceByGroup(int groupId) { - var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList(); + public List GetPresenceByGroup(int groupId) { + var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupId == groupId).ToList(); var presenceByGroup = _repositoryPresenceImpl.GetPresenceByGroup(groupId) - .Where(x => users.Any(user => user.Guid == x.UserGuid)) - .Select(presence => new Presence{ - User = new User{ - Guid = presence.UserGuid, - GroupId = new Group{ - Id = groupId, - Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name - }, - FIO = users.First(user => user.Guid == presence.UserGuid).FIO, + .Where(x => users.Any(user => user.UserId == x.UserId)) + .Select(presence => new PresenceDao{ + User = new UserDao{ + UserId = presence.UserId, + GroupId = groupId, + FIO = users.First(user => user.UserId == presence.UserId).FIO, }, ClassNumber = presence.ClassNumber, Date = presence.Date, @@ -43,19 +43,16 @@ namespace Posechaemost.Domain.UseCase return presenceByGroup; } - public List GetPresenceByGroupAndDate(int groupId, DateOnly date) { - var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID == groupId).ToList(); + public List GetPresenceByGroupAndDate(int groupId, DateOnly date) { + var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupId == groupId).ToList(); var presenceByGroupAndDate = _repositoryPresenceImpl.GetPresenceByGroupAndDate(groupId, date) - .Where(x => users.Any(user => user.Guid == x.UserGuid && x.Date == date)) - .Select(presence => new Presence{ - User = new User{ - Guid = presence.UserGuid, - GroupId = new Group{ - Id = groupId, - Name = _repositoryGroupImpl.GetAllGroup().First(group => group.Id == groupId).Name - }, - FIO = users.First(user => user.Guid == presence.UserGuid).FIO, + .Where(x => users.Any(user => user.UserId == x.UserId && x.Date == date)) + .Select(presence => new PresenceDao{ + User = new UserDao{ + UserId = presence.UserId, + GroupId = groupId, + FIO = users.First(user => user.UserId == presence.UserId).FIO, }, ClassNumber = presence.ClassNumber, Date = presence.Date, @@ -64,20 +61,22 @@ namespace Posechaemost.Domain.UseCase return presenceByGroupAndDate; } - public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid) { - return _repositoryPresenceImpl.UncheckAttendence(firstClass, lastClass, date, userGuid); + public bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId) { + return _repositoryPresenceImpl.UncheckAttendence(firstClass, lastClass, date, userId); } public void AddPresence(int firstClass, int lastClass, int groupId,DateOnly date) { - var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupID==groupId).ToList(); - List presenceList = new List(); - for (int i = firstClass; i < lastClass; i++) { - foreach (var user in users) { - PresenceLocalEntity pres = new PresenceLocalEntity {ClassNumber = i, Date = date, UserGuid = user.Guid}; + var users = _repositoryUserImpl.GetAllUser().Where(x => x.GroupId==groupId).ToList(); + List presenceList = new List(); + for (int i = firstClass; i < lastClass; i++) + { + foreach (var user in users) + { + PresenceDao pres = new PresenceDao{ClassNumber = i, Date = date, UserId = user.UserId, User = user}; presenceList.Add(pres); } } } } -} \ No newline at end of file +} diff --git a/Domain/UseCase/UserUseCase.cs b/Domain/UseCase/UserUseCase.cs index f3fc2fd..5285986 100644 --- a/Domain/UseCase/UserUseCase.cs +++ b/Domain/UseCase/UserUseCase.cs @@ -1,4 +1,5 @@ using Posechaemost.Data.LocalData.Entity; +using Posechaemost.Data.RemoteData.RemoteDataBase.DAO; using Posechaemost.Data.Repository; using Posechaemost.Domain.Models; using System; @@ -12,50 +13,43 @@ namespace Posechaemost.Domain.UseCase public class UserUseCase { - private readonly UserRepositoryImpl _repositoryUserImpl; + private readonly SQLUserRepositoryImpl _repositoryUserImpl; private readonly IGroupRepository _repositoryGroupImpl; - public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) + public UserUseCase(SQLUserRepositoryImpl repositoryImpl, SQLGroupRepositoryImpl repositoryGroupImpl) { _repositoryUserImpl = repositoryImpl; _repositoryGroupImpl = repositoryGroupImpl; } - private List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() - .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); - public List GetAllUsers() => _repositoryUserImpl.GetAllUsers + private List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() + .Select(it => new GroupDao { Id = it.Id, Name = it.Name}).ToList(); + public List GetAllUsers() => _repositoryUserImpl.GetAllUser() .Join(_repositoryGroupImpl.GetAllGroups(), - user => user.GroupID, + user => user.GroupId, group => group.Id, (user, group) => - new User { FIO = user.FIO, - Guid = user.Guid, - GroupId = new Group {Id = group.Id, Name = group.Name } } + new UserDao { FIO = user.FIO, + UserId = user.UserId, + GroupId = group.Id } ).ToList(); - public bool RemoveUserByGuid(Guid userGuid) { - return _repositoryUserImpl.RemoveUserByGuid(userGuid); + public bool RemoveUserById(int userId) { + return _repositoryUserImpl.RemoveUserById(userId); } - public User UpdateUser(User user) { - UserLocalEntity userLocalEnity = new UserLocalEntity { FIO = user.FIO, GroupID = user.GroupId.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, GroupId = group}; + public bool UpdateUser(UserDao user) { + UserDao userDao = new UserDao { FIO = user.FIO, GroupId = user.GroupId }; + return _repositoryUserImpl.UpdateUser(userDao); } - public UserLocalEntity GetUserByGuid(Guid userGuid) { - return _repositoryUserImpl.GetUserByGuid(userGuid); + public UserDao GetUserById(int userId) { + return _repositoryUserImpl.GetUserById(userId); } - public User UpdateUserByGuid(Guid userGuid, String fio, String groupId) { - UserLocalEntity? result = _repositoryUserImpl.UpdateUserByGuid(userGuid); - if (result == null) throw new Exception(""); - Group? group = GetAllGroups().FirstOrDefault(it => it.Id == int.Parse(groupId)); - if (group == null) throw new Exception(""); - return new User { FIO = fio, GroupId = group, Guid = userGuid }; + public bool UpdateUserById(int userId, String fio, int groupId) { + UserDao userDao = new UserDao { FIO = fio, GroupId = groupId }; + return _repositoryUserImpl.UpdateUser(userDao); } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 29074e5..00e5e73 100644 --- a/Program.cs +++ b/Program.cs @@ -7,13 +7,17 @@ using System.Text.RegularExpressions; IServiceCollection services = new ServiceCollection(); services - .AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); var serviceProvider = services.BuildServiceProvider(); -GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); -UserUseCase userUseCase = serviceProvider.GetService(); -GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl); +var userUseCase = serviceProvider.GetService(); +var groupUseCase = serviceProvider.GetService(); +var presenceUseCase = serviceProvider.GetService(); -MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase); \ No newline at end of file +MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase, presenceUseCase); diff --git a/UI/MainMenu.cs b/UI/MainMenu.cs index ba9a268..238ab4c 100644 --- a/UI/MainMenu.cs +++ b/UI/MainMenu.cs @@ -32,12 +32,16 @@ namespace Posechaemost.UI switch (Console.ReadLine()) { case "1": _userConsoleUI.DisplayAllUsers(); break; - case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; + case "2": _userConsoleUI.RemoveUserById(int.Parse(Console.ReadLine())); break; case "3": _groupConsoleUI.DisplayAllGroups(); break; - case "4": _userConsoleUI.GetUserByGuid(Guid.Parse(Console.ReadLine())); break; - case "5": _userConsoleUI.UpdateUserByGuid(Guid.Parse(Console.ReadLine()), Console.ReadLine(), Console.ReadLine()); break; + case "4": _userConsoleUI.GetUserById(int.Parse(Console.ReadLine())); break; + case "5": _userConsoleUI.UpdateUserById(int.Parse(Console.ReadLine()), Console.ReadLine(), int.Parse(Console.ReadLine())); break; case "6": _groupConsoleUI.UpdateGroupName(Console.ReadLine(), Console.ReadLine()); break; case "7": _groupConsoleUI.AddGroup(Console.ReadLine(), Console.ReadLine()); break; + case "8": _presenceConsoleUI.GetPresenceByGroup(int.Parse(Console.ReadLine())); break; + case "9": _presenceConsoleUI.GetPresenceByGroupAndDate(int.Parse(Console.ReadLine()), DateOnly.Parse(Console.ReadLine())); break; + case "10": _presenceConsoleUI.UncheckAttendence(int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), DateOnly.Parse(Console.ReadLine()), int.Parse(Console.ReadLine())); break; + case "11": _presenceConsoleUI.AddPresence(int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), DateOnly.Parse(Console.ReadLine())); break; default: DisplayMenu(); break; diff --git a/UI/PresenceConsole.cs b/UI/PresenceConsole.cs index f9dec1a..90d2d95 100644 --- a/UI/PresenceConsole.cs +++ b/UI/PresenceConsole.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; using Posechaemost.Domain.UseCase; @@ -9,24 +10,46 @@ namespace Posechaemost.UI public class PresenceConsoleUI { PresenceUseCase _presenceUseCase; - public PresenceConsoleUI(PresenceUseCase presenceUseCase) { + public PresenceConsoleUI(PresenceUseCase presenceUseCase) + { _presenceUseCase = presenceUseCase; } - public void GetPresenceByGroup(int groupId) { + public void GetPresenceByGroup(int groupId) + { + StringBuilder presenceOutput = new StringBuilder(); var presence = _presenceUseCase.GetPresenceByGroup(groupId); + foreach (var p in presence) + { + presenceOutput.AppendLine($"{p.User.UserId}\t{p.User.FIO}\t{p.ClassNumber}\t{p.Date}\t{p.IsAttendence}"); + } + Console.WriteLine(presenceOutput); } - public void GetPresenceByGroupAndDate(int groupId, DateOnly date) { + public void GetPresenceByGroupAndDate(int groupId, DateOnly date) + { + StringBuilder presenceOutput = new StringBuilder(); var presence = _presenceUseCase.GetPresenceByGroupAndDate(groupId, date); + foreach (var p in presence) + { + presenceOutput.AppendLine($"{p.User.UserId}\t{p.User.FIO}\t{p.ClassNumber}\t{p.Date}\t{p.IsAttendence}"); + } + Console.WriteLine(presenceOutput); } - public void UncheckAttendence(int firstClass, int lastClass, DateOnly date, Guid userGuid) { - var presence = _presenceUseCase.UncheckAttendence(firstClass, lastClass, date, userGuid); + public void UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId) + { + string output = _presenceUseCase.UncheckAttendence(firstClass, lastClass, date, userId) ? + "Посещаемость обновлена" : "Посещаемость не обновлена"; + Console.WriteLine(output); } - public void AddPresence(int firstClass, int lastClass, int groupId, DateOnly date) { + public void AddPresence(int firstClass, int lastClass, int groupId, DateOnly date) + { + StringBuilder presenceOutput = new StringBuilder(); _presenceUseCase.AddPresence(firstClass, lastClass, groupId, date); + presenceOutput.AppendLine("Посещаемость добавлена"); + Console.WriteLine(presenceOutput); } } } \ No newline at end of file diff --git a/UI/UserConsole.cs b/UI/UserConsole.cs index aedc9b0..7500ead 100644 --- a/UI/UserConsole.cs +++ b/UI/UserConsole.cs @@ -15,9 +15,9 @@ namespace Posechaemost.UI _userUseCase = userUseCase; } - public void RemoveUserByGuid(Guid guidUser) { + public void RemoveUserById(int userId) { - string output = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален"; + string output = _userUseCase.RemoveUserById(userId) ? "Пользователь удален" : "Пользователь не удален"; Console.WriteLine(output); } @@ -26,22 +26,21 @@ namespace Posechaemost.UI StringBuilder userOutput = new StringBuilder(); foreach (var user in _userUseCase.GetAllUsers()) { - userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.GroupId.Name}"); + userOutput.AppendLine($"{user.UserId}\t{user.FIO}\t{user.GroupId}"); } Console.WriteLine(userOutput); } - public void GetUserByGuid(Guid userGuid) { + public void GetUserById(int userId) { StringBuilder userOutput = new StringBuilder(); - var user = _userUseCase.GetUserByGuid(userGuid); - userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.GroupID}"); + var user = _userUseCase.GetUserById(userId); + userOutput.AppendLine($"{user.UserId}\t{user.FIO}\t{user.GroupId}"); Console.WriteLine(userOutput); - } + } - public void UpdateUserByGuid(Guid userGuid, String name, String groupId) { + public void UpdateUserById(int userId, String name, int groupId) { StringBuilder userOutput = new StringBuilder(); - var user = _userUseCase.UpdateUserByGuid(userGuid, name, groupId); - userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.GroupId.Name}"); + var user = _userUseCase.UpdateUserById(userId, name, groupId) ? "Пользователь обновлен" : "Пользователь не обновлен"; Console.WriteLine(userOutput); } } diff --git a/obj/Debug/net8.0/Posechaemost.AssemblyInfo.cs b/obj/Debug/net8.0/Posechaemost.AssemblyInfo.cs index 2f70f3c..e89b971 100644 --- a/obj/Debug/net8.0/Posechaemost.AssemblyInfo.cs +++ b/obj/Debug/net8.0/Posechaemost.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("Posechaemost")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+dd18de386525d2859a6db90bb74a2bdc95bc47e3")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0705829116f2c729cee9977f2de521bf54b6e4b1")] [assembly: System.Reflection.AssemblyProductAttribute("Posechaemost")] [assembly: System.Reflection.AssemblyTitleAttribute("Posechaemost")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net8.0/Posechaemost.AssemblyInfoInputs.cache b/obj/Debug/net8.0/Posechaemost.AssemblyInfoInputs.cache index ee33663..f8c3c80 100644 --- a/obj/Debug/net8.0/Posechaemost.AssemblyInfoInputs.cache +++ b/obj/Debug/net8.0/Posechaemost.AssemblyInfoInputs.cache @@ -1 +1 @@ -e484c6d2be3801b673b3b8bec4389b40f150f0a04dc9544c29a279722a7318c6 +04d7b3aacea28cffe6e56680c2c82b72f1a837729ee485f47bdc07c464612b47