118 lines
4.2 KiB
C#
118 lines
4.2 KiB
C#
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 PurchaseLogic : IPurchaseLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IPurchaseStorage _purchaseStorage;
|
||
|
||
public PurchaseLogic(ILogger<PurchaseLogic> logger, IPurchaseStorage purchaseStorage)
|
||
{
|
||
_logger = logger;
|
||
_purchaseStorage = purchaseStorage;
|
||
}
|
||
|
||
public bool CreatePurchase(PurchaseBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (model.Status != PurchaseStatus.Неизвестен)
|
||
{
|
||
_logger.LogWarning("Insert operation failed. Purchase status incorrect.");
|
||
return false;
|
||
}
|
||
model.Status = PurchaseStatus.Принят;
|
||
if (_purchaseStorage.Insert(model) == null)
|
||
{
|
||
model.Status = PurchaseStatus.Неизвестен;
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool StatusUpdate(PurchaseBindingModel model, PurchaseStatus newStatus)
|
||
{
|
||
CheckModel(model);
|
||
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 == PurchaseStatus.Выдан) model.DateImplement = DateTime.Now;
|
||
if (_purchaseStorage.Update(model) == null)
|
||
{
|
||
model.Status--;
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool TakePurchaseInWork(PurchaseBindingModel model)
|
||
{
|
||
return StatusUpdate(model, PurchaseStatus.Выполняется);
|
||
}
|
||
|
||
public bool DeliveryPurchase(PurchaseBindingModel model)
|
||
{
|
||
return StatusUpdate(model, PurchaseStatus.Готов);
|
||
}
|
||
|
||
public bool FinishPurchase(PurchaseBindingModel model)
|
||
{
|
||
return StatusUpdate(model, PurchaseStatus.Выдан);
|
||
}
|
||
|
||
public List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model)
|
||
{
|
||
_logger.LogInformation("Order. OrderID:{Id}", model?.Id);
|
||
var list = model == null ? _purchaseStorage.GetFullList() : _purchaseStorage.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(PurchaseBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (model.ComponentId < 0)
|
||
{
|
||
throw new ArgumentNullException("Некорректный идентификатор компонента", nameof(model.ComponentId));
|
||
}
|
||
if (model.Count <= 0)
|
||
{
|
||
throw new ArgumentNullException("Количество компонентов в заказе должно быть больше 0", nameof(model.Count));
|
||
}
|
||
if (model.Sum <= 0)
|
||
{
|
||
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
|
||
}
|
||
_logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. ComponentId: { ComponentId}", model.Id, model.Sum, model.ComponentId);
|
||
}
|
||
}
|
||
}
|