PIbd-23-Nasyrov-A.G.-Flower.../FlowerShopBusinessLogic/FlowerLogic.cs
2024-02-08 15:12:43 +03:00

113 lines
4.1 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 FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopBusinessLogic.BusinessLogic
{
public class FlowerLogic : IFlowerLogic
{
private readonly ILogger _logger;
private readonly IFlowerStorage _flowerStorage;
public FlowerLogic(ILogger<FlowerLogic> logger, IFlowerStorage flowerStorage)
{
_logger = logger;
_flowerStorage = flowerStorage;
}
public List<FlowerViewModel>? ReadList(FlowerSearchModel? model)
{
_logger.LogInformation("ReadList. FlowerName:{FlowerName}.Id:{ Id}", model?.FlowerName, model?.Id);
var list = model == null ? _flowerStorage.GetFullList() : _flowerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public FlowerViewModel? ReadElement(FlowerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FlowerName:{FlowerName}.Id:{ Id}", model.FlowerName, model.Id);
var element = _flowerStorage.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(FlowerBindingModel model)
{
CheckModel(model);
if (_flowerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(FlowerBindingModel model)
{
CheckModel(model);
if (_flowerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(FlowerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_flowerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(FlowerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FlowerName))
{
throw new ArgumentNullException("Нет названия цветка", nameof(model.FlowerName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена цветка должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Flower. FlowerName:{FlowerName}.Cost:{ Cost}. Id: { Id}", model.FlowerName, model.Price, model.Id);
var element = _flowerStorage.GetElement(new FlowerSearchModel
{
FlowerName = model.FlowerName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}