2024-10-23 08:14:10 +00:00
|
|
|
|
using Zurnal.Data.Repository;
|
2024-11-01 06:57:09 +00:00
|
|
|
|
using Zurnal.Date.Repository;
|
2024-11-01 07:49:39 +00:00
|
|
|
|
using Zurnal.RemaDateBase.DateDao;
|
2024-10-21 11:56:16 +00:00
|
|
|
|
|
2024-10-23 08:14:10 +00:00
|
|
|
|
namespace Zurnal.Domain.UseCase
|
2024-10-21 11:56:16 +00:00
|
|
|
|
{
|
2024-10-31 11:37:45 +00:00
|
|
|
|
public class UserUseCase : IGroupRepository
|
2024-10-21 11:56:16 +00:00
|
|
|
|
{
|
2024-10-25 09:20:05 +00:00
|
|
|
|
private readonly UserRepositoryImpl _repositoryUserImpl;
|
|
|
|
|
internal IGroupRepository RepositoryGroupImpl { get; }
|
2024-10-21 11:56:16 +00:00
|
|
|
|
|
2024-10-25 09:20:05 +00:00
|
|
|
|
public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
|
2024-11-01 06:57:09 +00:00
|
|
|
|
{
|
|
|
|
|
_repositoryUserImpl = repositoryImpl;
|
|
|
|
|
RepositoryGroupImpl = (IGroupRepository?)(repositoryGroupImpl ?? throw new ArgumentNullException(nameof(repositoryGroupImpl)));
|
|
|
|
|
}
|
2024-10-21 11:56:16 +00:00
|
|
|
|
|
2024-11-02 09:07:18 +00:00
|
|
|
|
public List<UserDao> GetAllUsers() => _repositoryUserImpl.GetAllUsersList()
|
2024-11-01 07:49:39 +00:00
|
|
|
|
.Join(RepositoryGroupImpl.AllGroup,
|
2024-10-21 11:56:16 +00:00
|
|
|
|
user => user.GroupID,
|
|
|
|
|
group => group.Id,
|
|
|
|
|
(user, group) =>
|
2024-11-02 09:07:18 +00:00
|
|
|
|
new UserDao { FIO = user.FIO,
|
|
|
|
|
UserGuid = user.Guid,
|
|
|
|
|
Group = new GroupDao { Id = group.Id, GroupName = group.GroupName },
|
|
|
|
|
GroupID = group.Id }
|
|
|
|
|
).ToList();
|
2024-10-21 11:56:16 +00:00
|
|
|
|
|
2024-10-25 09:20:05 +00:00
|
|
|
|
public bool RemoveUserByGuid(Guid userGuid)
|
|
|
|
|
{
|
|
|
|
|
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
|
2024-10-21 11:56:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 09:07:18 +00:00
|
|
|
|
public UserDao UpdateUser(UserDao user, int groupId)
|
2024-11-02 07:59:27 +00:00
|
|
|
|
{
|
2024-11-02 09:07:18 +00:00
|
|
|
|
UserLocalEntity userLocalEntity = new UserLocalEntity { FIO = user.FIO, GroupID = groupId, Guid = user.UserGuid };
|
2024-11-02 07:59:27 +00:00
|
|
|
|
UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEntity);
|
2024-11-02 09:07:18 +00:00
|
|
|
|
if (result == null) throw new Exception("Пользователь не обновлен.");
|
|
|
|
|
GroupDao? group = GetAllGroups().FirstOrDefault(it => it.Id == result.GroupID);
|
|
|
|
|
if (group == null) throw new Exception("Група не нфйдуна.");
|
|
|
|
|
return new UserDao { FIO = user.FIO, UserGuid = user.UserGuid, Group = group, GroupID = group.Id };
|
2024-11-02 07:59:27 +00:00
|
|
|
|
}
|
2024-10-25 09:20:05 +00:00
|
|
|
|
|
2024-11-02 09:07:18 +00:00
|
|
|
|
public IEnumerable<GroupDao> GetAllGroups()
|
2024-11-01 07:49:39 +00:00
|
|
|
|
{
|
2024-11-02 09:07:18 +00:00
|
|
|
|
return RepositoryGroupImpl.AllGroup;
|
2024-11-01 07:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 09:07:18 +00:00
|
|
|
|
public UserDao FindUserByGuid(Guid userGuid)
|
|
|
|
|
{
|
|
|
|
|
var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Пользователь не найден.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GroupDao group = RepositoryGroupImpl.AllGroup.FirstOrDefault(g => g.Id == user.GroupID);
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Группа не найдена.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new UserDao { FIO = user.FIO, UserGuid = user.Guid, Group = group, GroupID = group.Id };
|
|
|
|
|
}
|
2024-10-31 11:37:45 +00:00
|
|
|
|
|
2024-11-02 09:07:18 +00:00
|
|
|
|
public List<GroupDao> AllGroup => RepositoryGroupImpl.AllGroup.ToList();
|
2024-11-01 07:49:39 +00:00
|
|
|
|
List<GroupDao> IGroupRepository.AllGroup => throw new NotImplementedException();
|
2024-11-01 06:57:09 +00:00
|
|
|
|
|
2024-11-01 07:49:39 +00:00
|
|
|
|
IEnumerable<System.Text.RegularExpressions.Group> IGroupRepository.AllGroups()
|
2024-10-31 11:37:45 +00:00
|
|
|
|
{
|
2024-11-01 07:49:39 +00:00
|
|
|
|
return (IEnumerable<System.Text.RegularExpressions.Group>)((IGroupRepository)_repositoryUserImpl).GetAllGroups();
|
2024-10-31 11:37:45 +00:00
|
|
|
|
}
|
2024-11-01 06:57:09 +00:00
|
|
|
|
internal class UserLocalEntity
|
|
|
|
|
{
|
|
|
|
|
public string FIO { get; set; }
|
2024-11-02 07:59:27 +00:00
|
|
|
|
public int GroupID { get; set; }
|
2024-11-01 06:57:09 +00:00
|
|
|
|
public Guid Guid { get; set; }
|
|
|
|
|
}
|
2024-10-31 11:37:45 +00:00
|
|
|
|
}
|
2024-10-25 09:20:05 +00:00
|
|
|
|
}
|