using BarberShopContracts.BindingModels; using BarberShopContracts.BusinessLogicContracts; using BarberShopContracts.SearchModels; using BarberShopContracts.StorageContracts; using BarberShopContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BarberShopBusinessLogic { public class ServiceLogic : IServiceLogic { public readonly ILogger _logger; public readonly IServiceStorage _serviceStorage; public ServiceLogic(ILogger logger, IServiceStorage serviceStorage) { _logger = logger; _serviceStorage = serviceStorage; } public List? ReadList(ServiceSearchModel? model) { _logger.LogInformation("ReadList. ServiceName:{ServiceName}.Id:{ Id}", model?.ServiceName, model?.Id); var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public ServiceViewModel? ReadElement(ServiceSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ServiceName:{ServiceName}.Id:{ Id}", model.ServiceName, model.Id); var element = _serviceStorage.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(ServiceBindingModel model) { CheckModel(model); if (_serviceStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(ServiceBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_serviceStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public bool Update(ServiceBindingModel model) { CheckModel(model); if (_serviceStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(ServiceBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ServiceName)) { throw new ArgumentNullException("Нет названия услуги", nameof(model.ServiceName)); } if (model.ServicePrice <= 0) { throw new ArgumentNullException("Цена должна быть больше 0", nameof(model.ServicePrice)); } if (model.ServiceTime <= 0) { throw new ArgumentNullException("Время должно быть больше 0", nameof(model.ServiceTime)); } _logger.LogInformation("Service. ServiceName:{ServiceName}.ServicePhone:{ ServicePhone}.ServiceTime:{ ServiceTime}. Id: { Id}", model.ServiceName, model.ServicePrice, model.ServiceTime, model.Id); var element = _serviceStorage.GetElement(new ServiceSearchModel { ServiceName = model.ServiceName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Услуга с таким названием уже есть"); } } } }