Merge branch 'lab2_hard' of http://student.git.athene.tech/maxKarme/PIbd-22_Karamushko_M_K_Pizzeria into lab3_hard
This commit is contained in:
commit
205cc3df40
@ -12,13 +12,13 @@ namespace PizzeriaBusinessLogic
|
|||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IOrderStorage _orderStorage;
|
private readonly IOrderStorage _orderStorage;
|
||||||
private readonly IShopStorage _shopStorage;
|
private readonly IShopLogic _shopLogic;
|
||||||
private readonly IPizzaStorage _pizzaStorage;
|
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;
|
_logger = logger;
|
||||||
_orderStorage = orderStorage;
|
_orderStorage = orderStorage;
|
||||||
_shopStorage = shopStorage;
|
_shopLogic = shopLogic;
|
||||||
_pizzaStorage = pizzaStorage;
|
_pizzaStorage = pizzaStorage;
|
||||||
}
|
}
|
||||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||||
@ -74,7 +74,7 @@ namespace PizzeriaBusinessLogic
|
|||||||
{
|
{
|
||||||
var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId });
|
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.");
|
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count.");
|
||||||
throw new ArgumentException("Количество изделий слишком велико");
|
throw new ArgumentException("Количество изделий слишком велико");
|
||||||
@ -124,5 +124,38 @@ namespace PizzeriaBusinessLogic
|
|||||||
_logger.LogInformation("Sum:{ Sum}. Id: { Id}", model.Sum, model.Id);
|
_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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,6 +141,7 @@ namespace PizzeriaBusinessLogic
|
|||||||
ShopName = model.ShopName,
|
ShopName = model.ShopName,
|
||||||
OpenTime = model.OpenTime,
|
OpenTime = model.OpenTime,
|
||||||
Addres = model.Addres,
|
Addres = model.Addres,
|
||||||
|
Capacity = model.Capacity,
|
||||||
ShopPizzas = model.ShopPizzas,
|
ShopPizzas = model.ShopPizzas,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ namespace PizzeriaContracts.StoragesContracts
|
|||||||
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
|
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
|
||||||
ShopViewModel? GetElement(ShopSearchModel model);
|
ShopViewModel? GetElement(ShopSearchModel model);
|
||||||
bool TrySellPizza(IPizzaModel pizza, int count);
|
bool TrySellPizza(IPizzaModel pizza, int count);
|
||||||
bool TryRestoreShops(IPizzaModel pizza, int count);
|
|
||||||
ShopViewModel? Insert(ShopBindingModel model);
|
ShopViewModel? Insert(ShopBindingModel model);
|
||||||
ShopViewModel? Update(ShopBindingModel model);
|
ShopViewModel? Update(ShopBindingModel model);
|
||||||
ShopViewModel? Delete(ShopBindingModel model);
|
ShopViewModel? Delete(ShopBindingModel model);
|
||||||
|
@ -42,64 +42,6 @@ namespace PizzeriaFileImplement.Implements
|
|||||||
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
|
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
|
||||||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
(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)
|
public bool TrySellPizza(IPizzaModel pizza, int count)
|
||||||
{
|
{
|
||||||
int hasCount = 0;
|
int hasCount = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user