SushiBarBase/SushiBar/SushiBarListImplement/Implements/SushiStorage.cs

103 lines
3.1 KiB
C#
Raw Permalink Normal View History

2024-02-09 17:38:25 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement.Models;
namespace SushiBarListImplement.Implements
{
public class SushiStorage : ISushiStorage
{
private readonly DataListSingleton _source;
public SushiStorage()
{
2024-02-14 09:58:18 +04:00
_source = DataListSingleton.GetInstance();
2024-02-09 17:38:25 +04:00
}
public List<SushiViewModel> GetFullList()
{
var result = new List<SushiViewModel>();
foreach (var sushi in _source.Sushis)
{
result.Add(sushi.GetViewModel);
}
return result;
}
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
{
var result = new List<SushiViewModel>();
if (string.IsNullOrEmpty(model.SushiName))
{
return result;
}
foreach (var sushi in _source.Sushis)
{
if (sushi.SushiName.Contains(model.SushiName))
{
result.Add(sushi.GetViewModel);
}
}
return result;
}
public SushiViewModel? GetElement(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
{
return null;
}
foreach (var sushi in _source.Sushis)
{
if ((!string.IsNullOrEmpty(model.SushiName) &&
sushi.SushiName == model.SushiName) ||
(model.Id.HasValue && sushi.Id == model.Id))
{
return sushi.GetViewModel;
}
}
return null;
}
public SushiViewModel? Insert(SushiBindingModel model)
{
model.Id = 1;
foreach (var sushi in _source.Sushis)
{
if (model.Id <= sushi.Id)
{
model.Id = sushi.Id + 1;
}
}
var newSushi = Sushi.Create(model);
if (newSushi == null)
{
return null;
}
_source.Sushis.Add(newSushi);
return newSushi.GetViewModel;
}
public SushiViewModel? Update(SushiBindingModel model)
{
foreach (var sushi in _source.Sushis)
{
if (sushi.Id == model.Id)
{
sushi.Update(model);
return sushi.GetViewModel;
}
}
return null;
}
public SushiViewModel? Delete(SushiBindingModel model)
{
for (int i = 0; i < _source.Sushis.Count; ++i)
{
if (_source.Sushis[i].Id == model.Id)
{
var element = _source.Sushis[i];
_source.Sushis.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}