using CaseAccountingContracts.BindingModels; using CaseAccountingContracts.BusinessLogicContracts; using CaseAccountingContracts.SearchModels; using CaseAccountingContracts.StoragesContracts; using CaseAccountingContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CaseAccountingBusinessLogic.BusinessLogics { public class ContractLogic : IContractLogic { private readonly ILogger _logger; private readonly IContractStorage _contractStorage; public ContractLogic(ILogger logger, IContractStorage contractStorage) { _logger = logger; _contractStorage = contractStorage; } public bool Create(ContractBindingModel model) { CheckModel(model); if (_contractStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(ContractBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_contractStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public ContractViewModel? ReadElement(ContractSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); var element = _contractStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } public List? ReadList(ContractSearchModel? model) { _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); var list = model == null ? _contractStorage.GetFullList() : _contractStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool Update(ContractBindingModel model) { CheckModel(model); if (_contractStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(ContractBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.UserId < 0) { throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId)); } if (model.Coast <= 0) { throw new ArgumentNullException("Некорректная стоимость услуги", nameof(model.Coast)); } if (model.Service == string.Empty) { throw new ArgumentNullException("Некорректное название услуги", nameof(model.Service)); } } } }