CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs

120 lines
4.3 KiB
C#
Raw Normal View History

2024-04-28 23:09:52 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
//private readonly IOrderStorage _storage;
// todo нет интерфейса хранилища
public OrderLogic(ILogger<OrderLogic> logger) {
_logger = logger;
//storage = _storage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
model.Status = OrderStatus.Принят;
// todo _orderStorage.Insert(model) == null
if (model == null) {
_logger.LogInformation("Insert operation failed");
return false;
}
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выдан);
}
public bool FinishOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Готов);
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется);
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation($"ReadList:ID:{model?.ID}");
// todo model == null? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
var list = model;
if (list == null) {
_logger.LogWarning("ReadList return null list");
return null;
}
// todo "$ReadList.Count:{list.count}"
_logger.LogInformation("ReadList.Count:{Count}");
//todo return list
return null;
}
private void CheckModel(OrderBindingModel model, bool withParams = true) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
if (!withParams) {
return;
}
if (string.IsNullOrEmpty((model.ID).ToString())) {
throw new ArgumentNullException("Нет ID заказа", nameof(model.ID));
}
if (model.Sum <= 0) {
throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.Status:{model.Status}.PaymeantOption:{model.PaymeantOption}." +
$"DateCreate:{model.DateCreate}.DataImplement:{model.DateImplemet}");
}
private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
{
CheckModel(model, false);
//todo
var viewModel = model; // model == _orderStorage.GetElement(new OrderSearchModel { ID = model.ID });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newOrderStatus)
{
_logger.LogWarning("Status update to " + newOrderStatus.ToString() + " operation failed.");
return false;
}
model.Status = newOrderStatus;
if (model.Status == OrderStatus.Готов)
{
//todo подключить бд
//model.DateImplement = DateTime.Now;
}
else
{
// todo подключить бд
//model.DateImplement = viewModel.DateImplement;
}
// todo
// _orderStorage.Update(model) == null
if (model == null)
{
_logger.LogWarning("Update operarion failed");
return false;
}
return true;
}
}
}