PIbd-21_Zaharchenko_M.I._Pi.../Pizzeria/PizzeriaFileImplement/Implements/PizzaStorage.cs
2023-02-22 07:21:55 +03:00

102 lines
3.1 KiB
C#

using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaFileImplement.Models;
namespace PizzeriaFileImplement.Implements
{
public class PizzaStorage : IPizzaStorage
{
private readonly DataFileSingleton _source;
public PizzaStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<PizzaViewModel> GetFullList()
{
var result = new List<PizzaViewModel>();
foreach (var pizzas in _source.Pizzas)
{
result.Add(pizzas.GetViewModel);
}
return result;
}
public List<PizzaViewModel> GetFilteredList(PizzaSearchModel model)
{
var result = new List<PizzaViewModel>();
if (string.IsNullOrEmpty(model.PizzaName))
{
return result;
}
foreach (var pizzas in _source.Pizzas)
{
if (pizzas.PizzaName.Contains(model.PizzaName))
{
result.Add(pizzas.GetViewModel);
}
}
return result;
}
public PizzaViewModel? GetElement(PizzaSearchModel model)
{
if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue)
{
return null;
}
foreach (var pizzas in _source.Pizzas)
{
if ((!string.IsNullOrEmpty(model.PizzaName) && pizzas.PizzaName == model.PizzaName) ||
(model.Id.HasValue && pizzas.Id == model.Id))
{
return pizzas.GetViewModel;
}
}
return null;
}
public PizzaViewModel? Insert(PizzaBindingModel model)
{
model.Id = 1;
foreach (var pizzas in _source.Pizzas)
{
if (model.Id <= pizzas.Id)
{
model.Id = pizzas.Id + 1;
}
}
var newPizzas = Pizza.Create(model);
if (newPizzas == null)
{
return null;
}
_source.Pizzas.Add(newPizzas);
return newPizzas.GetViewModel;
}
public PizzaViewModel? Update(PizzaBindingModel model)
{
foreach (var pizzas in _source.Pizzas)
{
if (pizzas.Id == model.Id)
{
pizzas.Update(model);
return pizzas.GetViewModel;
}
}
return null;
}
public PizzaViewModel? Delete(PizzaBindingModel model)
{
for (int i = 0; i < _source.Pizzas.Count; ++i)
{
if (_source.Pizzas[i].Id == model.Id)
{
var element = _source.Pizzas[i];
_source.Pizzas.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}