2023-02-05 19:24:53 +04:00
|
|
|
|
using JewelryStoreContracts.BindingModels;
|
|
|
|
|
using JewelryStoreContracts.BusinessLogicsContracts;
|
|
|
|
|
using JewelryStoreContracts.SearchModels;
|
2023-02-06 15:27:10 +04:00
|
|
|
|
using JewelryStoreContracts.StoragesContracts;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
using JewelryStoreContracts.ViewModels;
|
2023-02-06 15:27:10 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace JewelryStoreBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
2023-02-05 21:54:36 +04:00
|
|
|
|
public class JewelLogic : IJewelLogic // TODO реализовать интерфейс идентично componentLogic
|
2023-02-05 19:24:53 +04:00
|
|
|
|
{
|
2023-02-06 15:27:10 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IJewelStorage _jewelStorage;
|
|
|
|
|
|
|
|
|
|
public JewelLogic(ILogger<JewelLogic> logger, IJewelStorage jewelStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_jewelStorage= jewelStorage;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 19:24:53 +04:00
|
|
|
|
public bool Create(JewelBindingModel model)
|
|
|
|
|
{
|
2023-02-06 15:27:10 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_jewelStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(JewelBindingModel model)
|
|
|
|
|
{
|
2023-02-06 15:27:10 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
if (_jewelStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JewelViewModel? ReadElement(JewelSearchModel model)
|
|
|
|
|
{
|
2023-02-06 15:27:10 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. JewelName:{JewelName}.Id:{ Id}", model.JewelName, model.Id);
|
|
|
|
|
var element = _jewelStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
return element;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<JewelViewModel>? ReadList(JewelSearchModel? model)
|
|
|
|
|
{
|
2023-02-06 15:27:10 +04:00
|
|
|
|
_logger.LogInformation("ReadList. JewelName:{JeweltName}.Id:{ Id}", model?.JewelName, model?.Id);
|
|
|
|
|
var list = model == null ? _jewelStorage.GetFullList() : _jewelStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(JewelBindingModel model)
|
|
|
|
|
{
|
2023-02-06 15:27:10 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_jewelStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private void CheckModel(JewelBindingModel model, bool withParams =
|
|
|
|
|
true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.JewelName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия драгоценности",
|
|
|
|
|
nameof(model.JewelName));
|
|
|
|
|
}
|
|
|
|
|
if (model.Price <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Цена драгоценности должна быть больше 0", nameof(model.Price));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Jewel. JewelName:{JewelName}. Price:{ Price}. Id: { Id}", model.JewelName, model.Price, model.Id);
|
|
|
|
|
var element = _jewelStorage.GetElement(new JewelSearchModel
|
|
|
|
|
{
|
|
|
|
|
JewelName = model.JewelName
|
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Драгоценность с таким названием уже есть");
|
|
|
|
|
}
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|