JewelLogic_1

This commit is contained in:
VictoriaPresnyakova 2023-02-06 15:27:10 +04:00
parent 385a43b849
commit 311bcca65f

View File

@ -1,7 +1,9 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.BusinessLogicsContracts;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
@ -12,29 +14,107 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
{
public class JewelLogic : IJewelLogic // TODO реализовать интерфейс идентично componentLogic
{
private readonly ILogger _logger;
private readonly IJewelStorage _jewelStorage;
public JewelLogic(ILogger<JewelLogic> logger, IJewelStorage jewelStorage)
{
_logger = logger;
_jewelStorage= jewelStorage;
}
public bool Create(JewelBindingModel model)
{
throw new NotImplementedException();
CheckModel(model);
if (_jewelStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(JewelBindingModel model)
{
throw new NotImplementedException();
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_jewelStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public JewelViewModel? ReadElement(JewelSearchModel model)
{
throw new NotImplementedException();
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;
}
public List<JewelViewModel>? ReadList(JewelSearchModel? model)
{
throw new NotImplementedException();
_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;
}
public bool Update(JewelBindingModel model)
{
throw new NotImplementedException();
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("Драгоценность с таким названием уже есть");
}
}
}
}