Илья Федотов d754b31220 Base.07
2024-04-19 11:14:33 +04:00

97 lines
3.8 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 DinerContracts.BindingModels;
using DinerContracts.BusinessLogicsContacts;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DineryBusinessLogic.BusinessLogic
{
public class FoodLogic : IFoodLogic
{
private readonly ILogger _logger;
private readonly IFoodStorage _componentStorage;
public FoodLogic(ILogger<FoodLogic> logger, IFoodStorage componentStorage) {
_logger = logger;
_componentStorage = componentStorage;
}
public bool Create(FoodBindingModel model)
{
CheckModel(model);
if (_componentStorage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(FoodBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_componentStorage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public FoodViewModel? ReadElement(FoodSearchModel model)
{
if (model == null) throw new ArgumentNullException(nameof(model));
_logger.LogInformation("ReadElement. ProductName:{ProductName}. ID:{ID}", model.ComponentName, model.ID);
var element = _componentStorage.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<FoodViewModel>? ReadList(FoodSearchModel? model)
{
_logger.LogInformation("ReadList. ProductName:{ProductName}. ID:{ID}", model?.ComponentName, model?.ID);
var list = model == null ? _componentStorage.GetFullList() : _componentStorage.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(FoodBindingModel model)
{
CheckModel(model);
if (_componentStorage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(FoodBindingModel model, bool withParams = true) {
if (model == null) throw new ArgumentNullException(nameof(model));
if (!withParams) return;
if (string.IsNullOrEmpty(model.ComponentName))
throw new ArgumentNullException("Нет названия компонента", nameof(model.ComponentName));
if (model.Price <= 0)
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Price));
_logger.LogInformation("Component. ProductName:{ProductName}. Price:{Price}. ID:{ID}",
model.ComponentName, model.Price, model.ID);
var element = _componentStorage.GetElement(new FoodSearchModel { ComponentName = model.ComponentName });
if (element != null && element.ID != model.ID)
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}