171 lines
4.6 KiB
C#
171 lines
4.6 KiB
C#
using GiftShopContracts.BindingModels;
|
|
using GiftShopContracts.SearchModels;
|
|
using GiftShopContracts.StoragesContracts;
|
|
using GiftShopContracts.ViewModels;
|
|
using GiftShopListImplement.Models;
|
|
using GiftShopDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Reflection;
|
|
|
|
namespace GiftShopListImplement.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;
|
|
}
|
|
public bool CheckAvailability(int giftId, int count)
|
|
{
|
|
int minus = _source.Shops.Select(x => x.ShopGifts.Select(y => (y.Value.Item1.Id == giftId ? y.Value.Item2 : 0)).Sum()).Sum();
|
|
count -= minus;
|
|
return count <= 0;
|
|
}
|
|
public bool SellGifts(IGiftModel model, int count)
|
|
{
|
|
|
|
var gift = _source.Gifts.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (gift == null || !CheckAvailability(gift.Id, count))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < _source.Shops.Count; i++)
|
|
{
|
|
var shop = _source.Shops[i];
|
|
var gifts = shop.ShopGifts;
|
|
|
|
foreach (var shopgift in gifts.Where(x => x.Value.Item1.Id == gift.Id))
|
|
{
|
|
var min = Math.Min(shopgift.Value.Item2, count);
|
|
gifts[shopgift.Value.Item1.Id] = (shopgift.Value.Item1, shopgift.Value.Item2 - min);
|
|
count -= min;
|
|
|
|
if (count <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
shop.Update(new ShopBindingModel
|
|
{
|
|
Id = shop.Id,
|
|
ShopName = shop.ShopName,
|
|
ShopAdress = shop.ShopAdress,
|
|
OpeningDate = shop.OpeningDate,
|
|
MaxCapacity = shop.MaxCapacity,
|
|
ShopGifts = gifts
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|