Coursework_ComputerStore_Li.../ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs

137 lines
4.5 KiB
C#
Raw Normal View History

using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Unknown)
{
_logger.LogWarning("Insert operation failed. Order status incorrect.");
return false;
}
model.Status = OrderStatus.Accepted;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Unknown;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool StatusUpdate(OrderBindingModel rawModel, OrderStatus newStatus)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel
{
ID = rawModel.ID
});
if (viewModel == null)
{
_logger.LogWarning("Order model not found");
return false;
}
OrderBindingModel model = new OrderBindingModel
{
ID = viewModel.ID,
Status = viewModel.Status,
DateCreate = viewModel.DateCreate,
DateImplement = viewModel.DateImplement,
2023-04-07 20:46:13 +04:00
Price = viewModel.Price,
SellerID = viewModel.SellerID,
OrderConsignments = viewModel.OrderConsignments,
OrderRequests = viewModel.OrderRequests
};
CheckModel(model, false);
if (model.Status + 1 != newStatus)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
return false;
}
model.Status = newStatus;
if (model.Status == OrderStatus.Given) model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool TakeOrderInProcess(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.InProcess);
}
public bool TakeOrderReady(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Ready);
}
public bool TakeOrderGiven(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Given);
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("Order. OrderID:{Id}", model?.ID);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Incorrect Price", nameof(model.Price));
}
if (string.IsNullOrEmpty(model.SellerID.ToString()) && model.SellerID < 0)
{
throw new ArgumentNullException("Incorrect seller ID", nameof(model.SellerID));
}
2023-04-07 20:46:13 +04:00
_logger.LogInformation("Order. OrderID:{ ID}. SellerID: { SellerID}", model.ID, model.SellerID);
}
}
}