using BankContracts.BindingModels; using BankContracts.BusinessLogicsContracts; using BankContracts.SearchModels; using BankContracts.StoragesContracts; using BankContracts.ViewModels; using Microsoft.Extensions.Logging; namespace BankBusinessLogic.BusinessLogics { public class TermLogic : ITermLogic { private readonly ILogger _logger; private readonly ITermStorage _termStorage; public TermLogic(ILogger logger, ITermStorage termStorage) { _logger = logger; _termStorage = termStorage; } public bool Create(TermBindingModel model) { CheckModel(model); if (_termStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(TermBindingModel model) { CheckModel(model); if (_termStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(TermBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_termStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public List? ReadList(TermSearchModel? model) { _logger.LogInformation("ReadList. Id: {Id}", model?.Id); var list = model == null ? _termStorage.GetFullList() : _termStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public TermViewModel? ReadElement(TermSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id: {Id}", model.Id); var element = _termStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); return element; } private void CheckModel(TermBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.ProgramId <= 0) { throw new ArgumentNullException( "Идентификатор кредитной программы должен быть больше 0", nameof(model.ProgramId)); } if (model.Duration <= 0) { throw new ArgumentNullException( "Длительность должна быть больше 0", nameof(model.Duration)); } _logger.LogInformation("Term. Id: {Id}. ProgramId: " + "{ProgramId}. Duration: {Duration}.", model.Id, model.ProgramId, model.Duration); } } }