147 lines
5.3 KiB
C#
147 lines
5.3 KiB
C#
using DeviceContracts.BindingModels;
|
|
using DeviceContracts.BusinessLogicsContracts;
|
|
using DeviceContracts.SearchModels;
|
|
using DeviceContracts.StoragesContracts;
|
|
using DeviceContracts.ViewModels;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace DeviceBusinessLogic.BusinessLogics
|
|
{
|
|
public class ServiceLogic : IServiceLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IServiceStorage _serviceStorage;
|
|
public ServiceLogic(ILogger<ServiceLogic> logger,
|
|
IServiceStorage serviceStorage)
|
|
{
|
|
_logger = logger;
|
|
_serviceStorage = serviceStorage;
|
|
}
|
|
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;
|
|
}
|
|
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
|
|
{
|
|
_logger.LogInformation(
|
|
"ReadList. Id: {Id}, Description: {Description}, StartDate: " +
|
|
"{StartDate}, EndDate: {EndDate}, DeviceId: {DeviceId}.",
|
|
model?.Id, model?.Description, model?.StartDate,
|
|
model?.EndDate, model?.DeviceId);
|
|
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}, Description: {Description}, " +
|
|
"StartDate: {StartDate}, EndDate: " +
|
|
"{EndDate}, DeviceId: {DeviceId}.",
|
|
model?.Id, model?.Description, model?.StartDate,
|
|
model?.EndDate, model?.DeviceId);
|
|
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;
|
|
}
|
|
private void CheckModel(ServiceBindingModel model,
|
|
bool withParams = true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
if (model.Id <= 0)
|
|
{
|
|
throw new ArgumentNullException(
|
|
"Идентификатор должен быть больше 0",
|
|
nameof(model.Id));
|
|
}
|
|
if (string.IsNullOrEmpty(model.Description))
|
|
{
|
|
throw new ArgumentNullException(
|
|
"Отсутствует описание",
|
|
nameof(model.Description));
|
|
}
|
|
if (model.DeviceId <= 0)
|
|
{
|
|
throw new ArgumentNullException(
|
|
"Идентификатор устройства должен быть больше 0",
|
|
nameof(model.Id));
|
|
}
|
|
_logger.LogInformation(
|
|
"Id: {Id}, Description: {Description}, " +
|
|
"StartDate: {StartDate}, EndDate: " +
|
|
"{EndDate}, DeviceId: {DeviceId}.",
|
|
model?.Id, model?.Description, model?.StartDate,
|
|
model?.EndDate, model?.DeviceId);
|
|
var elementByDescription = _serviceStorage.GetElement(
|
|
new ServiceSearchModel
|
|
{
|
|
Description = model.Description,
|
|
});
|
|
var elementByDeviceId = _serviceStorage.GetElement(
|
|
new ServiceSearchModel
|
|
{
|
|
DeviceId = model.DeviceId,
|
|
});
|
|
var elementByStartDate = _serviceStorage.GetElement(
|
|
new ServiceSearchModel
|
|
{
|
|
StartDate = model.StartDate,
|
|
});
|
|
var elementByEndDate = _serviceStorage.GetElement(
|
|
new ServiceSearchModel
|
|
{
|
|
EndDate = model.EndDate,
|
|
});
|
|
}
|
|
}
|
|
} |