using CanteenContracts.BindingModels; using CanteenContracts.BusinessLogicsContracts; using CanteenContracts.SearchModel; using CanteenContracts.StoragesContracts; using CanteenContracts.View; 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.FIO, 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?.FIO, 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.FIO)) { throw new ArgumentNullException("Нет ФИО у повара", nameof(model.FIO)); } if (model.ManagerId <= 0) { throw new ArgumentNullException("id менеджера должен быть больше 0", nameof(model.ManagerId)); } if (string.IsNullOrEmpty(model.Position)) { throw new ArgumentNullException("У повара нет должности", nameof(model.Position)); } _logger.LogInformation("Cook. FIO: {FIO}. ManagerId: {ManagerId}. Position: {Position}. Id: {Id}", model.FIO, model.ManagerId, model.Position, model.Id); var element = _cookStorage.GetElement(new CookSearchModel { Id = model.Id }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такой повар уже есть"); } } } }