CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs

100 lines
3.5 KiB
C#
Raw Normal View History

2024-05-30 23:49:53 +04:00
using DocumentFormat.OpenXml.Drawing.Charts;
using ElectronicsShopContracts.BindingModels;
2024-04-28 23:09:52 +04:00
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
2024-05-30 23:49:53 +04:00
using ElectronicsShopDataModels.Models;
2024-04-28 23:09:52 +04:00
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;
}
2024-05-30 23:49:53 +04:00
2024-05-31 16:05:43 +04:00
public bool Update(OrderBindingModel model) {
CheckModel(model);
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2024-06-01 00:42:46 +04:00
public bool Delete(OrderBindingModel model) {
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool DeleteProduct(OrderBindingModel model) {
if (_storage.DeleteProduct(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2024-05-30 23:49:53 +04:00
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
2024-04-28 23:09:52 +04:00
{
_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-30 23:49:53 +04:00
if (string.IsNullOrEmpty(model.ClientID.ToString())) {
2024-05-26 17:47:24 +04:00
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
}