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

127 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using STOContracts.BindingModels;
using STOContracts.BusinessLogicsContracts;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STOBusinessLogic
{
public class TechnicalWorkLogic : ITechnicalWorkLogic
{
private readonly ILogger _logger;
private readonly ITechnicalWorkStorage _TechnicalWorkStorage;
public TechnicalWorkLogic(ILogger<TechnicalWorkLogic> logger, ITechnicalWorkStorage TechnicalWorkStorage)
{
_logger = logger;
_TechnicalWorkStorage = TechnicalWorkStorage;
}
public List<TechnicalWorkViewModel>? ReadList(TechnicalWorkSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _TechnicalWorkStorage.GetFullList() : _TechnicalWorkStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public TechnicalWorkViewModel? ReadElement(TechnicalWorkSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement.Id:{ Id}", model.Id);
var element = _TechnicalWorkStorage.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(TechnicalWorkBindingModel model)
{
CheckModel(model);
if (_TechnicalWorkStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(TechnicalWorkBindingModel model)
{
CheckModel(model);
if (_TechnicalWorkStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(TechnicalWorkBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_TechnicalWorkStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(TechnicalWorkBindingModel 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));
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentNullException("Нет описания для ТО",
nameof(model.Name));
}
//по умолчанию если дата не была присвоена вручную то мы ей ставили минимальное значение
if (model.Date == DateTime.MinValue)
{
throw new ArgumentNullException("Нет даты",
nameof(model.Date));
}
_logger.LogInformation("TechnicalWork. Name: {Name}.Id: { Id}", model.Name, model.Id);
//имена ТО должны быть уникальными?
var element = _TechnicalWorkStorage.GetElement(new TechnicalWorkSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("ТО с таким названием уже есть");
}
}
}
}