pr1/Demo/Domain/UseCase/GroupUseCase.cs

52 lines
1.5 KiB
C#
Raw Normal View History

2024-10-19 21:39:57 +00:00
using Demo.Data.LocalData;
using Demo.Data.Repository;
using Demo.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Demo.Domain.UseCase
{
public class GroupUseCase
{
private List<GroupLocalEntity> _groups = LocalStaticData.groups;
public List<GroupLocalEntity> GetAllGroups() => _groups;
public void AddGroup(GroupLocalEntity group)
{
group.Id = _groups.Any() ? _groups.Max(g => g.Id) + 1 : 1;
_groups.Add(group);
}
public void UpdateGroup(GroupLocalEntity group)
{
var existingGroup = _groups.FirstOrDefault(g => g.Id == group.Id);
if (existingGroup != null)
{
existingGroup.Name = group.Name;
}
}
public void RenameGroup(int groupId, string newName)
{
if (string.IsNullOrWhiteSpace(newName))
{
throw new ArgumentException("Новое имя группы не может быть пустым", nameof(newName));
}
var existingGroup = _groups.FirstOrDefault(g => g.Id == groupId);
if (existingGroup == null)
{
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
}
existingGroup.Name = newName;
}
}
}