prezzens/Demo/Data/Repository/GroupRepositoryImpl.cs
2024-10-19 22:38:28 +03:00

85 lines
2.3 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 Demo.Data.LocalData;
using Demo.domain.Models;
using Demo.Data.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo.Data.Repository
{
public class GroupRepositoryImpl
{
private List<GroupLocalEntity> _groups;
public GroupRepositoryImpl()
{
_groups = LocalStaticData.groups;
}
public List<GroupLocalEntity> GetAllGroups()
{
return _groups;
}
public void AddGroup(int id, string name)
{
try
{
if (_groups.Any(g => g.Id == id))
{
throw new DataAlreadyExistsException($"Группа с ID {id} уже существует.");
}
_groups.Add(new GroupLocalEntity { Id = id, Name = name });
}
catch (DataAlreadyExistsException ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
public void UpdateGroup(int id, string newName)
{
try
{
var group = _groups.FirstOrDefault(g => g.Id == id);
if (group == null)
{
throw new InvalidGroupIdException($"Группа с ID {id} не существует.");
}
group.Name = newName;
}
catch (InvalidGroupIdException ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
catch (DataNotFoundException ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
public void DeleteGroupById(int id)
{
try
{
var group = _groups.FirstOrDefault(g => g.Id == id);
if (group != null)
{
_groups.Remove(group);
}
else
{
throw new DataNotFoundException($"Группа с ID {id} не найдена.");
}
}
catch (DataNotFoundException ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
}
}