using CanteenContracts.BindingModels; using CanteenContracts.BusinessLogicsContracts; using CanteenContracts.SearchModel; using CanteenContracts.SearchModels; using CanteenContracts.StoragesContracts; using CanteenContracts.View; using CanteenDataModels.Enums; using CanteenDataModels.Models; 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 LunchLogic : ILunchLogic { private readonly ILogger _logger; private readonly ILunchStorage _lunchStorage; public LunchLogic(ILogger logger, ILunchStorage lunchStorage) { _logger = logger; _lunchStorage = lunchStorage; } public List? ReadList(LunchSearchModel? model) { _logger.LogInformation("ReadList. Id: {Id}", model?.Id); var list = model == null ? _lunchStorage.GetFullList() : _lunchStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public LunchViewModel? ReadElement(LunchSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id: {Id}", model.Id); var element = _lunchStorage.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(LunchBindingModel model) { CheckModel(model); if (_lunchStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(LunchBindingModel model) { CheckModel(model); if (_lunchStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(LunchBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_lunchStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(LunchBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.LunchName)) { throw new ArgumentNullException("Нет названия обеда", nameof(model.LunchName)); } if (model.VisitorId <= 0) { throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId)); } _logger.LogInformation("Lunch. LunchName: {LunchName}. Sum: {Sum}. VisitorId: {VisitorId}. Id: {Id}", model.LunchName, model.Sum, model.VisitorId, model.Id); var element = _lunchStorage.GetElement(new LunchSearchModel { Id = model.Id }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Обед с таким id уже есть"); } } public bool Finish(LunchBindingModel model) { CheckModel(model, false); if (_lunchStorage.GetElement(new LunchSearchModel { Id = model.Id })?.Status != LunchStatus.Создан) { _logger.LogWarning("Invalid lunch status"); return false; } model.Status = LunchStatus.Окончен; model.DateImplement = DateTime.Now; if (_lunchStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); } return true; } public bool AddOrder(LunchOrderBindingModel model) { CheckModel(model, false); _logger.LogInformation("AddOrder. LunchId: {LunchId}. OrderId: {OrderId}. CountOrder: {CountOrder}", model.OrderId, model.LunchId, model.OrderCount); if (!_lunchStorage.AddOrder(model)) { _logger.LogWarning("AddOrder operation failed"); return false; } return true; } private void CheckModel(LunchOrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.OrderId <= 0) { throw new ArgumentNullException("id заказа должен быть больше 0", nameof(model.OrderId)); } if (model.LunchId <= 0) { throw new ArgumentNullException("id обеда должен быть больше 0", nameof(model.LunchId)); } if (model.VisitorId <= 0) { throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId)); } if (model.OrderCount <= 0) { throw new ArgumentNullException("количество одного заказа должно быть больше 0", nameof(model.OrderCount)); } _logger.LogInformation("LunchOrder. OrderId: {OrderId}. LunchId: {LunchId}. VisitorId: {VisitorId}. OrderCount: {OrderCount}", model.OrderId, model.LunchId, model.VisitorId, model.OrderCount); var element = _lunchStorage.GetLunchOrderElement(new LunchOrderSearchModel { Id = model.Id, OrderId = model.OrderId, LunchId = model.LunchId }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такая связь уже есть"); } } public bool AddProductToLunch(LunchSearchModel model, IProductModel product, int count) { throw new NotImplementedException(); } public bool AddOrderToLunch(LunchSearchModel model, IOrderModel order, int count) { throw new NotImplementedException(); } } }