presence/domain/UseCase/GroupUseCase.cs
2024-11-14 11:01:32 +03:00

102 lines
2.7 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 data.LocalData;
using data.Repository;
using domain.Models;
namespace domain.UseCase
{
public class GroupUseCase
{
private readonly IGroupRepository _repositoryGroupImpl;
public GroupUseCase(IGroupRepository repositoryGroupImpl)
{
_repositoryGroupImpl = repositoryGroupImpl;
}
private GroupLocalEntity ValidateGroupExistence(int groupId)
{
var existingGroup = _repositoryGroupImpl.GetAllGroup()
.FirstOrDefault(g => g.Id == groupId);
if (existingGroup == null)
{
throw new ArgumentException("Группа не найдена.");
}
return existingGroup;
}
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;
GroupLocalEntity newGroup = new GroupLocalEntity
{
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("Группа не найдена.");
}
}
// Метод для изменения названия группы
public void UpdateGroup(int groupId, string newGroupName)
{
var existingGroup = ValidateGroupExistence(groupId);
existingGroup.Name = newGroupName;
_repositoryGroupImpl.UpdateGroupById(existingGroup.Id, existingGroup);
}
}
}