223 lines
8.2 KiB
C#
223 lines
8.2 KiB
C#
using ComputersShopContracts.BindingModels;
|
||
using ComputersShopContracts.BusinessLogicsContracts;
|
||
using ComputersShopContracts.SearchModels;
|
||
using ComputersShopContracts.StoragesContracts;
|
||
using ComputersShopContracts.ViewModels;
|
||
using ComputersShopDataModels.Models;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Xml.Linq;
|
||
|
||
namespace ComputersShopBusinessLogic.BusinessLogic
|
||
{
|
||
public class ShopLogic : IShopLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IShopStorage _shopStorage;
|
||
private readonly IComputerStorage _computerStorage;
|
||
|
||
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage shopStorage, IComputerStorage computerStorage)
|
||
{
|
||
_logger = logger;
|
||
_shopStorage = shopStorage;
|
||
_computerStorage = computerStorage;
|
||
}
|
||
|
||
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id}", model?.ShopName, model?.Id);
|
||
var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
|
||
public ShopViewModel? ReadElement(ShopSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id);
|
||
var element = _shopStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||
return element;
|
||
}
|
||
|
||
public bool Create(ShopBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_shopStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Update(ShopBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_shopStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(ShopBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_shopStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool MakeSupply(SupplyBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (model.Count <= 0)
|
||
{
|
||
return false;
|
||
throw new ArgumentException("Количество добавляемого компьютера должно быть больше 0", nameof(model.Count));
|
||
}
|
||
var shop = _shopStorage.GetElement(new ShopSearchModel
|
||
{
|
||
Id = model.ShopId
|
||
});
|
||
if (shop == null)
|
||
{
|
||
throw new ArgumentException("Магазина не существует");
|
||
}
|
||
if (model.Capacity - model.Computers.Select(x => x.Value.Item2).Sum() < model.Count)
|
||
{
|
||
throw new ArgumentNullException("В магазине не хватает места", nameof(model.Count));
|
||
}
|
||
if (shop.ShopComputers.ContainsKey(model.ComputerId))
|
||
{
|
||
var oldValue = shop.ShopComputers[model.ComputerId];
|
||
oldValue.Item2 += model.Count;
|
||
shop.ShopComputers[model.ComputerId] = oldValue;
|
||
Capacity = model.Capacity,
|
||
}
|
||
else
|
||
{
|
||
var computer = _computerStorage.GetElement(new ComputerSearchModel
|
||
{
|
||
Id = model.ComputerId
|
||
});
|
||
if (computer == null)
|
||
{
|
||
throw new ArgumentException($"Поставка: Товар с id:{model.ComputerId} не найденн");
|
||
}
|
||
shop.ShopComputers.Add(model.ComputerId, (computer, model.Count));
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(ShopBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.Adress))
|
||
{
|
||
throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(model.Adress));
|
||
}
|
||
if (string.IsNullOrEmpty(model.ShopName))
|
||
{
|
||
throw new ArgumentException("Название магазина должно быть заполнено", nameof(model.ShopName));
|
||
}
|
||
_logger.LogInformation("Shop. ShopName:{ShopName}.Adres:{Adres}.OpeningDate:{OpeningDate}.Id:{ Id}", model.ShopName, model.Adress, model.OpeningDate, model.Id);
|
||
var element = _shopStorage.GetElement(new ShopSearchModel
|
||
{
|
||
ShopName = model.ShopName
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Магазин с таким названием уже есть");
|
||
}
|
||
}
|
||
public bool AddComputers(IComputerModel computer, int quantity)
|
||
{
|
||
if (computer == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(computer));
|
||
}
|
||
if (quantity <= 0)
|
||
{
|
||
throw new ArgumentException("Количество компьютеров должно быть больше 0", nameof(quantity));
|
||
}
|
||
_logger.LogInformation("AddComputers. ShopName:{ShopName}. Id:{Id}", computer.ComputerName, computer.Id);
|
||
var allFreeQuantity = _shopStorage.GetFullList().Select(x => x.Capacity - x.Computers.Select(x => x.Value.Item2).Sum()).Sum();
|
||
if (allFreeQuantity < quantity)
|
||
{
|
||
_logger.LogWarning("AddComputers operation failed.");
|
||
return false;
|
||
}
|
||
foreach (var shop in _shopStorage.GetFullList())
|
||
{
|
||
int freeQuantity = shop.Capacity - shop.Computers.Select(x => x.Value.Item2).Sum();
|
||
if (freeQuantity <= 0)
|
||
{
|
||
continue;
|
||
}
|
||
if (freeQuantity < quantity)
|
||
{
|
||
if (!MakeSupply(new() { Id = shop.Id }, computer, freeQuantity))
|
||
{
|
||
_logger.LogWarning("AddComputers operation failed.");
|
||
return false;
|
||
}
|
||
quantity -= freeQuantity;
|
||
}
|
||
else
|
||
{
|
||
if (!MakeSupply(new() { Id = shop.Id }, computer, quantity))
|
||
{
|
||
_logger.LogWarning("AddComputers operation failed.");
|
||
return false;
|
||
}
|
||
quantity = 0;
|
||
}
|
||
if (quantity == 0)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
_logger.LogWarning("AddComputers operation failed.");
|
||
return false;
|
||
}
|
||
public bool SellComputers(IComputerModel computer, int quantity)
|
||
{
|
||
return _shopStorage.SellComputers(computer, quantity);
|
||
}
|
||
}
|
||
}
|