2024-10-17 16:08:02 +00:00
|
|
|
using Posechaemost.Data.LocalData.Entity;
|
|
|
|
using Posechaemost.Data.Repository;
|
|
|
|
using Posechaemost.Domain.Models;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Posechaemost.Domain.UseCase
|
|
|
|
{
|
|
|
|
public class UserUseCase
|
|
|
|
{
|
2024-10-23 07:06:56 +00:00
|
|
|
|
|
|
|
private readonly UserRepositoryImpl _repositoryUserImpl;
|
|
|
|
private readonly IGroupRepository _repositoryGroupImpl;
|
2024-10-17 16:08:02 +00:00
|
|
|
|
|
|
|
public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
|
|
|
|
{
|
|
|
|
_repositoryUserImpl = repositoryImpl;
|
|
|
|
_repositoryGroupImpl = repositoryGroupImpl;
|
|
|
|
}
|
|
|
|
|
2024-10-23 07:06:56 +00:00
|
|
|
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
|
2024-10-17 16:08:02 +00:00
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
|
|
|
|
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
|
2024-10-23 07:06:56 +00:00
|
|
|
.Join(_repositoryGroupImpl.GetAllGroup(),
|
2024-10-17 16:08:02 +00:00
|
|
|
user => user.GroupID,
|
|
|
|
group => group.Id,
|
|
|
|
(user, group) =>
|
|
|
|
new User { FIO = user.FIO,
|
|
|
|
Guid = user.Guid,
|
2024-10-18 08:27:50 +00:00
|
|
|
GroupId = new Group {Id = group.Id, Name = group.Name } }
|
2024-10-17 16:08:02 +00:00
|
|
|
).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
public bool RemoveUserByGuid(Guid userGuid) {
|
|
|
|
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
|
|
|
|
}
|
|
|
|
public User UpdateUser(User user) {
|
2024-10-18 08:27:50 +00:00
|
|
|
UserLocalEntity userLocalEnity = new UserLocalEntity { FIO = user.FIO, GroupID = user.GroupId.Id, Guid = user.Guid };
|
2024-10-17 16:08:02 +00:00
|
|
|
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("");
|
2024-10-18 08:27:50 +00:00
|
|
|
return new User { FIO = user.FIO, Guid = user.Guid, GroupId = group};
|
2024-10-17 16:08:02 +00:00
|
|
|
}
|
2024-10-18 06:47:43 +00:00
|
|
|
|
|
|
|
public UserLocalEntity GetUserByGuid(Guid userGuid) {
|
|
|
|
return _repositoryUserImpl.GetUserByGuid(userGuid);
|
|
|
|
}
|
2024-10-18 08:27:50 +00:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
2024-10-17 16:08:02 +00:00
|
|
|
}
|
|
|
|
}
|