presence/Domain/UseCase/UserUseCase.cs

81 lines
3.1 KiB
C#
Raw Normal View History

2024-12-06 08:51:13 +00:00
using presence.Data.LocalData.Entity;
using presence.Data.RemoteData.RemoteDatabase.DAO;
using presence.Data.ReportsHistory;
using presence.Data.Repository;
using presence.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace presence.Domain.UseCase
{
public class UserUseCase
{
private readonly IUserRepository _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl;
public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
{
_repositoryUserImpl = repositoryImpl;
_repositoryGroupImpl = repositoryGroupImpl;
}
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
.Select(it => new Group { ID = it.ID, Name = it.Name }).ToList();
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUser()
.Join(_repositoryGroupImpl.GetAllGroup(),
user => user.GroupID,
group => group.ID,
(user, group) =>
new User
{
UserFIO = user.UserFIO,
UserID = user.UserID,
UserGroup = new Group { ID = group.ID, Name = group.Name }
}
).ToList();
public bool RemoveUserByGuid(int userId)
{
return _repositoryUserImpl.RemoveUserById(userId);
}
public User UpdateUser(User user)
{
UserDao userDao = new UserDao {
UserFIO = user.UserFIO,
GroupID = user.UserGroup.ID,
UserID = user.UserID
};
UserDao? result = _repositoryUserImpl.UpdateUser(userDao);
if (result == null) throw new Exception("Не удалось обновить пользователя");
Group? group = GetAllGroups().FirstOrDefault(it => it.ID == result.GroupID);
if (group == null) throw new Exception("Группа не найдена");
return new User { UserFIO = user.UserFIO, UserID = user.UserID, UserGroup = group };
}
public User FindUserByGuid(int userId)
{
var userDao = _repositoryUserImpl.FindUserById(userId);
return new User
{
UserID = userDao.UserID,
UserFIO = userDao.UserFIO,
UserGroup = GetAllGroups().FirstOrDefault(g => g.ID == userDao.GroupID)
};
}
public UserDao UpdateUserByGuid(int userId, string name, string groupId)
{
UserDao? result = _repositoryUserImpl.UpdateUserById(userId);
if (result == null) throw new Exception("Пользователь не найден");
Group? group = GetAllGroups().FirstOrDefault(it => it.ID == int.Parse(groupId));
if (group == null) throw new Exception("Группа не найдена");
return new UserDao { UserFIO = name, GroupID = int.Parse(groupId), UserID = userId };
}
}
}