ISEbd-21_Agliullov.D.A._Con.../ConfectionaryListImplement/PastryStorage.cs

109 lines
3.2 KiB
C#
Raw Normal View History

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
using ConfectioneryListImplement.Models;
namespace ConfectioneryListImplement
{
public class PastryStorage : IPastryStorage
{
private readonly DataListSingleton _source;
public PastryStorage()
{
_source = DataListSingleton.GetInstance();
}
public PastryViewModel? Delete(PastryBindingModel model)
{
for (int i = 0; i < _source.Pastry.Count; ++i)
{
if (_source.Pastry[i].Id == model.Id)
{
var element = _source.Pastry[i];
_source.Pastry.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public PastryViewModel? GetElement(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue)
{
return null;
}
foreach (var pastry in _source.Pastry)
{
if ((!string.IsNullOrEmpty(model.PastryName) &&
pastry.PastryName == model.PastryName) ||
(model.Id.HasValue && pastry.Id == model.Id))
{
return pastry.GetViewModel;
}
}
return null;
}
public List<PastryViewModel> GetFilteredList(PastrySearchModel model)
{
var result = new List<PastryViewModel>();
if (string.IsNullOrEmpty(model.PastryName))
{
return result;
}
foreach (var pastry in _source.Pastry)
{
if (pastry.PastryName.Contains(model.PastryName ?? string.Empty))
{
result.Add(pastry.GetViewModel);
}
}
return result;
}
public List<PastryViewModel> GetFullList()
{
var result = new List<PastryViewModel>();
foreach (var pastry in _source.Pastry)
{
result.Add(pastry.GetViewModel);
}
return result;
}
public PastryViewModel? Insert(PastryBindingModel model)
{
model.Id = 1;
foreach (var pastry in _source.Pastry)
{
if (model.Id <= pastry.Id)
{
model.Id = pastry.Id + 1;
}
}
var newPastry = Pastry.Create(model);
if (newPastry == null)
{
return null;
}
_source.Pastry.Add(newPastry);
return newPastry.GetViewModel;
}
public PastryViewModel? Update(PastryBindingModel model)
{
foreach (var pastry in _source.Pastry)
{
if (pastry.Id == model.Id)
{
pastry.Update(model);
return pastry.GetViewModel;
}
}
return null;
}
}
}