ещё один баг

This commit is contained in:
Макс Бондаренко 2023-02-13 22:39:28 +04:00
parent 6d0b5f9438
commit 8a05205ce1
3 changed files with 77 additions and 59 deletions

View File

@ -17,17 +17,73 @@ namespace ComputersShopBusinessLogic
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
} }
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Insert operation failed. Order status incorrect.");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newStatus)
{
_logger.LogWarning("Update operation failed. Order status incorrect.");
return false;
}
model.Status = newStatus;
if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now;
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model);
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update 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) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
_logger.LogInformation("ReadList. OrderId:{ Id}", model?.Id); _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); var list = model == null ? _orderStorage.GetFullList() :
_orderStorage.GetFilteredList(model);
if (list == null) if (list == null)
{ {
_logger.LogWarning("ReadList return null list"); _logger.LogWarning("ReadList return null list");
@ -36,40 +92,6 @@ namespace ComputersShopBusinessLogic
_logger.LogInformation("ReadList. Count:{Count}", list.Count); _logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list; return list;
} }
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if(!CheckStatus(model, OrderStatus.Принят, false)) return false;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Выполняется)) return false;
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Готов)) return false;
return true;
}
public bool FinishOrder(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Выдан)) return false;
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true) private void CheckModel(OrderBindingModel model, bool withParams = true)
{ {
if (model == null) if (model == null)
@ -82,35 +104,21 @@ namespace ComputersShopBusinessLogic
} }
if (model.ComputerId < 0) if (model.ComputerId < 0)
{ {
throw new ArgumentNullException("Некорректный идентификатор компьютера", nameof(model.ComputerId)); throw new ArgumentNullException("Некорректный идентификатор документа",
nameof(model.ComputerId));
} }
if (model.Count <= 0) if (model.Count <= 0)
{ {
throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0", nameof(model.Count)); throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0",
nameof(model.Count));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); throw new ArgumentNullException("Сумма заказа должна быть больше 0",
nameof(model.Sum));
} }
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. ComputerId: { ComputerId}", model.Id, model.Sum, model.ComputerId); _logger.LogInformation("Computer. OrderID:{Id}. Sum:{Sum}. ComputerID:{ComputerID}.}",
} model.Id, model.Sum, model.ComputerId);
private bool CheckStatus(OrderBindingModel model, OrderStatus newstatus, bool update = true)
{
if (model.Status != newstatus - 1)
{
_logger.LogWarning("Failed to change status");
return false;
}
model.Status = newstatus;
if(!update) return true;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
return true;
} }
} }
} }

View File

@ -67,6 +67,7 @@
// //
this.textBoxPrice.Location = new System.Drawing.Point(88, 38); this.textBoxPrice.Location = new System.Drawing.Point(88, 38);
this.textBoxPrice.Name = "textBoxPrice"; this.textBoxPrice.Name = "textBoxPrice";
this.textBoxPrice.ReadOnly = true;
this.textBoxPrice.Size = new System.Drawing.Size(159, 23); this.textBoxPrice.Size = new System.Drawing.Size(159, 23);
this.textBoxPrice.TabIndex = 3; this.textBoxPrice.TabIndex = 3;
// //

View File

@ -66,4 +66,13 @@
<metadata name="CountColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="CountColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="ColumnId.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ComponentColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="CountColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root> </root>