PIbd-23_Starostin_I.K._Cour.../STOBusinessLogic/CarPartLogic.cs

107 lines
3.5 KiB
C#

using Microsoft.Extensions.Logging;
using STOContracts.BindingModels;
using STOContracts.BusinessLogicContracts;
using STOContracts.SearchModels;
using STOContracts.StorageContracts;
using STOContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STOBusinessLogic
{
public class CarPartLogic: ICarPartLogic
{
private readonly ILogger _logger;
private readonly ICarPartStorage _CarPartStorage;
public CarPartLogic(ILogger<CarPartLogic> logger, ICarPartStorage CarPartStorage)
{
_logger = logger;
_CarPartStorage = CarPartStorage;
}
public List<CarPartViewModel>? ReadList(CarPartSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _CarPartStorage.GetFullList() : _CarPartStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CarPartViewModel? ReadElement(CarPartSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement.Id:{ Id}", model.Id);
var element = _CarPartStorage.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(CarPartBindingModel model)
{
CheckModel(model);
if (_CarPartStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CarPartBindingModel model)
{
CheckModel(model);
if (_CarPartStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CarPartBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_CarPartStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CarPartBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.CarPartName))
{
throw new ArgumentNullException("Нет запчасти",
nameof(model.CarPartName));
}
_logger.LogInformation("CarPart. CarPart:{CarPartName}. Id: { Id}", model.CarPartName, model.Id);
}
}
}