PIbd-23_Zargarov_M.A._Cours.../CarCenter/CarCenterBusinessLogic/BusinessLogics/ConfigurationLogic.cs

105 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class ConfigurationLogic : IConfigurationLogic
{
private readonly ILogger _logger;
private readonly IConfigurationStorage _ConfigurationStorage;
public ConfigurationLogic(ILogger<ConfigurationLogic> logger, IConfigurationStorage ConfigurationStorage)
{
_logger = logger;
_ConfigurationStorage = ConfigurationStorage;
}
public List<ConfigurationViewModel>? ReadList(ConfigurationSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _ConfigurationStorage.GetFullList() : _ConfigurationStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ConfigurationViewModel? ReadElement(ConfigurationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _ConfigurationStorage.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(ConfigurationBindingModel model)
{
CheckModel(model);
if (_ConfigurationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ConfigurationBindingModel model)
{
CheckModel(model);
if (_ConfigurationStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ConfigurationBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_ConfigurationStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ConfigurationBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия комплектации!", nameof(model.Name));
}
if (model.BossId < 0)
{
throw new InvalidOperationException("Id начальника меньше нуля!");
}
_logger.LogInformation("Configuration. Name:{ Name}. BossId: { BossId}. Id: { Id}", model.Name, model.BossId, model.Id);
}
}
}