167 lines
4.7 KiB
C#

using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
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 InspectionLogic : IInspectionLogic
{
private readonly ILogger _logger;
private readonly IInspectionStorage _inspectionStorage;
public InspectionLogic(ILogger<InspectionLogic> logger, IInspectionStorage inspectionStorage)
{
_logger = logger;
_inspectionStorage = inspectionStorage;
}
public bool Create(InspectionBindingModel model)
{
CheckModel(model);
model.InspectionCars = new();
var result = _inspectionStorage.Insert(model);
if (result == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(InspectionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
var result = _inspectionStorage.Delete(model);
if (result == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public InspectionViewModel? ReadElement(InspectionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _inspectionStorage.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<InspectionViewModel>? ReadList(InspectionSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _inspectionStorage.GetFullList() : _inspectionStorage.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(InspectionBindingModel model)
{
CheckModel(model);
if (_inspectionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool AddCarToInspection(InspectionSearchModel model, ICarModel car)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("AddCarToInspection. InspectionName:{InspectionName}.Id:{ Id}", model.InspectionName, model.Id);
var element = _inspectionStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddCarToInspection element not found");
return false;
}
_logger.LogInformation("AddCarToInspection find. Id:{Id}", element.Id);
element.InspectionCars[car.Id] = car;
_inspectionStorage.Update(new()
{
Id = element.Id,
InspectionName = element.InspectionName,
InspectionDate = element.InspectionDate,
AdministratorId = element.AdministratorId,
EmployeeId = element.EmployeeId,
InspectionCars = element.InspectionCars
});
return true;
}
private void CheckModel(InspectionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.InspectionName))
{
throw new ArgumentNullException("Нет названия осмотра", nameof(model.InspectionName));
}
_logger.LogInformation("Inspection. Id: { Id}", model.Id);
}
}
}