2024-10-23 08:14:10 +00:00
|
|
|
|
using Zurnal.Data.Repository;
|
|
|
|
|
using Zurnal.domain.Models;
|
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-25 09:20:05 +00:00
|
|
|
|
public class UserUseCase
|
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)
|
|
|
|
|
{
|
|
|
|
|
_repositoryUserImpl = repositoryImpl;
|
|
|
|
|
RepositoryGroupImpl = repositoryGroupImpl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Group> GetAllGroups()
|
|
|
|
|
{
|
|
|
|
|
return RepositoryGroupImpl.GetAllGroup()
|
|
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
|
|
|
|
|
}
|
2024-10-21 11:56:16 +00:00
|
|
|
|
|
2024-10-25 09:20:05 +00:00
|
|
|
|
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers()
|
2024-10-21 11:56:16 +00:00
|
|
|
|
.Join(RepositoryGroupImpl.GetAllGroup(),
|
|
|
|
|
user => user.GroupID,
|
|
|
|
|
group => group.Id,
|
|
|
|
|
(user, group) =>
|
|
|
|
|
new User { FIO = user.FIO,
|
|
|
|
|
Guid = user.Guid,
|
2024-10-25 09:20:05 +00:00
|
|
|
|
Group = new Group { Id = group.Id, Name = group.Name } }
|
|
|
|
|
).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-10-25 09:20:05 +00:00
|
|
|
|
public User UpdateUser(User user)
|
|
|
|
|
{
|
|
|
|
|
UserLocalEnity userLocalEntity = new UserLocalEntity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
|
|
|
|
|
UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEntity);
|
|
|
|
|
if (result == null) throw new Exception("User update failed.");
|
|
|
|
|
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 };
|
2024-10-21 11:56:16 +00:00
|
|
|
|
}
|
2024-10-25 09:20:05 +00:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
}
|