141 lines
4.4 KiB
C#
141 lines
4.4 KiB
C#
using FlowerShopContracts.BindingModels;
|
|
using FlowerShopContracts.SearchModels;
|
|
using FlowerShopContracts.StoragesContracts;
|
|
using FlowerShopContracts.ViewModels;
|
|
using FlowerShopDataModels.Models;
|
|
using FlowerShopListImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FlowerShopListImplement.Implements
|
|
{
|
|
public class ShopStorage : IShopStorage
|
|
{
|
|
private readonly DataListSingleton _source;
|
|
public ShopStorage()
|
|
{
|
|
_source = DataListSingleton.GetInstance();
|
|
}
|
|
public List<ShopViewModel> GetFullList()
|
|
{
|
|
var result = new List<ShopViewModel>();
|
|
foreach (var shop in _source.Shops)
|
|
{
|
|
result.Add(shop.GetViewModel);
|
|
}
|
|
return result;
|
|
}
|
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel
|
|
model)
|
|
{
|
|
var result = new List<ShopViewModel>();
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return result;
|
|
}
|
|
foreach (var shop in _source.Shops)
|
|
{
|
|
if (shop.ShopName.Contains(model.Name))
|
|
{
|
|
result.Add(shop.GetViewModel);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
foreach (var shop in _source.Shops)
|
|
{
|
|
if ((!string.IsNullOrEmpty(model.Name) &&
|
|
shop.ShopName == model.Name) ||
|
|
(model.Id.HasValue && shop.Id == model.Id))
|
|
{
|
|
return shop.GetViewModel;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
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;
|
|
}
|
|
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 bool SupplyFlowers(ShopSearchModel model, IFlowerModel flower, int count)
|
|
{
|
|
if (model == null)
|
|
throw new ArgumentNullException(nameof(model));
|
|
if (flower == null)
|
|
throw new ArgumentNullException(nameof(flower));
|
|
if (count <= 0)
|
|
throw new ArgumentNullException("Количество должно быть положительным числом");
|
|
|
|
ShopViewModel curModel = GetElement(model);
|
|
if (curModel == null)
|
|
throw new ArgumentNullException(nameof(curModel));
|
|
if (curModel.ShopFlowers.TryGetValue(flower.Id, out var pair))
|
|
{
|
|
curModel.ShopFlowers[flower.Id] = (pair.Item1, pair.Item2 + count);
|
|
}
|
|
else
|
|
{
|
|
curModel.ShopFlowers.Add(flower.Id, (flower, count));
|
|
}
|
|
Update(new()
|
|
{
|
|
Id = curModel.Id,
|
|
ShopName = curModel.ShopName,
|
|
DateOpen = curModel.DateOpen,
|
|
Address = curModel.Address,
|
|
ShopFlowers = curModel.ShopFlowers,
|
|
});
|
|
return true;
|
|
}
|
|
}
|
|
}
|