CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs
Илья Федотов 134f7d6cef Business logic 0.2
2024-04-29 01:28:41 +04:00

105 lines
3.9 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 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;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage storage) {
_logger = logger;
_storage = storage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
model.Status = OrderStatus.Принят;
if (_storage.Insert(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}");
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
if (list == null) {
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation($"ReadList.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 (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.DateImplement}");
}
private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
{
CheckModel(model, false);
var viewModel = _storage.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.Готов) {
model.DateImplement = DateTime.Now;
}
else {
model.DateImplement = viewModel.DateImplement;
}
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operarion failed");
return false;
}
return true;
}
}
}