100 lines
3.5 KiB
C#
100 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 bool Update(OrderBindingModel model) {
|
||
CheckModel(model);
|
||
if (_storage.Update(model) == null) {
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(OrderBindingModel model) {
|
||
CheckModel(model, false);
|
||
_logger.LogInformation($"Delete. ID:{model.ID}");
|
||
if (_storage.Delete(model) == null) {
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool DeleteProduct(OrderBindingModel model) {
|
||
if (_storage.DeleteProduct(model) == null) {
|
||
_logger.LogWarning("Update 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;
|
||
}
|
||
}
|
||
}
|