using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.BusinessLogicContracts; using ConstructionCompanyContracts.SearchModels; using ConstructionCompanyContracts.StorageContracts; using ConstructionCompanyContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConstructionCompanyBusinessLogic.BusinessLogics { public class OrderLogic : IOrderLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; public OrderLogic(ILogger logger, IOrderStorage orderStorage) { _logger = logger; _orderStorage = orderStorage; } public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. Adress:{Adress}. Id:{Id}", model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public OrderViewModel? ReadElement(OrderSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id:{Id}", model.Id); var element = _orderStorage.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 CreateOrder(OrderBindingModel model) { CheckModel(model); model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Принят; if (_orderStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool TakeOrderInWork(OrderBindingModel model) { CheckModel(model); model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Выполняется; if (_orderStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool FinishOrder(OrderBindingModel model) { CheckModel(model); model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Завершён; model.DateEnd = DateTime.Now; if (_orderStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Description)) { throw new ArgumentNullException("Нет описания заказа", nameof(model.Description)); } if (string.IsNullOrEmpty(model.Adress)) { throw new ArgumentNullException("Нет адреса заказа", nameof(model.Adress)); } if (model.Price <= 0) { throw new InvalidOperationException("Цена должна быть больше 0!"); } if (string.IsNullOrEmpty(model.CustomerNumber)) { throw new ArgumentNullException("Нет телефона", nameof(model.CustomerNumber)); } _logger.LogInformation("Order. Description:{Description}. Adress:{Adress}. Price:{Price} Id:{Id}", model.Description, model.Adress, model.Price, model.Id); } } }