PIbd-22_Chernyshev_G.J._30_.../GarmentFactoryBusinessLogic/BusinessLogics/TextileLogic.cs

127 lines
4.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 GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.BusinessLogicsContracts;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.StoragesContracts;
using GarmentFactoryContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GarmentFactoryBusinessLogic.BusinessLogics
{
public class TextileLogic : ITextileLogic
{
private readonly ILogger _logger;
//Хранение всех изделий
private readonly ITextileStorage _textileStorage;
public TextileLogic(ILogger<TextileLogic> logger, ITextileStorage textileStorage)
{
_logger = logger;
_textileStorage = textileStorage;
}
//Чтение всего списка изделий
public List<TextileViewModel>? ReadList(TextileSearchModel? model)
{
_logger.LogInformation("ReadList.TextileName:{TextileName} .Id:{Id}", model?.TextileName, model?.Id);
var list = model == null ? _textileStorage.GetFullList() : _textileStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList.Count:{Count}", list.Count);
return list;
}
//Чтение одного изделия
public TextileViewModel? ReadElement(TextileSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement.TextileName:{TextileName} .Id:{ Id}", model.TextileName, model.Id);
var element = _textileStorage.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(TextileBindingModel model)
{
CheckModel(model);
if (_textileStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
//Обновление данных изделия
public bool Update(TextileBindingModel model)
{
CheckModel(model);
if (_textileStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
//Удаление изделия
public bool Delete(TextileBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete.Id:{Id}", model.Id);
if (_textileStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
//Проверка данных текстиля при добавлении/удалении/обновлении
private void CheckModel(TextileBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.TextileName))
{
throw new ArgumentNullException("Нет названия текстиля", nameof(model.TextileName));
}
if (model.Price <= 0)
{
throw new ArgumentException("Цена текстиля должна быть больше 0", nameof(model.Price));
}
if (model.TextileComponents == null || model.TextileComponents.Count == 0)
{
throw new ArgumentNullException("Список кмопонентов не может быть пустым", nameof(model.TextileComponents));
}
_logger.LogInformation("Textile. TextileName:{TextileName} .Price:{Price} .Id:{Id}", model.TextileName, model.Price, model.Id);
var element = _textileStorage.GetElement(new TextileSearchModel { TextileName = model.TextileName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Текстиль с таким названием уже есть");
}
}
}
}