PIbd-21_Ihonkina_E.S._Preca.../PrecastConcretePlantBusinessLogic/BusinessLogic/ReinforcedLogic.cs

141 lines
4.3 KiB
C#
Raw Normal View History

2023-02-19 11:27:59 +04:00
using Microsoft.Extensions.Logging;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantBusinessLogic.BusinessLogic
{
public class ReinforcedLogic : IReinforcedLogic
{
private readonly ILogger _logger;
private readonly IReinforcedStorage _reinforcedStorage;
public ReinforcedLogic(ILogger<ReinforcedLogic> logger, IReinforcedStorage reinforcedStorage)
{
_logger = logger;
_reinforcedStorage = reinforcedStorage;
}
public bool Create(ReinforcedBindingModel model)
{
CheckModel(model);
if (_reinforcedStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ReinforcedBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_reinforcedStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ReinforcedViewModel? ReadElement(ReinforcedSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ReinforcedName:{ReinforcedName}.Id:{ Id}", model.ReinforcedName, model.Id);
var element = _reinforcedStorage.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<ReinforcedViewModel>? ReadList(ReinforcedSearchModel? model)
{
_logger.LogInformation("ReadList. ReinforcedName:{ReinforcedName}.Id:{ Id}", model?.ReinforcedName, model?.Id);
var list = model == null ? _reinforcedStorage.GetFullList() : _reinforcedStorage.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(ReinforcedBindingModel model)
{
CheckModel(model);
if (_reinforcedStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ReinforcedBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ReinforcedName))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.ReinforcedName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Reinforced. ReinforcedName:{ReinforcedName}.Price:{ Cost}. Id: { Id}", model.ReinforcedName, model.Price, model.Id);
var element = _reinforcedStorage.GetElement(new ReinforcedSearchModel
{
ReinforcedName = model.ReinforcedName
});
if (element != null && element.Id != model.Id)
{
2023-03-22 20:33:00 +04:00
throw new InvalidOperationException("ЖБИ с таким названием уже есть");
2023-02-19 11:27:59 +04:00
}
}
}
}