PIbd-21_Medvedkov_Coursach/ComputerShopBusinessLogic/BusinessLogics/Batch.cs
2024-05-01 04:42:57 +04:00

100 lines
2.2 KiB
C#

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class Batch : IBatchLogic
{
private readonly ILogger _logger;
private readonly IBatchStorage _batchStorage;
public Batch(ILogger<Batch> logger, IBatchStorage batchStorage)
{
_logger = logger;
_batchStorage = batchStorage;
}
public List<BatchViewModel> ReadList(BatchSearchModel? model)
{
var list = (model == null) ? _batchStorage.GetFullList() : _batchStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("Read null list");
return null;
}
return list;
}
public BatchViewModel? ReadElement(BatchSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var batch = _batchStorage.GetElement(model);
if (batch == null)
{
_logger.LogWarning("ReadElement batch not found");
return null;
}
return batch;
}
public bool Create(BatchBindingModel model)
{
CheckModel(model);
if (_batchStorage.Insert(model) == null)
{
_logger.LogWarning("Insert failed");
return false;
}
return true;
}
public bool Update(BatchBindingModel model)
{
CheckModel(model);
if (_batchStorage.Update(model) == null)
{
_logger.LogWarning("Update failed");
return false;
}
return true;
}
public bool Delete(BatchBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_batchStorage.Delete(model) == null)
{
_logger.LogWarning("Delete failed");
return false;
}
return true;
}
private void CheckModel(BatchBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
}
}
}