2024-11-18 12:42:33 +00:00
|
|
|
|
|
2024-12-16 04:10:04 +00:00
|
|
|
|
using data.Exception;
|
2024-11-18 12:42:33 +00:00
|
|
|
|
using data.RemoteData.RemoteDataBase.DAO;
|
2024-11-11 09:07:11 +00:00
|
|
|
|
using data.Repository;
|
|
|
|
|
using domain.Models;
|
|
|
|
|
|
|
|
|
|
|
2024-11-14 08:01:32 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
namespace domain.UseCase
|
|
|
|
|
{
|
|
|
|
|
public class GroupUseCase
|
|
|
|
|
{
|
|
|
|
|
private readonly IGroupRepository _repositoryGroupImpl;
|
|
|
|
|
|
|
|
|
|
public GroupUseCase(IGroupRepository repositoryGroupImpl)
|
|
|
|
|
{
|
|
|
|
|
_repositoryGroupImpl = repositoryGroupImpl;
|
|
|
|
|
}
|
2024-11-14 11:46:38 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
|
|
|
|
|
private GroupLocalEntity ValidateGroupExistence(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var existingGroup = _repositoryGroupImpl.GetAllGroup()
|
|
|
|
|
.FirstOrDefault(g => g.Id == groupId);
|
|
|
|
|
|
|
|
|
|
if (existingGroup == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Группа не найдена.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 04:10:04 +00:00
|
|
|
|
|
2024-11-14 11:46:38 +00:00
|
|
|
|
return new GroupLocalEntity
|
|
|
|
|
{
|
|
|
|
|
Id = existingGroup.Id,
|
|
|
|
|
Name = existingGroup.Name
|
|
|
|
|
};
|
2024-11-11 09:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Group> GetAllGroups()
|
|
|
|
|
{
|
|
|
|
|
return [.. _repositoryGroupImpl.GetAllGroup()
|
2024-12-18 06:39:51 +00:00
|
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name, Users = it.Users.Select(user => new User { FIO = user.FIO, GroupId = user.GroupID, Guid = user.Guid}).ToList() })];
|
2024-11-11 09:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Group FindGroupById(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var group = GetAllGroups().FirstOrDefault(g => g.Id == groupId);
|
|
|
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Группа не найдена.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void AddGroup(string groupName)
|
|
|
|
|
{
|
2024-12-16 04:10:04 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
|
|
|
|
|
var newId = _repositoryGroupImpl.GetAllGroup().Any()
|
|
|
|
|
? _repositoryGroupImpl.GetAllGroup().Max(g => g.Id) + 1
|
|
|
|
|
: 1;
|
|
|
|
|
|
2024-11-20 09:15:21 +00:00
|
|
|
|
GroupDao newGroup = new GroupDao
|
2024-11-11 09:07:11 +00:00
|
|
|
|
{
|
|
|
|
|
Id = newId,
|
|
|
|
|
Name = groupName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_repositoryGroupImpl.AddGroup(newGroup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveGroupById(int groupId)
|
|
|
|
|
{
|
2024-12-16 04:10:04 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
var existingGroup = ValidateGroupExistence(groupId);
|
|
|
|
|
List<Group> _groups = GetAllGroups();
|
|
|
|
|
|
|
|
|
|
// Находим группу по ID и удаляем ее
|
|
|
|
|
var groupToRemove = _groups.FirstOrDefault(g => g.Id == existingGroup.Id);
|
|
|
|
|
if (groupToRemove != null)
|
|
|
|
|
{
|
|
|
|
|
_groups.Remove(groupToRemove);
|
|
|
|
|
_repositoryGroupImpl.RemoveGroupById(existingGroup.Id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Группа не найдена.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Метод для изменения названия группы
|
2024-11-14 11:46:38 +00:00
|
|
|
|
public bool UpdateGroup(int groupId, string newGroupName)
|
2024-11-11 09:07:11 +00:00
|
|
|
|
{
|
2024-11-14 11:46:38 +00:00
|
|
|
|
var existingGroup = _repositoryGroupImpl.GetAllGroup()
|
|
|
|
|
.FirstOrDefault(g => g.Id == groupId);
|
|
|
|
|
|
|
|
|
|
if (existingGroup == null)
|
|
|
|
|
{
|
|
|
|
|
return false; // Группа с таким ID не найдена
|
|
|
|
|
}
|
2024-11-11 09:07:11 +00:00
|
|
|
|
|
|
|
|
|
existingGroup.Name = newGroupName;
|
|
|
|
|
_repositoryGroupImpl.UpdateGroupById(existingGroup.Id, existingGroup);
|
2024-11-14 11:46:38 +00:00
|
|
|
|
return true; // Успешное обновление
|
2024-11-11 09:07:11 +00:00
|
|
|
|
}
|
2024-11-18 12:42:33 +00:00
|
|
|
|
public List<Group> GetAllGroupWithStident()
|
|
|
|
|
{
|
|
|
|
|
// Загружаем группы с пользователями
|
|
|
|
|
var groups = _repositoryGroupImpl.GetAllGroupWithStident()
|
|
|
|
|
.Select(g => new Group
|
|
|
|
|
{
|
|
|
|
|
Id = g.Id,
|
|
|
|
|
Name = g.Name,
|
|
|
|
|
Users = g.Users.Select(u => new User
|
|
|
|
|
{
|
|
|
|
|
Guid = u.Guid,
|
|
|
|
|
FIO = u.FIO
|
|
|
|
|
}).ToList()
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 18:00:36 +00:00
|
|
|
|
public void RemoveAllStudentsFromGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var existingGroup = ValidateGroupExistence(groupId);
|
|
|
|
|
_repositoryGroupImpl.RemoveAllStudentsFromGroup(existingGroup.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddStudentToGroup(int groupId, User newStudent)
|
|
|
|
|
{
|
|
|
|
|
// Проверяем существование группы по ID
|
|
|
|
|
var existingGroup = ValidateGroupExistence(groupId);
|
|
|
|
|
|
|
|
|
|
// Создаем UserDao для добавления в базу данных
|
|
|
|
|
UserDao studentDao = new UserDao
|
|
|
|
|
{
|
|
|
|
|
Guid = newStudent.Guid,
|
|
|
|
|
FIO = newStudent.FIO
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Проверка существования пользователя в группе (по GUID)
|
|
|
|
|
var existingStudent = existingGroup.Users.FirstOrDefault(u => u.Guid == newStudent.Guid);
|
|
|
|
|
if (existingStudent != null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Студент с GUID {newStudent.Guid} уже добавлен в эту группу.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Добавляем студента в группу
|
|
|
|
|
_repositoryGroupImpl.AddStudentToGroup(existingGroup.Id, studentDao);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-12 08:32:15 +00:00
|
|
|
|
public void RemoveStudentFromGroup(int groupId, Guid studentGuid)
|
|
|
|
|
{
|
|
|
|
|
// Получаем группу из репозитория
|
|
|
|
|
var group = _repositoryGroupImpl.GetGroupById(groupId);
|
|
|
|
|
if (group == null) throw new ArgumentException("Group not found");
|
|
|
|
|
|
|
|
|
|
// Проверяем наличие студента в группе
|
|
|
|
|
var student = group.Users?.FirstOrDefault(u => u.Guid == studentGuid);
|
|
|
|
|
if (student == null) throw new ArgumentException("Student not found in the group");
|
|
|
|
|
|
|
|
|
|
// Удаляем студента из группы
|
|
|
|
|
group.Users.Remove(student);
|
|
|
|
|
|
|
|
|
|
// Преобразуем объект группы в GroupDao
|
|
|
|
|
var groupDao = new GroupDao
|
|
|
|
|
{
|
|
|
|
|
Id = group.Id,
|
|
|
|
|
Name = group.Name,
|
|
|
|
|
Users = group.Users.Select(u => new UserDao
|
|
|
|
|
{
|
|
|
|
|
Guid = u.Guid,
|
|
|
|
|
FIO = u.FIO
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
2024-12-04 18:00:36 +00:00
|
|
|
|
|
2024-12-12 08:32:15 +00:00
|
|
|
|
// Сохраняем изменения в базе данных
|
|
|
|
|
_repositoryGroupImpl.UpdateGroup(groupDao);
|
|
|
|
|
}
|
2024-11-14 08:01:32 +00:00
|
|
|
|
|
2024-12-16 04:10:04 +00:00
|
|
|
|
|
|
|
|
|
public bool RemoveUserByGuid(Guid userGuid)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _repositoryGroupImpl.RemoveUserByGuid(userGuid);
|
|
|
|
|
}
|
|
|
|
|
catch (UserNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка: {ex.Message}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (RepositoryException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка в репозитории: {ex.Message}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserDao UpdateUser(Guid userGuid, string newFio, int groupId)
|
|
|
|
|
{
|
|
|
|
|
UserDao userDao = new UserDao
|
|
|
|
|
{
|
|
|
|
|
Guid = userGuid,
|
|
|
|
|
FIO = newFio,
|
|
|
|
|
GroupID = groupId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return userDao;
|
|
|
|
|
}
|
2024-11-11 09:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|