207 lines
6.4 KiB
C#
Raw Normal View History

2024-02-27 21:03:00 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TypographyContracts.BindingModels;
using TypographyContracts.BusinessLogicsContracts;
using TypographyContracts.SearchModels;
using TypographyContracts.StoragesContracts;
using TypographyContracts.ViewModels;
using TypographyDataModels.Models;
using Microsoft.Extensions.Logging;
namespace TypographyBusinessLogic
{
public class ShopLogic : IShopLogic
{
private readonly ILogger _logger;
private readonly IShopStorage _shopStorage;
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage shopStorage)
{
_logger = logger;
_shopStorage = shopStorage;
}
2024-04-21 16:26:49 +04:00
public ShopViewModel? ReadElement(ShopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Shop Name:{0}, ID:{1}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement. element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2024-02-27 21:03:00 +04:00
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
2024-04-21 16:26:49 +04:00
_logger.LogInformation("ReadList. Shop Name:{0}, ID:{1} ", model?.ShopName, model?.Id);
var list = (model == null) ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model);
2024-02-27 21:03:00 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
2024-04-21 16:26:49 +04:00
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
2024-02-27 21:03:00 +04:00
return list;
}
2024-04-21 16:26:49 +04:00
public bool MakeShipment(ShopSearchModel model, IPrintedModel printed, int count)
2024-02-27 21:03:00 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2024-04-21 16:26:49 +04:00
if (printed == null)
2024-02-27 21:03:00 +04:00
{
2024-04-21 16:26:49 +04:00
throw new ArgumentNullException(nameof(printed));
2024-02-27 21:03:00 +04:00
}
2024-04-21 16:26:49 +04:00
if (count <= 0)
{
throw new ArgumentNullException("Count of printeds in supply must be more than 0", nameof(count));
}
var shopElement = _shopStorage.GetElement(model);
if (shopElement == null)
{
_logger.LogWarning("Required shop element not found in storage");
return false;
}
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.ShopName);
int countPrinteds = 0;
foreach (var c in shopElement.ShopPrinteds)
countPrinteds += c.Value.Item2;
if (count > shopElement.MaxCountPrinteds - countPrinteds)
{
_logger.LogWarning("Required shop will be overflowed");
return false;
}
else
{
if (shopElement.ShopPrinteds.TryGetValue(printed.Id, out var samePrinted))
{
shopElement.ShopPrinteds[printed.Id] = (printed, samePrinted.Item2 + count);
_logger.LogInformation("Same printed found by supply. Added {0} of {1} in {2} shop", count, printed.PrintedName, shopElement.ShopName);
}
else
{
shopElement.ShopPrinteds[printed.Id] = (printed, count);
_logger.LogInformation("New printed added by supply. Added {0} of {1} in {2} shop", count, printed.PrintedName, shopElement.ShopName);
}
_shopStorage.Update(new()
{
Id = shopElement.Id,
ShopName = shopElement.ShopName,
Address = shopElement.Address,
MaxCountPrinteds = shopElement.MaxCountPrinteds,
DateOpening = shopElement.DateOpening,
ShopPrinteds = shopElement.ShopPrinteds
});
}
return true;
2024-02-27 21:03:00 +04:00
}
public bool Create(ShopBindingModel model)
{
CheckModel(model);
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
return true;
}
public bool Update(ShopBindingModel model)
{
CheckModel(model);
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
if (_shopStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
return true;
}
public bool Delete(ShopBindingModel model)
{
CheckModel(model, false);
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
if (_shopStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
if (!withParams)
{
return;
}
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
if (string.IsNullOrEmpty(model.ShopName))
{
2024-04-21 16:26:49 +04:00
throw new ArgumentNullException("Нет названия магазина!", nameof(model.ShopName));
2024-02-27 21:03:00 +04:00
}
2024-04-21 16:26:49 +04:00
if (model.MaxCountPrinteds < 0)
2024-02-27 21:03:00 +04:00
{
2024-04-21 16:26:49 +04:00
throw new InvalidOperationException("Отрицательное максимальное количество авто!");
2024-02-27 21:03:00 +04:00
}
2024-04-21 16:26:49 +04:00
_logger.LogInformation("Shop. Name: {0}, Address: {1}, ID: {2}", model.ShopName, model.Address, model.Id);
2024-02-27 21:03:00 +04:00
var element = _shopStorage.GetElement(new ShopSearchModel
{
ShopName = model.ShopName
});
2024-04-21 16:26:49 +04:00
2024-02-27 21:03:00 +04:00
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
2024-04-21 16:26:49 +04:00
public bool SellPrinted(IPrintedModel printed, int count)
{
return _shopStorage.SellPrinted(printed, count);
}
2024-02-27 21:03:00 +04:00
}
}