Реализована логика хранилища магазинов
This commit is contained in:
parent
771985a191
commit
4368d4d6cc
@ -2,6 +2,7 @@
|
|||||||
using ConfectioneryContracts.SearchModels;
|
using ConfectioneryContracts.SearchModels;
|
||||||
using ConfectioneryContracts.StoragesContract;
|
using ConfectioneryContracts.StoragesContract;
|
||||||
using ConfectioneryContracts.ViewModels;
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using ConfectioneryDataModels.Models;
|
||||||
|
|
||||||
namespace ConfectioneryFileImplement
|
namespace ConfectioneryFileImplement
|
||||||
{
|
{
|
||||||
@ -15,93 +16,97 @@ namespace ConfectioneryFileImplement
|
|||||||
|
|
||||||
public ShopViewModel? Delete(ShopBindingModel model)
|
public ShopViewModel? Delete(ShopBindingModel model)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _source.Shops.Count; ++i)
|
var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
{
|
{
|
||||||
if (_source.Shops[i].Id == model.Id)
|
_source.Shops.Remove(element);
|
||||||
{
|
_source.SaveShops();
|
||||||
var element = _source.Shops[i];
|
|
||||||
_source.Shops.RemoveAt(i);
|
|
||||||
return element.GetViewModel;
|
return element.GetViewModel;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
if (!model.Id.HasValue)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
foreach (var shop in _source.Shops)
|
return _source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||||
{
|
|
||||||
if ((!string.IsNullOrEmpty(model.Name) &&
|
|
||||||
shop.Name == model.Name) ||
|
|
||||||
(model.Id.HasValue && shop.Id == model.Id))
|
|
||||||
{
|
|
||||||
return shop.GetViewModel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||||
{
|
{
|
||||||
var result = new List<ShopViewModel>();
|
|
||||||
if (string.IsNullOrEmpty(model.Name))
|
if (string.IsNullOrEmpty(model.Name))
|
||||||
{
|
{
|
||||||
return result;
|
return new();
|
||||||
}
|
}
|
||||||
foreach (var shop in _source.Shops)
|
return _source.Shops
|
||||||
{
|
.Select(x => x.GetViewModel)
|
||||||
if (shop.Name.Contains(model.Name ?? string.Empty))
|
.Where(x => x.Name.Contains(model.Name ?? string.Empty))
|
||||||
{
|
.ToList();
|
||||||
result.Add(shop.GetViewModel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShopViewModel> GetFullList()
|
public List<ShopViewModel> GetFullList()
|
||||||
{
|
{
|
||||||
var result = new List<ShopViewModel>();
|
return _source.Shops
|
||||||
foreach (var shop in _source.Shops)
|
.Select(shop => shop.GetViewModel)
|
||||||
{
|
.ToList();
|
||||||
result.Add(shop.GetViewModel);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShopViewModel? Insert(ShopBindingModel model)
|
public ShopViewModel? Insert(ShopBindingModel model)
|
||||||
{
|
{
|
||||||
model.Id = 1;
|
model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1;
|
||||||
foreach (var shop in _source.Shops)
|
|
||||||
{
|
|
||||||
if (model.Id <= shop.Id)
|
|
||||||
{
|
|
||||||
model.Id = shop.Id + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var newShop = Shop.Create(model);
|
var newShop = Shop.Create(model);
|
||||||
if (newShop == null)
|
if (newShop == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
_source.Shops.Add(newShop);
|
_source.Shops.Add(newShop);
|
||||||
|
_source.SaveShops();
|
||||||
return newShop.GetViewModel;
|
return newShop.GetViewModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShopViewModel? Update(ShopBindingModel model)
|
public ShopViewModel? Update(ShopBindingModel model)
|
||||||
{
|
{
|
||||||
foreach (var shop in _source.Shops)
|
var shop = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (shop == null)
|
||||||
{
|
{
|
||||||
if (shop.Id == model.Id)
|
|
||||||
{
|
|
||||||
shop.Update(model);
|
|
||||||
return shop.GetViewModel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
shop.Update(model);
|
||||||
|
_source.SaveShops();
|
||||||
|
return shop.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
||||||
|
{
|
||||||
|
var resultCount = _source.Shops
|
||||||
|
.Select(shop => shop.Pastries
|
||||||
|
.FirstOrDefault(x => x.Key == pastry.Id).Value.Item2)
|
||||||
|
.Sum();
|
||||||
|
return resultCount >= needCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SellPastries(IPastryModel pastry, int needCount)
|
||||||
|
{
|
||||||
|
if (!HasNeedPastries(pastry, needCount))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id)))
|
||||||
|
{
|
||||||
|
var tuple = pair.Pastries[pastry.Id];
|
||||||
|
var diff = Math.Min(tuple.Item2, needCount);
|
||||||
|
pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff);
|
||||||
|
needCount -= diff;
|
||||||
|
if (needCount <= 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using ConfectioneryContracts.SearchModels;
|
using ConfectioneryContracts.SearchModels;
|
||||||
using ConfectioneryContracts.StoragesContract;
|
using ConfectioneryContracts.StoragesContract;
|
||||||
using ConfectioneryContracts.ViewModels;
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using ConfectioneryDataModels.Models;
|
||||||
|
|
||||||
namespace ConfectioneryListImplement
|
namespace ConfectioneryListImplement
|
||||||
{
|
{
|
||||||
@ -72,6 +73,11 @@ namespace ConfectioneryListImplement
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public ShopViewModel? Insert(ShopBindingModel model)
|
public ShopViewModel? Insert(ShopBindingModel model)
|
||||||
{
|
{
|
||||||
model.Id = 1;
|
model.Id = 1;
|
||||||
@ -91,6 +97,11 @@ namespace ConfectioneryListImplement
|
|||||||
return newShop.GetViewModel;
|
return newShop.GetViewModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool SellPastries(IPastryModel pastry, int needCount)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public ShopViewModel? Update(ShopBindingModel model)
|
public ShopViewModel? Update(ShopBindingModel model)
|
||||||
{
|
{
|
||||||
foreach (var shop in _source.Shops)
|
foreach (var shop in _source.Shops)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using ConfectioneryContracts.BindingModels;
|
using ConfectioneryContracts.BindingModels;
|
||||||
using ConfectioneryContracts.SearchModels;
|
using ConfectioneryContracts.SearchModels;
|
||||||
using ConfectioneryContracts.ViewModels;
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using ConfectioneryDataModels.Models;
|
||||||
|
|
||||||
namespace ConfectioneryContracts.StoragesContract
|
namespace ConfectioneryContracts.StoragesContract
|
||||||
{
|
{
|
||||||
@ -12,5 +13,8 @@ namespace ConfectioneryContracts.StoragesContract
|
|||||||
ShopViewModel? Insert(ShopBindingModel model);
|
ShopViewModel? Insert(ShopBindingModel model);
|
||||||
ShopViewModel? Update(ShopBindingModel model);
|
ShopViewModel? Update(ShopBindingModel model);
|
||||||
ShopViewModel? Delete(ShopBindingModel model);
|
ShopViewModel? Delete(ShopBindingModel model);
|
||||||
|
|
||||||
|
bool HasNeedPastries(IPastryModel pastry, int needCount);
|
||||||
|
public bool SellPastries(IPastryModel pastry, int needCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user