2023-01-29 23:17:04 +04:00
|
|
|
|
using DressAtelierContracts.BindingModels;
|
|
|
|
|
using DressAtelierContracts.BusinessLogicContracts;
|
|
|
|
|
using DressAtelierContracts.SearchModels;
|
|
|
|
|
using DressAtelierContracts.StorageContracts;
|
|
|
|
|
using DressAtelierContracts.ViewModels;
|
|
|
|
|
using DressAtelierDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-06-01 01:58:22 +04:00
|
|
|
|
using System.Transactions;
|
2023-01-29 23:17:04 +04:00
|
|
|
|
|
|
|
|
|
namespace DressAtelierBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-06-01 01:58:22 +04:00
|
|
|
|
private readonly IAtelierLogic _atelierLogic;
|
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IAtelierLogic atelierLogic)
|
2023-01-29 23:17:04 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2023-06-01 01:58:22 +04:00
|
|
|
|
_atelierLogic = atelierLogic;
|
2023-01-29 23:17:04 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (model.Status != OrderStatus.Unknown)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = OrderStatus.Accepted;
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GivenOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-13 10:07:35 +04:00
|
|
|
|
CheckModel(model, false);
|
2023-01-29 23:17:04 +04:00
|
|
|
|
_logger.LogInformation("Update to given status. ID:{ID}", model.ID);
|
2023-02-13 10:07:35 +04:00
|
|
|
|
if (model.Status != OrderStatus.Ready)
|
2023-01-29 23:17:04 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-01 01:58:22 +04:00
|
|
|
|
|
|
|
|
|
var ateliers = _atelierLogic.ReadList(null);
|
|
|
|
|
if (ateliers == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int quantity = model.Count;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-06-03 14:45:16 +04:00
|
|
|
|
using (TransactionScope scope = new TransactionScope())
|
|
|
|
|
{
|
|
|
|
|
if(ateliers.Sum(x => x.MaxTotalOfDresses - x.DressesList.Sum(y => y.Value.Item2)) < quantity)
|
|
|
|
|
{
|
|
|
|
|
throw new OverflowException("There is not enough space in ateliers");
|
|
|
|
|
}
|
|
|
|
|
foreach (var atelier in ateliers)
|
|
|
|
|
{
|
|
|
|
|
quantity = _atelierLogic.AddDress(new AtelierBindingModel { ID = atelier.ID }, new DressBindingModel { ID = model.DressID }, quantity).quantity;
|
|
|
|
|
}
|
2023-06-01 01:58:22 +04:00
|
|
|
|
|
2023-06-03 14:45:16 +04:00
|
|
|
|
model.Status = OrderStatus.Given;
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
_orderStorage.Update(model);
|
2023-06-01 01:58:22 +04:00
|
|
|
|
}
|
2023-06-03 14:45:16 +04:00
|
|
|
|
}
|
2023-06-01 01:58:22 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new OverflowException("Shops' storages are full.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 14:45:16 +04:00
|
|
|
|
|
2023-01-29 23:17:04 +04:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ReadyOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-13 10:07:35 +04:00
|
|
|
|
CheckModel(model,false);
|
2023-01-29 23:17:04 +04:00
|
|
|
|
_logger.LogInformation("Update to ready status. ID:{ID}", model.ID);
|
2023-02-13 10:07:35 +04:00
|
|
|
|
if ( model.Status != OrderStatus.InProcess)
|
2023-01-29 23:17:04 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-01 01:58:22 +04:00
|
|
|
|
|
2023-01-29 23:17:04 +04:00
|
|
|
|
model.Status = OrderStatus.Ready;
|
2023-02-13 10:07:35 +04:00
|
|
|
|
|
|
|
|
|
_orderStorage.Update(model);
|
2023-01-29 23:17:04 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-13 10:07:35 +04:00
|
|
|
|
CheckModel(model,false);
|
|
|
|
|
_logger.LogInformation("Update to in process status. ID:{ID}", model.ID);
|
|
|
|
|
if (model.Status != OrderStatus.Accepted)
|
2023-01-29 23:17:04 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.InProcess;
|
2023-02-13 10:07:35 +04:00
|
|
|
|
|
|
|
|
|
_orderStorage.Update(model);
|
2023-01-29 23:17:04 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
2023-02-13 02:45:41 +04:00
|
|
|
|
_logger.LogInformation("ReadList. OrderID:{ID}",model?.ID);
|
2023-01-29 23:17:04 +04:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 10:07:35 +04:00
|
|
|
|
public void CheckModel(OrderBindingModel model, bool withParams = true)
|
2023-01-29 23:17:04 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2023-02-13 10:07:35 +04:00
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-01-29 23:17:04 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderID: { ID} ",model.ID);
|
|
|
|
|
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
ID = model.ID
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-03 23:59:47 +04:00
|
|
|
|
if (element != null && element.ID != model.ID)
|
2023-01-29 23:17:04 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Order with such name already exists.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|