104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using Demo.Data.LocalData;
|
||
using Demo.Domain.Models;
|
||
using Demo.Data.Exceptions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Demo.Domain.UseCase;
|
||
|
||
namespace Demo.Data.Repository
|
||
{
|
||
public class GroupRepositoryImpl: IGroupRepository
|
||
{
|
||
private List<GroupLocalEntity> _groups;
|
||
|
||
public GroupRepositoryImpl()
|
||
{
|
||
_groups = LocalStaticData.groups;
|
||
}
|
||
|
||
public List<GroupLocalEntity> GetAllGroups()
|
||
{
|
||
return _groups;
|
||
}
|
||
|
||
public bool AddGroup(GroupLocalEntity group)
|
||
{
|
||
_groups.Add(new GroupLocalEntity { Id = group.Id, Name = group.Name });
|
||
return true;
|
||
}
|
||
|
||
public bool UpdateGroup(int id)
|
||
{
|
||
try
|
||
{
|
||
var group = _groups.FirstOrDefault(g => g.Id == id);
|
||
|
||
if (group == null)
|
||
{
|
||
throw new InvalidGroupIdException($"Группа с ID {id} не существует.");
|
||
}
|
||
string newName = Console.ReadLine();
|
||
try
|
||
{
|
||
if (newName != "")
|
||
{
|
||
group.Name = newName;
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
Console.WriteLine("Введите корректное название группы");
|
||
}
|
||
return true;
|
||
}
|
||
catch (InvalidGroupIdException ex)
|
||
{
|
||
Console.WriteLine($"Ошибка: {ex.Message}");
|
||
}
|
||
catch (DataNotFoundException ex)
|
||
{
|
||
Console.WriteLine($"Ошибка: {ex.Message}");
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool DeleteGroupById(int id)
|
||
{
|
||
try
|
||
{
|
||
var group = _groups.FirstOrDefault(g => g.Id == id);
|
||
if (group != null)
|
||
{
|
||
_groups.Remove(group);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
throw new DataNotFoundException($"Группа с ID {id} не найдена.");
|
||
}
|
||
}
|
||
catch (DataNotFoundException ex)
|
||
{
|
||
Console.WriteLine($"Ошибка: {ex.Message}");
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public GroupLocalEntity GetGroupById(int groupID)
|
||
{
|
||
var _groups = GetAllGroups();
|
||
foreach (var _group in _groups)
|
||
{
|
||
if (_group.Id == groupID)
|
||
{
|
||
Console.WriteLine();
|
||
Console.WriteLine($"ID группы: {_group.Id} Название группы: {_group.Name}");
|
||
Console.WriteLine();
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
}
|