2023-02-05 15:15:38 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.StoragesContract;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
2023-02-15 06:34:44 +04:00
|
|
|
|
using ConfectioneryDataModels.Models;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
|
|
|
|
|
namespace ConfectioneryListImplement
|
|
|
|
|
{
|
|
|
|
|
public class ShopStorage : IShopStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataListSingleton _source;
|
|
|
|
|
public ShopStorage()
|
|
|
|
|
{
|
|
|
|
|
_source = DataListSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel? Delete(ShopBindingModel model)
|
|
|
|
|
{
|
2023-02-05 17:05:49 +04:00
|
|
|
|
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;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
|
|
|
|
{
|
2023-02-05 17:05:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
foreach (var shop in _source.Shops)
|
|
|
|
|
{
|
|
|
|
|
if ((!string.IsNullOrEmpty(model.Name) &&
|
|
|
|
|
shop.Name == model.Name) ||
|
|
|
|
|
(model.Id.HasValue && shop.Id == model.Id))
|
|
|
|
|
{
|
|
|
|
|
return shop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
|
|
|
|
{
|
2023-02-05 17:05:49 +04:00
|
|
|
|
var result = new List<ShopViewModel>();
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
foreach (var shop in _source.Shops)
|
|
|
|
|
{
|
|
|
|
|
if (shop.Name.Contains(model.Name ?? string.Empty))
|
|
|
|
|
{
|
|
|
|
|
result.Add(shop.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ShopViewModel> GetFullList()
|
|
|
|
|
{
|
2023-02-05 17:05:49 +04:00
|
|
|
|
var result = new List<ShopViewModel>();
|
|
|
|
|
foreach (var shop in _source.Shops)
|
|
|
|
|
{
|
|
|
|
|
result.Add(shop.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 06:34:44 +04:00
|
|
|
|
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 15:15:38 +04:00
|
|
|
|
public ShopViewModel? Insert(ShopBindingModel model)
|
|
|
|
|
{
|
2023-02-05 17:05:49 +04:00
|
|
|
|
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;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 06:34:44 +04:00
|
|
|
|
public bool SellPastries(IPastryModel pastry, int needCount)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 15:15:38 +04:00
|
|
|
|
public ShopViewModel? Update(ShopBindingModel model)
|
|
|
|
|
{
|
2023-02-05 17:05:49 +04:00
|
|
|
|
foreach (var shop in _source.Shops)
|
|
|
|
|
{
|
|
|
|
|
if (shop.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
shop.Update(model);
|
|
|
|
|
return shop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-02-05 15:15:38 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|