PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/OrderLogic.cs

145 lines
5.3 KiB
C#
Raw Normal View History

2023-04-09 17:55:42 +04:00
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
2023-04-09 18:08:04 +04:00
using ConstructionCompanyDataModels.Enums;
2023-04-09 17:55:42 +04:00
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Adress:{Adress}. 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 OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
2023-04-09 18:08:04 +04:00
if (model.Status != OrderStatus.Неизвестен) return false;
model.Status = OrderStatus.Принят;
2023-04-09 17:55:42 +04:00
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2023-04-09 18:08:04 +04:00
public bool FinishOrder(OrderBindingModel model)
2023-04-09 17:55:42 +04:00
{
2023-04-09 18:08:04 +04:00
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel
2023-04-09 17:55:42 +04:00
{
2023-04-09 18:08:04 +04:00
Id = model.Id
});
if (element == null)
{
_logger.LogWarning("Read operation failed");
2023-04-09 17:55:42 +04:00
return false;
}
2023-04-09 18:08:04 +04:00
if (element.Status != OrderStatus.Выполняется)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Заказ должен быть переведен в статус выполнения перед готовностью!");
}
model.Status = OrderStatus.Завершён;
_orderStorage.Update(model);
2023-04-09 17:55:42 +04:00
return true;
}
2023-04-09 18:08:04 +04:00
public bool TakeOrderInWork(OrderBindingModel model)
2023-04-09 17:55:42 +04:00
{
2023-04-09 18:08:04 +04:00
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel
2023-04-09 17:55:42 +04:00
{
2023-04-09 18:08:04 +04:00
Id = model.Id
});
if (element == null)
{
_logger.LogWarning("Read operation failed");
2023-04-09 17:55:42 +04:00
return false;
}
2023-04-09 18:08:04 +04:00
if (element.Status != OrderStatus.Принят)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Заказ должен быть переведен в статус принятого перед его выполнением!");
}
model.Status = OrderStatus.Выполняется;
_orderStorage.Update(model);
2023-04-09 17:55:42 +04:00
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentNullException("Нет описания заказа", nameof(model.Description));
}
if (string.IsNullOrEmpty(model.Adress))
{
throw new ArgumentNullException("Нет адреса заказа", nameof(model.Adress));
}
if (model.Price <= 0)
{
throw new InvalidOperationException("Цена должна быть больше 0!");
}
if (string.IsNullOrEmpty(model.CustomerNumber))
{
throw new ArgumentNullException("Нет телефона", nameof(model.CustomerNumber));
}
_logger.LogInformation("Order. Description:{Description}. Adress:{Adress}. Price:{Price} Id:{Id}", model.Description, model.Adress, model.Price, model.Id);
}
}
}