CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs

74 lines
2.8 KiB
C#
Raw Normal View History

2024-04-28 23:09:52 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using Microsoft.Extensions.Logging;
2024-05-29 18:32:24 +04:00
2024-04-28 23:09:52 +04:00
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
2024-04-29 01:28:41 +04:00
private readonly IOrderStorage _storage;
2024-04-28 23:09:52 +04:00
2024-04-29 01:28:41 +04:00
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage storage) {
2024-04-28 23:09:52 +04:00
_logger = logger;
2024-04-29 01:28:41 +04:00
_storage = storage;
2024-04-28 23:09:52 +04:00
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
2024-04-29 01:28:41 +04:00
if (_storage.Insert(model) == null) {
2024-04-28 23:09:52 +04:00
_logger.LogInformation("Insert operation failed");
return false;
}
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation($"ReadList:ID:{model?.ID}");
2024-04-29 01:28:41 +04:00
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
2024-04-28 23:09:52 +04:00
if (list == null) {
_logger.LogWarning("ReadList return null list");
return null;
}
2024-04-29 01:28:41 +04:00
_logger.LogInformation($"ReadList.Count:{list.Count}");
return list;
2024-04-28 23:09:52 +04:00
}
private void CheckModel(OrderBindingModel model, bool withParams = true) {
if (!withParams) {
return;
}
if (string.IsNullOrEmpty((model.ID).ToString())) {
throw new ArgumentNullException("Нет ID заказа", nameof(model.ID));
}
2024-05-29 21:23:34 +04:00
if (model.Sum <= 0) {////byda
2024-04-28 23:09:52 +04:00
throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum));
}
2024-05-26 17:47:24 +04:00
if (model.ClientID < 0) {
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientID));
}
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}.ClientID:{model.ClientID}");
2024-04-28 23:09:52 +04:00
}
2024-05-19 18:43:00 +04:00
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null) throw new ArgumentNullException(nameof(model));
_logger.LogInformation($"ReadElement. ID:{model.ID}");
2024-05-29 21:23:34 +04:00
var element = _storage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement. elementn not found");
return null;
}
_logger.LogInformation($"ReadElement find. ID:{element.ID}");
return element;
2024-05-19 18:43:00 +04:00
}
2024-05-29 18:32:24 +04:00
}
2024-04-28 23:09:52 +04:00
}