PIbd-23_Borschevskaya_A.A._.../FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs
2023-02-13 09:02:44 +04:00

164 lines
5.4 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 FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyBusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus)
{
if (model.Status + 1 != orderStatus)
{
return false;
}
model.Status = orderStatus;
return true;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (!ChangeStatus(model, OrderStatus.Принят))
{
_logger.LogWarning("Order's status is wrong");
return false;
}
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
model = Find(model);
if (model == null)
{
return false;
}
if (!ChangeStatus(model, OrderStatus.Выполняется))
{
_logger.LogWarning("Order's status is wrong");
return false;
}
_orderStorage.Update(model);
return true;
}
public bool FinishOrder(OrderBindingModel model)
{
model = Find(model);
if (model == null) {
return false;
}
if (!ChangeStatus(model, OrderStatus.Готов))
{
_logger.LogWarning("Order's status is wrong");
return false;
}
_orderStorage.Update(model);
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
{
model = Find(model);
if (model == null)
{
return false;
}
if (!ChangeStatus(model, OrderStatus.Выдан))
{
_logger.LogWarning("Order's status is wrong");
return false;
}
model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ 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.FurnitureId <= 0)
{
throw new ArgumentNullException("Неверный идентификатор изделия", nameof(model.FurnitureId));
}
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}. OrderStatus: {OrderStatus} DateCreate: {DateCreate} " +
"FurnitureId:{FurnitureId}. FurnitureName:{FurnitureName}. Count:{ Count}. Sum:{ Sum}. ",
model.Id, model.Status, model.DateCreate, model.FurnitureId, model.FurnitureName, model.Count, model.Sum);
}
private OrderBindingModel Find(OrderBindingModel model)
{
var modelView = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (modelView == null)
{
return null;
}
model.FurnitureId = modelView.FurnitureId;
model.DateCreate = modelView.DateCreate;
model.Count = modelView.Count;
model.Sum = modelView.Sum;
model.FurnitureId = model.FurnitureId;
model.FurnitureName = modelView.FurnitureName;
model.Status = modelView.Status;
return model;
}
}
}