CourseWork_CarCenter/CarCenter/CarCenterBusinessLogic/BusinessLogics/CarLogic.cs

137 lines
3.9 KiB
C#

using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class CarLogic : ICarLogic
{
private readonly ILogger _logger;
private readonly ICarStorage _carStorage;
private readonly IAdministratorLogic _administratorLogic;
public CarLogic(ILogger<CarLogic> logger, ICarStorage carStorage, IAdministratorLogic administratorLogic)
{
_logger = logger;
_carStorage = carStorage;
_administratorLogic = administratorLogic;
}
public bool Create(CarBindingModel model)
{
CheckModel(model);
var result = _carStorage.Insert(model);
if (result == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CarBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
var result = _carStorage.Delete(model);
if (result == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CarViewModel? ReadElement(CarSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BrandCar:{BrandCar}.Id:{Id}", model.BrandCar, model.Id);
var element = _carStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<CarViewModel>? ReadList(CarSearchModel? model)
{
_logger.LogInformation("ReadList. BrandCar:{BrandCar}.Id:{ Id}", model?.BrandCar, model?.Id);
var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(CarBindingModel model)
{
CheckModel(model);
if (_carStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CarBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.BrandCar))
{
throw new ArgumentNullException("Нет названия бренда", nameof(model.BrandCar));
}
if (string.IsNullOrEmpty(model.Model))
{
throw new ArgumentNullException("Нет названия модели", nameof(model.Model));
}
_logger.LogInformation("Car. BrandCar:{BrandCar}.Model:{ Model}. Id: { Id}", model.BrandCar, model.BrandCar, model.Id);
}
}
}