using BeautySalonContracts.BindingModels; using BeautySalonContracts.BusinessLogicsContracts; using BeautySalonContracts.SearchModels; using BeautySalonContracts.StoragesContracts; using BeautySalonContracts.ViewModels; using Microsoft.Extensions.Logging; using System; namespace BeautySalonBusinessLogic.BusinessLogics { public class MasterLogic : IMasterLogic { private readonly ILogger _logger; private readonly IMasterStorage _masterStorage; public MasterLogic(ILogger logger, IMasterStorage masterStorage) { _logger = logger; _masterStorage = masterStorage; } public List? ReadList(MasterSearchModel? model) { _logger.LogInformation("ReadList. MasterFIO:{MasterFIO}. Id:{ Id}", model?.MasterFIO, model?.Id); var list = model == null ? _masterStorage.GetFullList() : _masterStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public MasterViewModel? ReadElement(MasterSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. MasterFIO:{MasterFIO}. Id:{ Id}", model.MasterFIO, model.Id); var element = _masterStorage.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 Create(MasterBindingModel model) { CheckModel(model); if (_masterStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(MasterBindingModel model) { CheckModel(model); if (_masterStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(MasterBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_masterStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(MasterBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.MasterFIO)) { throw new ArgumentNullException("Нет ФИО мастера", nameof(model.MasterFIO)); } if (model.Wage <= 0) { throw new ArgumentNullException("Зарплата должна быть больше 0", nameof(model.Wage)); } _logger.LogInformation("Master. MasterFIO:{MasterFIO}. Wage:{ Wage}. Id: { Id} ", model.MasterFIO, model.Wage, model.Id); var element = _masterStorage.GetElement(new MasterSearchModel { MasterFIO = model.MasterFIO }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Мастер с таким ФИО уже есть"); } } public string TestInsertList(int v) { return _masterStorage.TestInsertList(v); } public string TestReadList(int v) { return _masterStorage.TestReadList(v); } } }