SUBD_Gerimovich_Ilya_PIbd-22/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BussinessLogic/AdLogic.cs

158 lines
5.0 KiB
C#
Raw Normal View History

2024-05-24 12:23:50 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
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.BussinessLogic
{
2024-05-24 13:05:47 +04:00
public class AdLogic : IAdLogic
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
private readonly IAdStorage _orderStorage;
2024-05-24 12:23:50 +04:00
private readonly ILogger _logger;
2024-05-24 13:05:47 +04:00
public AdLogic(ILogger<AdLogic> logger, IAdStorage orderStorage)
2024-05-24 12:23:50 +04:00
{
_logger = logger;
_orderStorage = orderStorage;
}
2024-05-24 13:05:47 +04:00
public List<AdViewModel>? ReadList(AdSearchModel? model)
2024-05-24 12:23:50 +04:00
{
_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;
}
2024-05-24 13:05:47 +04:00
public bool CreateOrder(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
CheckModel(model);
2024-05-24 13:05:47 +04:00
if(model.Status != AdStatus.Неизвестен)
2024-05-24 12:23:50 +04:00
{
_logger.LogWarning("Insert operation failed, incorrect order status");
return false;
}
2024-05-24 13:05:47 +04:00
model.Status = AdStatus.Принят;
2024-05-24 12:23:50 +04:00
if(_orderStorage.Insert(model) == null)
{
2024-05-24 13:05:47 +04:00
model.Status = AdStatus.Неизвестен;
2024-05-24 12:23:50 +04:00
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2024-05-24 13:05:47 +04:00
public bool TakeOrderInWork(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
return StatusUpdate(model, AdStatus.Выполняется);
2024-05-24 12:23:50 +04:00
}
2024-05-24 13:05:47 +04:00
public bool FinishOrder(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
return StatusUpdate(model, AdStatus.Готов);
2024-05-24 12:23:50 +04:00
}
2024-05-24 13:05:47 +04:00
public bool DeliveryOrder(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
return StatusUpdate(model, AdStatus.Выдан);
2024-05-24 12:23:50 +04:00
}
2024-05-24 13:05:47 +04:00
private void CheckModel(AdBindingModel model, bool withParams = true)
2024-05-24 12:23:50 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
// Проверка на наличие товаров в заказе
if(model.Count <= 0)
{
throw new ArgumentNullException("В заказе не может быть 0 изделий", nameof(model.Count));
}
// Проверка на наличие нормальной суммарной стоимости чека
if(model.Sum <= 0)
{
throw new ArgumentNullException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum));
}
// Проверка корректности id у изделий
if(model.FurnitureId < 0)
{
throw new ArgumentNullException("Некорректный id у изделия", nameof(model.FurnitureId));
}
// Проверка корректности дат
if(model.DateCreate > model.DateImplement)
{
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
}
_logger.LogInformation("Order. OrderId:{Id}, Sum:{Sum}. FurnitureId:{Id}", model.Id, model.Sum, model.FurnitureId);
}
2024-05-24 13:05:47 +04:00
public bool StatusUpdate(AdBindingModel model, AdStatus newOrderStatus)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
var viewModel = _orderStorage.GetElement(new AdSearchModel { Id = model.Id });
2024-05-24 12:23:50 +04:00
if(viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if(viewModel.Status + 1 != newOrderStatus)
{
_logger.LogWarning("Status update operation failed. Status " + newOrderStatus.ToString() + "incorrect");
return false;
}
model.Status = newOrderStatus;
2024-05-24 13:05:47 +04:00
if(model.Status == AdStatus.Выдан)
2024-05-24 12:23:50 +04:00
{
model.DateImplement = DateTime.Now;
}
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if(_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
}
}