115 lines
4.1 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 ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.BusinessLogics
{
public class SweetsLogic : ISweetsLogic
{
private readonly ILogger _logger;
private readonly ISweetsStorage _sweetsStorage;
public SweetsLogic(ILogger<SweetsLogic> logger, ISweetsStorage sweetsStorage)
{
_logger = logger;
_sweetsStorage = sweetsStorage;
}
public List<SweetsViewModel>? ReadList(SweetsSearchModel? model)
{
_logger.LogInformation("ReadList. SushiName: {SushiName}. Id: {Id}", model?.SweetsName, model?.Id);
var list = model == null ? _sweetsStorage.GetFullList() :
_sweetsStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public SweetsViewModel? ReadElement(SweetsSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. SushiName: {SushiName}. Id: {Id}", model.SweetsName, model.Id);
var element = _sweetsStorage.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(SweetsBindingModel model)
{
CheckModel(model);
if (_sweetsStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(SweetsBindingModel model)
{
CheckModel(model);
if (_sweetsStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(SweetsBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_sweetsStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(SweetsBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.SweetsName))
{
throw new ArgumentNullException("Нет названия сладости", nameof(model.SweetsName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена сладости должна быть больше 0",
nameof(model.Price));
}
_logger.LogInformation("Sweets. SweetsName: {SweetsName}. Cost: {Cost}. Id: {Id}", model.SweetsName, model.Price, model.Id);
var element = _sweetsStorage.GetElement(new SweetsSearchModel
{
SweetsName = model.SweetsName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Сладости с таким названием уже есть");
}
}
}
}