ISEbd-21_Zinovev_V.V._Furni.../FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogic/FurnitureLogic.cs
2024-05-04 20:17:10 +04:00

148 lines
4.3 KiB
C#
Raw Permalink 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 FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyBusinessLogic.BusinessLogic
{
public class FurnitureLogic : IFurnitureLogic
{
private readonly ILogger _logger;
private readonly IFurnitureStorage _furnitureStorage;
public FurnitureLogic(ILogger<FurnitureLogic> logger, IFurnitureStorage furnitureStorage)
{
_logger = logger;
_furnitureStorage = furnitureStorage;
}
public List<FurnitureViewModel>? ReadList(FurnitureSearchModel? model)
{
_logger.LogInformation("ReadList. FurnitureName: {FurnitureName}. Id:{Id}", model?.FurnitureName, model?.Id);
var list = model == null ? _furnitureStorage.GetFullList() : _furnitureStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public FurnitureViewModel? ReadElement(FurnitureSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FurnitureName: {FurnitureName}. Id:{Id}", model.FurnitureName, model.Id);
var element = _furnitureStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("Readelement find. Id:{Id}", model.Id);
return element;
}
public bool Create(FurnitureBindingModel model)
{
CheckModel(model);
if (_furnitureStorage.Insert(model) == null)
{
_logger.LogWarning("Create operation failed");
return false;
}
return true;
}
public bool Update(FurnitureBindingModel model)
{
CheckModel(model);
if (_furnitureStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(FurnitureBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete, Id:{Id}", model.Id);
if (_furnitureStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(FurnitureBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FurnitureName))
{
throw new ArgumentNullException("Нет названия заготовки", nameof(model.FurnitureName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена заготовки должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Furniture. FurnitureName:{FurnitureName}. Price:{Price}. Id:{Id}",
model.FurnitureName, model.Price, model.Id);
var element = _furnitureStorage.GetElement(new FurnitureSearchModel
{
FurnitureName = model.FurnitureName,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}