170 lines
5.0 KiB
C#
170 lines
5.0 KiB
C#
using IceCreamShopContracts.BindingModels;
|
|
using IceCreamShopContracts.SearchModels;
|
|
using IceCreamShopContracts.StoragesContracts;
|
|
using IceCreamShopContracts.ViewModels;
|
|
using IceCreamShopDataModels.Models;
|
|
using IceCreamShopListImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IceCreamShopListImplement.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 CheckAvailability(int iceCreamId, int count)
|
|
{
|
|
foreach (var shop in _source.Shops)
|
|
{
|
|
foreach (var icecream in shop.ShopIceCreams)
|
|
{
|
|
if (icecream.Value.Item1.Id == iceCreamId)
|
|
{
|
|
count -= icecream.Value.Item2;
|
|
}
|
|
if (count <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return count <= 0;
|
|
}
|
|
|
|
public bool SellIceCreams(IIceCreamModel model, int count)
|
|
{
|
|
var iceCream = _source.IceCreams.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (iceCream == null || !CheckAvailability(iceCream.Id, count))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < _source.Shops.Count; i++)
|
|
{
|
|
var shop = _source.Shops[i];
|
|
var icecreams = shop.ShopIceCreams;
|
|
|
|
foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id))
|
|
{
|
|
var min = Math.Min(icecream.Value.Item2, count);
|
|
icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min);
|
|
count -= min;
|
|
|
|
if (count <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
shop.Update(new ShopBindingModel
|
|
{
|
|
Id = shop.Id,
|
|
ShopName = shop.ShopName,
|
|
Address = shop.Address,
|
|
DateOpen = shop.DateOpen,
|
|
MaxCapacity = shop.MaxCapacity,
|
|
ShopIceCreams = icecreams
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|
|
}
|