CourseWork_CompShop/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/SupplyLogic.cs
2023-12-02 01:30:33 +04:00

146 lines
4.7 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 ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class SupplyLogic : ISupplyLogic
{
private readonly ILogger _logger;
private readonly ISupplyStorage _supplyStorage;
private readonly IOrderStorage _orderStorage;
public SupplyLogic(ILogger<SupplyLogic> logger, ISupplyStorage supplyStorage, IOrderStorage orderStorage)
{
_logger = logger;
_supplyStorage = supplyStorage;
_orderStorage = orderStorage;
}
public bool Create(SupplyBindingModel model)
{
CheckModel(model);
if (_supplyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(SupplyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_supplyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SupplyViewModel? ReadElement(SupplySearchModel model)
{
throw new NotImplementedException();
}
public List<SupplyViewModel>? ReadList(SupplySearchModel? model)
{
_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;
}
public bool TakeInWork(SupplyBindingModel model)
{
return StatusUpdate(model, SupplyStatus.Отправляется);
}
public bool Finish(SupplyBindingModel model)
{
return StatusUpdate(model, SupplyStatus.Поставлено);
}
public bool Update(SupplyBindingModel model)
{
throw new NotImplementedException();
}
private void CheckModel(SupplyBindingModel model, bool withParams = true)
{
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;
if (model.Status == SupplyStatus.Поставлено) model.DateImplement = DateTime.Now;
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_supplyStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
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;
}
supply.SupplyOrders[supply.Id] = order;
_supplyStorage.Update(new()
{
Id = supply.Id,
SupplyOrders = supply.SupplyOrders,
});
return true;
}
}
}