PIbd-21 Potapov N.S. LabWork02 Hard #9

Closed
ns.potapov wants to merge 34 commits from LabWorkHard02 into LabWork02
Showing only changes of commit 61650ce283 - Show all commits

View File

@ -75,5 +75,58 @@ namespace SecuritySystemFileImplement.Implements
source.SaveShops();
return shop.GetViewModel;
}
public bool SellSecures(ISecureModel secureModel, int securesCount)
{
var car = source.Secures.FirstOrDefault(x => x.Id == secureModel.Id);
if (car == null)
{
return false;
}
var shopSecures = source.Shops.SelectMany(shop => shop.ShopSecures.Where(c => c.Value.Item1.Id == car.Id));
int countStore = 0;
foreach (var it in shopSecures)
countStore += it.Value.Item2;
if (securesCount > countStore)
return false;
foreach (var shop in source.Shops)
{
var cars = shop.ShopSecures;
foreach (var c in cars.Where(x => x.Value.Item1.Id == car.Id))
{
int min = Math.Min(c.Value.Item2, securesCount);
cars[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min);
securesCount -= min;
if (securesCount <= 0)
break;
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Address = shop.Address,
MaxSecuresCount = shop.MaxSecuresCount,
OpeningDate = shop.OpeningDate,
ShopSecures = cars
});
source.SaveShops();
if (securesCount <= 0)
return true;
}
return true;
}
}
}