PIbd-22_Chernyshev_Shabunov.../ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs

125 lines
3.4 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.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class RequestLogic : IRequestLogic
{
private readonly ILogger _logger;
private readonly IRequestStorage _requestStorage;
public RequestLogic(ILogger<RequestLogic> logger, IRequestStorage requestStorage)
{
_logger = logger;
_requestStorage = requestStorage;
}
public List<RequestViewModel>? ReadList(RequestSearchModel? model)
{
//model.UserId = -1 для swagger, чтобы можно было считать все заявки всех пользователей
var list = (model == null || model.UserId == -1) ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList requests return null list");
return null;
}
_logger.LogInformation("ReadList requests.Count:{Count}", list.Count);
return list;
}
public RequestViewModel? ReadElement(RequestSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _requestStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement request not found");
return null;
}
_logger.LogInformation("ReadElement reguest found Id:{Id}", element.Id);
return element;
}
public bool Create(RequestBindingModel model)
{
CheckModel(model);
if (_requestStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(RequestBindingModel model)
{
CheckModel(model);
if (_requestStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
//!!!мб УДАЛИТЬ
//public bool AddOrder(int requestId, int orderId)
//{
// return _requestStorage.ConnectRequestAssembly(requestId, assemblyId);
//}
//!!!ПРОВЕРИТЬ
public bool ConnectRequestAssembly(int requestId, int assemblyId)
{
_logger.LogInformation("Connect Assembly {rId} with request {aId}", requestId, assemblyId);
return _requestStorage.ConnectRequestAssembly(requestId, assemblyId);
}
public bool Delete(RequestBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_requestStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
//!!!мб проверять, что есть userID
private void CheckModel(RequestBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Нет ФИО клиента, оформившего заявку", nameof(model.ClientFIO));
}
if (model.DateRequest > DateTime.Now)
{
throw new ArgumentException($"Дата оформления заявки {model.DateRequest} не может быть в будущем");
}
}
}
}