using CarCenterContracts.BindingModels; using CarCenterContracts.BusinessLogicsContracts; using CarCenterContracts.SearchModels; using CarCenterContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using CarCenterContracts.StoragesContracts; namespace CarCenterBusinessLogic.BusinessLogics { public class BossLogic : IBossLogic { private readonly ILogger _logger; private readonly IBossStorage _BossStorage; public BossLogic(ILogger logger, IBossStorage BossStorage) { _logger = logger; _BossStorage = BossStorage; } public List? ReadList(BossSearchModel? model) { _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); var list = model == null ? _BossStorage.GetFullList() : _BossStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public BossViewModel? ReadElement(BossSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id:{ Id}. Login: { Login}", model.Id, model.Login); var element = _BossStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}. Login: { Login}", element.Id); return element; } public bool Create(BossBindingModel model) { CheckModel(model); if (_BossStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(BossBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_BossStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public bool Update(BossBindingModel model) { CheckModel(model); if (_BossStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(BossBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Нет имени начальника!", nameof(model.Name)); } if (string.IsNullOrEmpty(model.Surname)) { throw new ArgumentNullException("Нет фамилии начальника!", nameof(model.Surname)); } if (string.IsNullOrEmpty(model.Login)) { throw new ArgumentNullException("Нет логина начальника!", nameof(model.Login)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля начальника!", nameof(model.Password)); } _logger.LogInformation("Boss. Surname:{Surname}. Name:{Name}. Login:{Login}. Password:{Password}. Id:{Id}", model.Surname, model.Name, model.Login, model.Password, model.Id); var element = _BossStorage.GetElement(new BossSearchModel { Login = model.Login }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Начальник с таким логином уже есть"); } } } }