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

107 lines
3.4 KiB
C#

using Microsoft.Extensions.Logging;
using STOContracts.BindingModels;
using STOContracts.BusinessLogicsContracts;
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 ServiceLogic: IServiceLogic
{
private readonly ILogger _logger;
private readonly IServiceStorage _ServiceStorage;
public ServiceLogic(ILogger<ServiceLogic> logger, IServiceStorage ServiceStorage)
{
_logger = logger;
_ServiceStorage = ServiceStorage;
}
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _ServiceStorage.GetFullList() : _ServiceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ServiceViewModel? ReadElement(ServiceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement.Id:{ Id}", model.Id);
var element = _ServiceStorage.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(ServiceBindingModel model)
{
CheckModel(model);
if (_ServiceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ServiceBindingModel model)
{
CheckModel(model);
if (_ServiceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ServiceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_ServiceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ServiceBindingModel 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));
}
_logger.LogInformation("CarPart. Id: { Id}", model.Id);
}
}
}