using CanteenContracts.BindingModels; using CanteenContracts.BusinessLogicsContracts; using CanteenContracts.SearchModel; using CanteenContracts.StorageContracts; using CanteenContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CanteenBusinessLogic.BusinessLogics { public class CookLogic : ICookLogic { private readonly ILogger _logger; private readonly ICookStorage _cookStorage; public CookLogic(ILogger logger, ICookStorage cookStorage) { _logger = logger; _cookStorage = cookStorage; } public bool Create(CookBindingModel model) { CheckModel(model); if (_cookStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(CookBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_cookStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public CookViewModel? ReadElement(CookSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. FIO:{FIO}.Id:{ Id}", model.CookFIO, model.Id); var element = _cookStorage.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(CookSearchModel? model) { _logger.LogInformation("ReadList. FIO:{FIO}.Id:{ Id} ", model?.CookFIO, model?.Id); var list = (model == null) ? _cookStorage.GetFullList() : _cookStorage.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(CookBindingModel model) { CheckModel(model); if (_cookStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(CookBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля сотрудника", nameof(model.CookFIO)); } if (string.IsNullOrEmpty(model.CookFIO)) { throw new ArgumentNullException("Нет фио сотрудника", nameof(model.CookFIO)); } _logger.LogInformation("Employee. Id: {Id}, FIO: {FIO}", model.Id, model.CookFIO); var element = _cookStorage.GetElement(new CookSearchModel { CookFIO = model.CookFIO, }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Сотрудник с таким фио уже есть"); } } } }