2024-02-25 19:43:57 +04:00
|
|
|
|
using SushiBarContracts.BindingModel;
|
|
|
|
|
using SushiBarContracts.SearchModel;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarListImplement;
|
2024-02-25 19:52:48 +04:00
|
|
|
|
using SushiBarListImplements.Models;
|
2024-02-25 19:43:57 +04:00
|
|
|
|
|
2024-02-25 19:52:48 +04:00
|
|
|
|
namespace SushiBarListImplements.Implements
|
2024-02-25 19:43:57 +04:00
|
|
|
|
{
|
|
|
|
|
public class ComponentStorage : IComponentStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataListSingleton _source;
|
|
|
|
|
public ComponentStorage()
|
|
|
|
|
{
|
|
|
|
|
_source = DataListSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
public List<ComponentViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
var result = new List<ComponentViewModel>();
|
|
|
|
|
foreach (var component in _source.Components)
|
|
|
|
|
{
|
|
|
|
|
result.Add(component.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<ComponentViewModel>();
|
|
|
|
|
if (string.IsNullOrEmpty(model.ComponentName))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
foreach (var component in _source.Components)
|
|
|
|
|
{
|
|
|
|
|
if (component.ComponentName.Contains(model.ComponentName))
|
|
|
|
|
{
|
|
|
|
|
result.Add(component.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
foreach (var component in _source.Components)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(model.ComponentName) && component.ComponentName == model.ComponentName ||
|
|
|
|
|
model.Id.HasValue && component.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
return component.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
public ComponentViewModel? Insert(ComponentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.Id = 1;
|
|
|
|
|
foreach (var component in _source.Components)
|
|
|
|
|
{
|
|
|
|
|
if (model.Id <= component.Id)
|
|
|
|
|
{
|
|
|
|
|
model.Id = component.Id + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var newComponent = Component.Create(model);
|
|
|
|
|
if (newComponent == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_source.Components.Add(newComponent);
|
|
|
|
|
return newComponent.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
foreach (var component in _source.Components)
|
|
|
|
|
{
|
|
|
|
|
if (component.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
component.Update(model);
|
|
|
|
|
return component.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
public ComponentViewModel? Delete(ComponentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _source.Components.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_source.Components[i].Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
var element = _source.Components[i];
|
|
|
|
|
_source.Components.RemoveAt(i);
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|