ISEbd-22_Baygulov_A.A._Sush.../SushiBar/SushiBarListImplement/SushiStorage.cs

114 lines
3.2 KiB
C#
Raw Permalink Normal View History

using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarListImplement.Implements
{
public class SushiStorage : ISushiStorage
{
private readonly DataListSingleton _source;
public SushiStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<SushiViewModel> GetFullList()
{
var result = new List<SushiViewModel>();
foreach (var Sushis in _source.Sushis)
{
result.Add(Sushis.GetViewModel);
}
return result;
}
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
{
var result = new List<SushiViewModel>();
if (string.IsNullOrEmpty(model.SushiName))
{
return result;
}
foreach (var Sushis in _source.Sushis)
{
if (Sushis.SushiName.Contains(model.SushiName))
{
result.Add(Sushis.GetViewModel);
}
}
return result;
}
public SushiViewModel? GetElement(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
{
return null;
}
foreach (var sushis in _source.Sushis)
{
if ((!string.IsNullOrEmpty(model.SushiName) && sushis.SushiName == model.SushiName) ||
(model.Id.HasValue && sushis.Id == model.Id))
{
return sushis.GetViewModel;
}
}
return null;
}
public SushiViewModel? Insert(SushiBindingModel model)
{
model.Id = 1;
foreach (var sushis in _source.Sushis)
{
if (model.Id <= sushis.Id)
{
model.Id = sushis.Id + 1;
}
}
var newSushis = Sushi.Create(model);
if (newSushis == null)
{
return null;
}
_source.Sushis.Add(newSushis);
return newSushis.GetViewModel;
}
public SushiViewModel? Update(SushiBindingModel model)
{
foreach (var sushis in _source.Sushis)
{
if (sushis.Id == model.Id)
{
sushis.Update(model);
return sushis.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;
}
}
}