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-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()
|
|
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name })];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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-11-14 08:01:32 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|