Agliullov D. A. Lab Work 2 Hard #5
@ -2,6 +2,7 @@
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.StoragesContract;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
|
||||
namespace ConfectioneryFileImplement
|
||||
{
|
||||
@ -15,93 +16,97 @@ namespace ConfectioneryFileImplement
|
||||
|
||||
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)
|
||||
{
|
||||
var element = _source.Shops[i];
|
||||
_source.Shops.RemoveAt(i);
|
||||
return element.GetViewModel;
|
||||
}
|
||||
_source.Shops.Remove(element);
|
||||
_source.SaveShops();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
||||
if (!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;
|
||||
return _source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
var result = new List<ShopViewModel>();
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
return result;
|
||||
return new();
|
||||
}
|
||||
foreach (var shop in _source.Shops)
|
||||
{
|
||||
if (shop.Name.Contains(model.Name ?? string.Empty))
|
||||
{
|
||||
result.Add(shop.GetViewModel);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return _source.Shops
|
||||
.Select(x => x.GetViewModel)
|
||||
.Where(x => x.Name.Contains(model.Name ?? string.Empty))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
var result = new List<ShopViewModel>();
|
||||
foreach (var shop in _source.Shops)
|
||||
{
|
||||
result.Add(shop.GetViewModel);
|
||||
}
|
||||
return result;
|
||||
return _source.Shops
|
||||
.Select(shop => shop.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
foreach (var shop in _source.Shops)
|
||||
{
|
||||
if (model.Id <= shop.Id)
|
||||
{
|
||||
model.Id = shop.Id + 1;
|
||||
}
|
||||
}
|
||||
model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1;
|
||||
var newShop = Shop.Create(model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
_source.Shops.Add(newShop);
|
||||
_source.SaveShops();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
{
|
||||
shop.Update(model);
|
||||
return shop.GetViewModel;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.StoragesContract;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
|
||||
namespace ConfectioneryListImplement
|
||||
{
|
||||
@ -72,6 +73,11 @@ namespace ConfectioneryListImplement
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
@ -91,6 +97,11 @@ namespace ConfectioneryListImplement
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
public bool SellPastries(IPastryModel pastry, int needCount)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
foreach (var shop in _source.Shops)
|
||||
|
@ -1,6 +1,7 @@
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
|
||||
namespace ConfectioneryContracts.StoragesContract
|
||||
{
|
||||
@ -12,5 +13,8 @@ namespace ConfectioneryContracts.StoragesContract
|
||||
ShopViewModel? Insert(ShopBindingModel model);
|
||||
ShopViewModel? Update(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