This commit is contained in:
parap 2023-04-23 20:52:44 +04:00
commit 205cc3df40
4 changed files with 38 additions and 63 deletions

View File

@ -12,13 +12,13 @@ namespace PizzeriaBusinessLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IPizzaStorage _pizzaStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage)
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IPizzaStorage pizzaStorage)
{
_logger = logger;
_orderStorage = orderStorage;
_shopStorage = shopStorage;
_shopLogic = shopLogic;
_pizzaStorage = pizzaStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
@ -74,7 +74,7 @@ namespace PizzeriaBusinessLogic
{
var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId });
if(!_shopStorage.TryRestoreShops(pizza, model.Count))
if(!TryRestoreShops(pizza, model.Count))
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count.");
throw new ArgumentException("Количество изделий слишком велико");
@ -124,5 +124,38 @@ namespace PizzeriaBusinessLogic
_logger.LogInformation("Sum:{ Sum}. Id: { Id}", model.Sum, model.Id);
}
private bool TryRestoreShops(IPizzaModel pizza, int count)
{
int freePlaces = 0;
_shopLogic.ReadList(null).ForEach(x =>
{
int currentFreePlaces = x.Capacity;
foreach (var pair in x.ShopPizzas)
{
currentFreePlaces -= pair.Value.Item2;
}
freePlaces += currentFreePlaces;
});
if (freePlaces < count) return false;
_shopLogic.ReadList(null).ForEach(x =>
{
if (count <= 0) return;
int currentFreePlaces = x.Capacity;
foreach (var elem in x.ShopPizzas)
{
currentFreePlaces -= elem.Value.Item2;
}
_shopLogic.AddPizza(new ShopSearchModel { Id = x.Id }, pizza, Math.Min(currentFreePlaces, count));
count -= currentFreePlaces;
});
return true;
}
}
}

View File

@ -141,6 +141,7 @@ namespace PizzeriaBusinessLogic
ShopName = model.ShopName,
OpenTime = model.OpenTime,
Addres = model.Addres,
Capacity = model.Capacity,
ShopPizzas = model.ShopPizzas,
});

View File

@ -11,7 +11,6 @@ namespace PizzeriaContracts.StoragesContracts
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
ShopViewModel? GetElement(ShopSearchModel model);
bool TrySellPizza(IPizzaModel pizza, int count);
bool TryRestoreShops(IPizzaModel pizza, int count);
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);

View File

@ -42,64 +42,6 @@ namespace PizzeriaFileImplement.Implements
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public bool TryRestoreShops(IPizzaModel pizza, int count)
{
int freePlaces = 0;
source.Shops.ForEach(x =>
{
int currentFreePlaces = x.Capacity;
foreach (var pair in x.ShopPizzas)
{
currentFreePlaces -= pair.Value.Item2;
}
freePlaces += currentFreePlaces;
});
if (freePlaces < count) return false;
source.Shops.ForEach(x =>
{
int currentFreePlaces = x.Capacity;
foreach (var elem in x.ShopPizzas)
{
currentFreePlaces -= elem.Value.Item2;
}
if(!x.ShopPizzas.TryGetValue(pizza.Id, out var check))
{
x.ShopPizzas.Add(pizza.Id, (pizza, 0));
}
x.ShopPizzas.TryGetValue(pizza.Id, out var pair);
if (count >= currentFreePlaces)
{
count -= currentFreePlaces;
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + currentFreePlaces);
}
else
{
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + count);
count = 0;
}
x.Update(new ShopBindingModel
{
Id = x.Id,
Addres = x.Addres,
Capacity = x.Capacity,
OpenTime = x.OpenTime,
ShopName = x.ShopName,
ShopPizzas = x.ShopPizzas
});
});
source.SaveShops();
return true;
}
public bool TrySellPizza(IPizzaModel pizza, int count)
{
int hasCount = 0;