106 lines
3.3 KiB
C#
106 lines
3.3 KiB
C#
using data.RemoteData.RemoteDataBase.DAO;
|
||
using data.Repository;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace domain.UseCase
|
||
{
|
||
public class GroupUseCase
|
||
{
|
||
private readonly IGroupRepository _groupRepository;
|
||
|
||
public GroupUseCase(IGroupRepository groupRepository)
|
||
{
|
||
_groupRepository = groupRepository;
|
||
}
|
||
|
||
private void ValidateGroupName(string groupName)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(groupName))
|
||
throw new InvalidDataException("Название группы обязательно");
|
||
}
|
||
|
||
private GroupDAO ValidateGroupExistsAsync(int groupId)
|
||
{
|
||
var group = _groupRepository.GetGroupById(groupId)
|
||
?? throw new ArgumentException("Группа не найдена");
|
||
return group;
|
||
}
|
||
|
||
public async Task<List<GroupDAO>> GetAllGroupsAsync()
|
||
{
|
||
try
|
||
{
|
||
return _groupRepository.GetAllGroups();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentException("Ошибка получения групп", ex);
|
||
}
|
||
}
|
||
|
||
public List<GroupDAO> GetAllGroups()
|
||
{
|
||
return _groupRepository.GetAllGroups();
|
||
}
|
||
|
||
/*public async Task<GroupDAO> GetGroupDetailsAsync(int groupId)
|
||
{
|
||
var group = await ValidateGroupExistsAsync(groupId);
|
||
group.Users = await _groupRepository.GetGroupUsersAsync(groupId);
|
||
return group;
|
||
}*/
|
||
|
||
public async Task<GroupDAO> CreateGroupAsync(string groupName)
|
||
{
|
||
ValidateGroupName(groupName);
|
||
|
||
if (_groupRepository.GetAllGroups().Equals(groupName))
|
||
throw new ArgumentException("Группа с таким названием уже существует");
|
||
|
||
try
|
||
{
|
||
return _groupRepository.AddGroup(groupName);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentException("Ошибка создания группы ", ex);
|
||
}
|
||
}
|
||
|
||
public GroupDAO UpdateGroupAsync(int groupId, string newName)
|
||
{
|
||
ValidateGroupName(newName);
|
||
var existingGroup = ValidateGroupExistsAsync(groupId);
|
||
|
||
existingGroup.Name = newName;
|
||
_groupRepository.UpdateGroupAsync(existingGroup.Id, existingGroup.Name);
|
||
return existingGroup;
|
||
}
|
||
|
||
public async Task DeleteGroupAsync(int groupId)
|
||
{
|
||
ValidateGroupExistsAsync(groupId);
|
||
|
||
try
|
||
{
|
||
await _groupRepository.DeleteGroupAsync(groupId);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentException("Ошибка удаления группы", ex);
|
||
}
|
||
}
|
||
|
||
public async Task ClearGroupStudentsAsync(int groupId)
|
||
{
|
||
ValidateGroupExistsAsync(groupId);
|
||
await _groupRepository.RemoveAllStudentsFromGroup(groupId);
|
||
}
|
||
|
||
public GroupDAO GetGroupById(int groupId)
|
||
{
|
||
return _groupRepository.GetAllGroups().Where(g => g.Id == groupId).FirstOrDefault();
|
||
}
|
||
}
|
||
} |