PIbd-23_Nevaeva_K.A._Travel.../TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ConditionLogic.cs

113 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels;
using TravelCompanyContracts.StoragesContracts;
using TravelCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace TravelCompanyBusinessLogic.BusinessLogic
{
public class ConditionLogic : IConditionLogic
{
private readonly ILogger _logger;
private readonly IConditionStorage _ConditionStorage;
public ConditionLogic(ILogger<ConditionLogic> logger, IConditionStorage ConditionStorage)
{
_logger = logger;
_ConditionStorage = ConditionStorage;
}
public List<ConditionViewModel>? ReadList(ConditionSearchModel? model)
{
_logger.LogInformation("ReadList. ConditionName:{ConditionName}.Id:{ Id}", model?.ConditionName, model?.Id);
var list = model == null ? _ConditionStorage.GetFullList() : _ConditionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ConditionViewModel? ReadElement(ConditionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ConditionName:{ConditionName}.Id:{ Id}", model.ConditionName, model.Id);
var element = _ConditionStorage.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(ConditionBindingModel model)
{
CheckModel(model);
if (_ConditionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ConditionBindingModel model)
{
CheckModel(model);
if (_ConditionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ConditionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_ConditionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ConditionBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ConditionName))
{
throw new ArgumentNullException("Нет названия компонента",
nameof(model.ConditionName));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost));
}
_logger.LogInformation("Condition. ConditionName:{ConditionName}.Cost:{ Cost}.Id: { Id}", model.ConditionName, model.Cost, model.Id);
var element = _ConditionStorage.GetElement(new ConditionSearchModel { ConditionName = model.ConditionName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}