CourseWork_CompShop/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/SupplyLogic.cs

146 lines
4.7 KiB
C#
Raw Normal View History

2023-05-17 14:20:02 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
2023-05-20 00:08:47 +04:00
using ComputerShopContracts.StorageContracts;
2023-05-17 14:20:02 +04:00
using ComputerShopContracts.ViewModels;
2023-05-20 00:08:47 +04:00
using ComputerShopDataModels.Enums;
using Microsoft.Extensions.Logging;
2023-05-17 14:20:02 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class SupplyLogic : ISupplyLogic
{
2023-05-20 00:08:47 +04:00
private readonly ILogger _logger;
private readonly ISupplyStorage _supplyStorage;
2023-05-20 08:34:33 +04:00
private readonly IOrderStorage _orderStorage;
2023-12-02 01:30:33 +04:00
public SupplyLogic(ILogger<SupplyLogic> logger, ISupplyStorage supplyStorage, IOrderStorage orderStorage)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
_logger = logger;
_supplyStorage = supplyStorage;
2023-05-20 08:34:33 +04:00
_orderStorage = orderStorage;
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool Create(SupplyBindingModel model)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
CheckModel(model);
if (_supplyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
2023-05-17 14:20:02 +04:00
}
public bool Delete(SupplyBindingModel model)
{
2023-05-20 00:08:47 +04:00
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_supplyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public SupplyViewModel? ReadElement(SupplySearchModel model)
2023-05-17 14:20:02 +04:00
{
throw new NotImplementedException();
}
2023-05-20 00:08:47 +04:00
public List<SupplyViewModel>? ReadList(SupplySearchModel? model)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
_logger.LogInformation("ReadList. Supplyid:{ Id}", model?.Id);
var list = model == null ? _supplyStorage.GetFullList() : _supplyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool TakeInWork(SupplyBindingModel model)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
return StatusUpdate(model, SupplyStatus.Отправляется);
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool Finish(SupplyBindingModel model)
2023-05-17 14:20:02 +04:00
{
2023-12-01 19:59:06 +04:00
return StatusUpdate(model, SupplyStatus.Поставлено);
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool Update(SupplyBindingModel model)
2023-05-17 14:20:02 +04:00
{
throw new NotImplementedException();
}
2023-05-20 00:08:47 +04:00
private void CheckModel(SupplyBindingModel model, bool withParams = true)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
}
public bool StatusUpdate(SupplyBindingModel model, SupplyStatus _newStatus)
{
var viewModel = _supplyStorage.GetElement(new SupplySearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != _newStatus)
{
_logger.LogWarning("Status update to " + _newStatus.ToString() + " operation failed. Order status incorrect.");
return false;
}
model.Status = _newStatus;
2023-12-01 19:59:06 +04:00
if (model.Status == SupplyStatus.Поставлено) model.DateImplement = DateTime.Now;
2023-05-20 00:08:47 +04:00
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_supplyStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
2023-05-17 14:20:02 +04:00
}
2023-05-20 08:34:33 +04:00
public bool AddOrder(SupplySearchModel supplymodel, OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var order = _orderStorage.GetElement(model);
var supply = _supplyStorage.GetElement(supplymodel);
if (order == null || supply == null)
{
return false;
}
2023-12-01 19:59:06 +04:00
supply.SupplyOrders[supply.Id] = order;
_supplyStorage.Update(new()
{
Id = supply.Id,
SupplyOrders = supply.SupplyOrders,
});
return true;
2023-12-02 01:30:33 +04:00
}
2023-05-17 14:20:02 +04:00
}
}