рефакторинг кода логики магазина

This commit is contained in:
Мк Игорь 2023-03-28 13:20:54 +04:00
parent 0e73f3940f
commit 8834faa3ae

View File

@ -78,7 +78,21 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
}
return true;
}
public bool AddManufacture(IManufactureModel manufacturemodel, StoreSearchModel model, int count)
private void SaveChanges(StoreViewModel? model)
{
if (model == null)
{
return;
}
_storeStorage.Update(new StoreBindingModel()
{
StoreName = model.StoreName,
Address = model.Address,
OpeningDate = model.OpeningDate,
Manufactures = model.Manufactures
});
}
public bool AddManufacture(IManufactureModel manufacture, StoreSearchModel model, int count)
{
if (model == null)
{
@ -86,39 +100,30 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
}
if (count <= 0)
{
throw new ArgumentNullException("Количество добавляемых изделий должно быть больше нуля");
throw new ArgumentException("Количество добавляемых изделий должно быть больше нуля");
}
_logger.LogInformation("Adding manufacture {manufacture} in store {store}. Id = {id}", manufacture.ManufactureName, model.StoreName, model.Id);
//ищем магазин
StoreViewModel? selectedStore = _storeStorage.GetElement(model);
if (selectedStore == null)
{
_logger.LogWarning("Store with id {Id} not found", model.Id);
return false;
}
//ищем изделие в магазине
foreach (var manufacture in selectedStore.Manufactures)
foreach (var man in selectedStore.Manufactures)
{
if (manufacture.Item1.Id == manufacturemodel.Id)//если изделие уже есть в магазине, увеличиваем его количество
if (man.Item1.Id == manufacture.Id)//если изделие уже есть в магазине, увеличиваем его количество
{
selectedStore.Manufactures[manufacturemodel.Id - 1] = (manufacturemodel, selectedStore.Manufactures[manufacturemodel.Id - 1].Item2 + count);
_storeStorage.Update(new StoreBindingModel()
{
StoreName = selectedStore.StoreName,
Address = selectedStore.Address,
OpeningDate = selectedStore.OpeningDate,
Manufactures = selectedStore.Manufactures
});
selectedStore.Manufactures[manufacture.Id - 1] = (manufacture, selectedStore.Manufactures[manufacture.Id - 1].Item2 + count);
SaveChanges(selectedStore);
_logger.LogInformation("Filled store Id = {storeId} with manufacture Id = {manufactureId}. Count = {count}", model.i)
return true;
}
}
//иначе добавляем изделие в магазин в указанном количестве
selectedStore.Manufactures.Add((manufacturemodel, count));
_storeStorage.Update(new StoreBindingModel()
{
StoreName = selectedStore.StoreName,
Address = selectedStore.Address,
OpeningDate = selectedStore.OpeningDate,
Manufactures = selectedStore.Manufactures
});
selectedStore.Manufactures.Add((manufacture, count));
SaveChanges(selectedStore);
return true;
}
private void CheckModel(StoreBindingModel model, bool withParams = true)
@ -150,4 +155,4 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
}
}
}
}
}