This commit is contained in:
Userok 2024-10-25 12:20:05 +03:00
parent 1b2b3fc51b
commit ac0c8401da
8 changed files with 117 additions and 62 deletions

View File

@ -6,17 +6,16 @@ namespace Zurnal.Data.Repository
{ {
public class UserRepositoryImpl : IGroupRepository public class UserRepositoryImpl : IGroupRepository
{ {
public UserRepositoryImpl() { public UserRepositoryImpl() => GetAllUsers = LocalStaticData.users;
GetAllUsers = LocalStaticData.users;
}
public List<UserLocalEnity> GetAllUsers public List<UserLocalEnity> GetAllUsers
{ get; set; } { get; set; }
public bool RemoveUserByGuid(Guid userGuid) public bool RemoveUserByGuid(Guid userGuid)
{ {
UserLocalEnity? userLocal = GetAllUsers UserLocalEnity? userLocal = GetAllUsers
.Where(x => x.Guid == userGuid).FirstOrDefault(); .Where(x => x.Guid == userGuid)
.FirstOrDefault();
if (userLocal == null) return false; if (userLocal == null) return false;
return GetAllUsers.Remove(userLocal); return GetAllUsers.Remove(userLocal);
@ -40,10 +39,10 @@ namespace Zurnal.Data.Repository
} }
public List<UserLocalEnity> GetAllUsersList() public List<UserLocalEnity> GetAllUsersList()
{ {
return GetAllUsers; return GetAllUsersList;
} }
List<GroupLocalEntity> IGroupRepository.GetAllGroup() List<GroupLocalEntity> IGroupRepository.GetAllGroups()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -67,6 +66,41 @@ namespace Zurnal.Data.Repository
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
internal object GetAllUsers()
{
throw new NotImplementedException();
}
public void AddGroup(System.Text.RegularExpressions.Group group)
{
throw new NotImplementedException();
}
public System.Text.RegularExpressions.Group GetGroupById(int id)
{
throw new NotImplementedException();
}
public IEnumerable<System.Text.RegularExpressions.Group> GetAllGroups()
{
throw new NotImplementedException();
}
public void UpdateGroup(System.Text.RegularExpressions.Group group)
{
throw new NotImplementedException();
}
public void DeleteGroup(int id)
{
throw new NotImplementedException();
}
public object GetAllGroup()
{
throw new NotImplementedException();
}
} }
} }

View File

@ -1,45 +1,63 @@
using Zurnal.Data.Repository; using Zurnal.Data.Repository;
using Zurnal.domain.Models; using Zurnal.domain.Models;
using Zurnal.Date.Repository;
namespace Zurnal.Domain.UseCase namespace Zurnal.Domain.UseCase
{ {
public class UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) public class UserUseCase
{ {
private UserRepositoryImpl _repositoryUserImpl = repositoryImpl; private readonly UserRepositoryImpl _repositoryUserImpl;
internal IGroupRepository RepositoryGroupImpl { get; }
internal IGroupRepository RepositoryGroupImpl { get; } = (IGroupRepository?)repositoryGroupImpl; public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
{
_repositoryUserImpl = repositoryImpl;
RepositoryGroupImpl = repositoryGroupImpl;
}
public List<Group> GetAllGroups() => RepositoryGroupImpl.GetAllGroup() public List<Group> GetAllGroups()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); {
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers return RepositoryGroupImpl.GetAllGroup()
.Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
}
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers()
.Join(RepositoryGroupImpl.GetAllGroup(), .Join(RepositoryGroupImpl.GetAllGroup(),
user => user.GroupID, user => user.GroupID,
group => group.Id, group => group.Id,
(user, group) => (user, group) =>
new User { FIO = user.FIO, new User { FIO = user.FIO,
Guid = user.Guid, Guid = user.Guid,
Group = new Group {Id = group.Id, Name = group.Name } } Group = new Group { Id = group.Id, Name = group.Name } }
).ToList(); ).ToList();
public bool RemoveUserByGuid(Guid userGuid) { public bool RemoveUserByGuid(Guid userGuid)
return _repositoryUserImpl.RemoveUserByGuid(userGuid); {
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
} }
public User UpdateUser(User user) {
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; public User UpdateUser(User user)
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); {
if (result == null) throw new Exception(""); UserLocalEnity userLocalEntity = new UserLocalEntity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID); UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEntity);
if (group == null) throw new Exception("Группа не найдена"); if (result == null) throw new Exception("User update failed.");
return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; Group? group = GetAllGroups().FirstOrDefault(it => it.Id = result.GroupID);
if (group == null) throw new Exception("Group not found.");
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
public User FindUserByGuid(Guid userGuid)
{
var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid);
if (user == null) throw new Exception("User not found.");
var group = RepositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == user.GroupID);
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
}
internal class UserLocalEntity
{
public string FIO { get; set; }
public Guid GroupID { get; set; }
public Guid Guid { get; set; }
} }
}
public User FindUserByGuid(Guid userGuid)
{
var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid);
if (user == null) throw new Exception("Нет такого пользователя");
var group = RepositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == user.GroupID);
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
}
}

View File

@ -11,22 +11,7 @@ public interface IGroupRepository
void UpdateGroup(Group group); void UpdateGroup(Group group);
void DeleteGroup(int id); void DeleteGroup(int id);
object GetAllGroup(); object GetAllGroup();
bool RemoveGroupById(int groupID);
bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup);
} }
public interface IUserRepository
{
void AddUser(User user);
User GetUserById(int id);
IEnumerable<User> GetAllUsers();
void UpdateUser(User user);
void DeleteUser(int id);
}
public interface IPresenceRepository
{
void AddPresence(Presence presence);
Presence GetPresenceById(int id);
IEnumerable<Presence> GetAllPresences();
void UpdatePresence(Presence presence);
void DeletePresence(int id);
}

View File

@ -0,0 +1,9 @@
using Zurnal.domain.Models;
public interface IPresenceRepository
{
void AddPresence(Presence presence);
Presence GetPresenceById(int id);
IEnumerable<Presence> GetAllPresences();
void UpdatePresence(Presence presence);
void DeletePresence(int id);
}

View File

@ -0,0 +1,10 @@
using Zurnal.domain.Models;
public interface IUserRepository
{
void AddUser(User user);
User GetUserById(int id);
IEnumerable<User> GetAllUsers();
void UpdateUser(User user);
void DeleteUser(int id);
}

View File

@ -11,7 +11,7 @@ public class RemoteDateBaseContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Username=postgres;Password=123;Database=postgres"); optionsBuilder.UseNpgsql("Host=45.67.56.214;Port=5432;Username=postgres;Password= ;Database=user1");
} }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
@ -30,4 +30,4 @@ public class RemoteDateBaseContext : DbContext
presence.LessonNumber presence.LessonNumber
}); });
} }
} }

View File

@ -1,10 +1,9 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// Этот код создан программой. // This code was generated by a tool.
// Исполняемая версия:4.0.30319.42000
// //
// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае // Changes to this file may cause incorrect behavior and will be lost if
// повторной генерации кода. // the code is regenerated.
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -14,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Zurnal")] [assembly: System.Reflection.AssemblyCompanyAttribute("Zurnal")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+3c2a052a46be0d31bde662e889b1125d89b34ddf")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+1b2b3fc51b8e6c70458156ed95a4cc212f9e51a9")]
[assembly: System.Reflection.AssemblyProductAttribute("Zurnal")] [assembly: System.Reflection.AssemblyProductAttribute("Zurnal")]
[assembly: System.Reflection.AssemblyTitleAttribute("Zurnal")] [assembly: System.Reflection.AssemblyTitleAttribute("Zurnal")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Создано классом WriteCodeFragment MSBuild. // Generated by the MSBuild WriteCodeFragment class.

View File

@ -1 +1 @@
c93eabaca987d32605ba5a086a447483e47422719e2959ace2200ffe485e77e0 e6b6f9e54fa4ba82e681a5a3ba9349d7912311711fddca257c129394bd4b3c36