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 TablewareLogic : ITablewareLogic { private readonly ILogger _logger; private readonly ITablewareStorage _tablewareStorage; public TablewareLogic(ILogger logger, ITablewareStorage tablewareStorage) { _logger = logger; _tablewareStorage = tablewareStorage; } public bool Create(TablewareBindingModel model) { CheckModel(model); if (_tablewareStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(TablewareBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_tablewareStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public TablewareViewModel? ReadElement(TablewareSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. TablewareName: {TablewareName}. Id: {Id}", model.TablewareName, model.Id); var element = _tablewareStorage.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(TablewareSearchModel? model) { _logger.LogInformation("ReadList. TablewareName: {TablewareName}. Id: {Id}", model?.TablewareName, model?.Id); var list = model == null ? _tablewareStorage.GetFullList() : _tablewareStorage.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(TablewareBindingModel model) { CheckModel(model); if (_tablewareStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(TablewareBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.TablewareName)) { throw new ArgumentNullException("Нет названия у прибора", nameof(model.TablewareName)); } if (model.VisitorId <= 0) { throw new ArgumentNullException("id посетителя должен дыть больше 0", nameof(model.VisitorId)); } _logger.LogInformation("Tableware. TablewareName: {TablewareName}. VisitorId: {VisitorId}. Id: {Id}", model.TablewareName, model.VisitorId, model.Position, model.Id); var element = _tablewareStorage.GetElement(new TablewareSearchModel { TablewareName = model.TablewareName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такой прибор уже есть"); } } } }