124 lines
5.0 KiB
C#
124 lines
5.0 KiB
C#
using DinerContracts.BindingModels;
|
||
using DinerContracts.BusinessLogicsContacts;
|
||
using DinerContracts.SearchModels;
|
||
using DinerContracts.StoragesContracts;
|
||
using DinerContracts.ViewModels;
|
||
using DinerDataModels.Enums;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace DineryBusinessLogic.BusinessLogic
|
||
{
|
||
public class OrderLogic : IOrderLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IOrderStorage _orderStorage;
|
||
|
||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) {
|
||
_logger = logger;
|
||
_orderStorage = orderStorage;
|
||
}
|
||
|
||
public bool CreateOrder(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
model.Status = OrderStatus.Принят;
|
||
if (_orderStorage.Insert(model) == null) {
|
||
model.Status = OrderStatus.Неизвестен;
|
||
_logger.LogWarning("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 OrderViewModel? ReadElement(OrderSearchModel model) {
|
||
if (model == null) {
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation($"ReadElement. ImplementerLogin:{model.ImplementerID}.ID:{model.ID}");
|
||
var element = _orderStorage.GetElement(model);
|
||
if (element == null) {
|
||
_logger.LogWarning("ReadElement. element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadElement. findID:{element.ID}");
|
||
return element;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public bool TakeOrderInWork(OrderBindingModel model)
|
||
{
|
||
return StatusUpdate(model, OrderStatus.Выполняется);
|
||
}
|
||
|
||
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));
|
||
if (model.ClientID < 0) {
|
||
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientID));
|
||
}
|
||
if (model.ImplementerID < 0) {
|
||
throw new ArgumentNullException("Некорректный идентификатор у исполнителя", nameof(model.ImplementerID));
|
||
}
|
||
_logger.LogInformation("Order. SnackID:{SnackID}. Count:{Count}. Sum:{Sum}. Status:{Status}. " +
|
||
"DateCreate:{DateCreate}. DateImplement:{DateImplement}. ID:{ID}. ClientId: {ClientId}. " +
|
||
"ImplementerID: {ImplementerId}",
|
||
model.SnackID, model.Count, model.Sum, model.Status, model.DateCreate, model.DateImplement, model.ID,
|
||
model.ClientID, model.ImplementerID);
|
||
}
|
||
private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus) {
|
||
CheckModel(model, false);
|
||
var viewModel = _orderStorage.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 (_orderStorage.Update(model) == null) {
|
||
_logger.LogWarning("Update operarion failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
}
|