presence/Domain/UseCase/UserUseCase.cs
Class_Student e771fe1659 new
2024-12-06 11:51:13 +03:00

81 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 };
}
}
}