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

107 lines
3.4 KiB
C#

using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class CarLogic : ICarLogic
{
private readonly ILogger _logger;
private readonly ICarStorage _CarStorage;
public CarLogic(ILogger<CarLogic> logger, ICarStorage CarStorage)
{
_logger = logger;
_CarStorage = CarStorage;
}
public List<CarViewModel>? ReadList(CarSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", 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 CarViewModel? ReadElement(CarSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", 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 bool Create(CarBindingModel model)
{
CheckModel(model);
if (_CarStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CarBindingModel model)
{
CheckModel(model);
if (_CarStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CarBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_CarStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CarBindingModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия машины!", nameof(model.Name));
}
if (model.EmployeeId < 0)
{
throw new InvalidOperationException("Неверный ID сотрудника");
}
_logger.LogInformation("Car. Name:{Name}. EmployeeId:{EmployeeId}. Id:{Id}", model.Name, model.EmployeeId, model.Id);
}
}
}