ISEbd-22_Alimova_M.S._Confe.../Confectionery/ConfectioneryListImplement/PastryStorage.cs

114 lines
3.3 KiB
C#
Raw Normal View History

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