PIbd-23-Volkov-N.A.-Compute.../ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs
2024-02-18 12:22:10 +04:00

124 lines
3.2 KiB
C#

using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopListImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataListSingleton _source;
public ShopStorage()
{
_source = DataListSingleton.GetInstance();
}
public ShopViewModel? Delete(ShopBindingModel model)
{
for (int i = 0; i < _source.Shops.Count; ++i)
{
if (_source.Shops[i].Id == model.Id)
{
var element = _source.Shops[i];
_source.Shops.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
foreach (var Shop in _source.Shops)
{
if ((!string.IsNullOrEmpty(model.ShopName) && Shop.ShopName == model.ShopName) || (model.Id.HasValue && Shop.Id == model.Id))
{
return Shop.GetViewModel;
}
}
return null;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
var result = new List<ShopViewModel>();
if (string.IsNullOrEmpty(model.ShopName))
{
return result;
}
foreach (var Shop in _source.Shops)
{
if (Shop.ShopName.Contains(model.ShopName))
{
result.Add(Shop.GetViewModel);
}
}
return result;
}
public List<ShopViewModel> GetFullList()
{
var result = new List<ShopViewModel>();
foreach (var Shop in _source.Shops)
{
result.Add(Shop.GetViewModel);
}
return result;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = 1;
foreach (var Shop in _source.Shops)
{
if (model.Id <= Shop.Id)
{
model.Id = Shop.Id + 1;
}
}
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
_source.Shops.Add(newShop);
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
foreach (var Shop in _source.Shops)
{
if (Shop.Id == model.Id)
{
Shop.Update(model);
return Shop.GetViewModel;
}
}
return null;
}
}
}