97 lines
3.5 KiB
C#
97 lines
3.5 KiB
C#
using DocumentFormat.OpenXml.Drawing.Charts;
|
||
using ElectronicsShopContracts.BindingModels;
|
||
using ElectronicsShopContracts.BusinessLogicContracts;
|
||
using ElectronicsShopContracts.SearchModels;
|
||
using ElectronicsShopContracts.StorageContracts;
|
||
using ElectronicsShopContracts.ViewModels;
|
||
using ElectronicsShopDataModels.Enums;
|
||
using ElectronicsShopDataModels.Models;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
|
||
namespace ElectronicsShopBusinessLogic.BusinessLogic
|
||
{
|
||
public class OrderLogic : IOrderLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IOrderStorage _storage;
|
||
|
||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage storage) {
|
||
_logger = logger;
|
||
_storage = storage;
|
||
}
|
||
public bool CreateOrder(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_storage.Insert(model) == null) {
|
||
_logger.LogInformation("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||
{
|
||
_logger.LogInformation($"ReadList:ID:{model?.ID}");
|
||
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
|
||
if (list == null) {
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadList.Count:{list.Count}");
|
||
return list;
|
||
}
|
||
|
||
private void CheckModel(OrderBindingModel model, bool withParams = true) {
|
||
if (!withParams) {
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty((model.ID).ToString())) {
|
||
throw new ArgumentNullException("Нет ID заказа", nameof(model.ID));
|
||
}
|
||
if (string.IsNullOrEmpty(model.ClientID.ToString())) {
|
||
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientID));
|
||
}
|
||
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}.ClientID:{model.ClientID}");
|
||
}
|
||
|
||
public OrderViewModel? ReadElement(OrderSearchModel model)
|
||
{
|
||
if (model == null) throw new ArgumentNullException(nameof(model));
|
||
_logger.LogInformation($"ReadElement. ID:{model.ID}");
|
||
var element = _storage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement. elementn not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadElement find. ID:{element.ID}");
|
||
return element;
|
||
}
|
||
|
||
public bool AddProduct(IProductModel product, int count, int orderID) {
|
||
var _order = ReadElement(new OrderSearchModel { ID = orderID });
|
||
try {
|
||
if (_order.ProductList.ContainsKey(product.ID)) {
|
||
_order.ProductList[product.ID] = (product, count);
|
||
}
|
||
else {
|
||
_order.ProductList.Add(product.ID, (product, count));
|
||
_storage.Update(new OrderBindingModel {
|
||
ClientID = _order.ClientID,
|
||
ProductList = _order.ProductList,
|
||
Sum = 5000,
|
||
DateCreate = DateTime.Now,
|
||
ID = _order.ID,
|
||
});
|
||
}
|
||
}
|
||
catch {
|
||
_logger.LogWarning("AddProduct. operation is failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
}
|