BusinessLogics Работника готова

This commit is contained in:
Николай 2023-04-01 19:41:22 +04:00
parent 9222e0f647
commit 7903dee3e8
5 changed files with 367 additions and 3 deletions

View File

@ -0,0 +1,120 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace HardwareShopContracts.BusinessLogicsContracts
{
public class BuildLogic : IBuildLogic
{
private readonly ILogger _logger;
private readonly IBuildStorage _buildStorage;
public BuildLogic(ILogger<BuildLogic> logger, IBuildStorage buildStorage)
{
_logger = logger;
_buildStorage = buildStorage;
}
public List<BuildViewModel>? ReadList(BuildSearchModel? model)
{
_logger.LogInformation("ReadList. BuildName:{BuildName}. Id:{Id}", model?.BuildName, model?.Id);
var list = model == null ? _buildStorage.GetFullList() : _buildStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public BuildViewModel? ReadElement(BuildSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BuildName:{BuildName}. Id:{Id}", model.BuildName, model.Id);
var element = _buildStorage.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 Create(BuildBindingModel model)
{
CheckModel(model);
if (_buildStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(BuildBindingModel model)
{
CheckModel(model);
if (_buildStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(BuildBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_buildStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(BuildBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.BuildName))
{
throw new ArgumentNullException("Нет названия сборки", nameof(model.BuildName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Price));
}
if (model.UserId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.UserId));
}
if (model.Id < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у сборки", nameof(model.Id));
}
_logger.LogInformation("Build. BuildName:{BuildName}. Price:{Price}. Id:{Id}", model.BuildName, model.Price, model.Id);
var element = _buildStorage.GetElement(new BuildSearchModel
{
BuildName = model.BuildName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,107 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace HardwareShopContracts.BusinessLogicsContracts
{
public class CommentLogic : ICommentLogic
{
private readonly ILogger _logger;
private readonly ICommentStorage _commentStorage;
public CommentLogic(ILogger<CommentLogic> logger, ICommentStorage commentStorage)
{
_logger = logger;
_commentStorage = commentStorage;
}
public List<CommentViewModel>? ReadList(CommentSearchModel? model)
{
_logger.LogInformation("ReadList. BuildId:{BuildId}. Id:{Id}", model?.BuildId, model?.Id);
var list = model == null ? _commentStorage.GetFullList() : _commentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CommentViewModel? ReadElement(CommentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BuildId:{BuildId}. Id:{Id}", model.BuildId, model.Id);
var element = _commentStorage.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 Create(CommentBindingModel model)
{
CheckModel(model);
if (_commentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CommentBindingModel model)
{
CheckModel(model);
if (_commentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CommentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_commentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CommentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Text))
{
throw new ArgumentNullException("Нет текста у комментария", nameof(model.Id));
}
if (model.BuildId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у сборки", nameof(model.BuildId));
}
if (model.Id < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у комментария", nameof(model.Id));
}
_logger.LogInformation("Comment. BuildId:{BuildId}. Id:{Id}", model.BuildId, model.Id);
}
}
}

View File

@ -0,0 +1,127 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Enums;
using Microsoft.Extensions.Logging;
namespace HardwareShopContracts.BusinessLogicsContracts
{
public class PurchaseLogic : IPurchaseLogic
{
private readonly ILogger _logger;
private readonly IPurchaseStorage _purchaseStorage;
public PurchaseLogic(ILogger<PurchaseLogic> logger, IPurchaseStorage purchaseStorage)
{
_logger = logger;
_purchaseStorage = purchaseStorage;
}
public List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _purchaseStorage.GetFullList() : _purchaseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CommentViewModel? ReadElement(CommentSearchModel model)
{
throw new NotImplementedException();
}
public bool DeliveryPurchase(PurchaseBindingModel model)
{
var viewModel = _purchaseStorage.GetElement(new PurchaseSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.PurchaseStatus + 1 != PurchaseStatus.Выдан)
{
_logger.LogWarning("Change status operation failed");
return false;
}
model.PurchaseStatus = PurchaseStatus.Выдан;
model.DatePurchase = DateTime.Now;
CheckModel(model, false);
if (_purchaseStorage.Update(model) == null)
{
_logger.LogWarning("Change status operation failed");
return false;
}
return true;
}
public bool Create(PurchaseBindingModel model)
{
CheckModel(model);
if (model.PurchaseStatus != PurchaseStatus.Неизвестен)
{
_logger.LogWarning("Insert operation failed");
return false;
}
model.PurchaseStatus = PurchaseStatus.Выполняется;
if (_purchaseStorage.Insert(model) == null)
{
model.PurchaseStatus = PurchaseStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PurchaseBindingModel model)
{
CheckModel(model);
if (_purchaseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PurchaseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_purchaseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PurchaseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Id < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у продукта", nameof(model.Id));
}
if (model.UserId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.UserId));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Purchase. PurchaseID:{Id}. Sum:{ Sum}", model.Id, model.Sum);
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@ -8,7 +8,14 @@
<ItemGroup> <ItemGroup>
<Folder Include="BusinessLogics\Storekeeper\" /> <Folder Include="BusinessLogics\Storekeeper\" />
<Folder Include="BusinessLogics\Worker\" /> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HardwareShopContracts\HardwareShopContracts.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -7,7 +7,10 @@ namespace HardwareShopContracts.BusinessLogicsContracts
public interface IPurchaseLogic public interface IPurchaseLogic
{ {
List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model); List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model);
bool CreatePurchase(PurchaseBindingModel model); CommentViewModel? ReadElement(CommentSearchModel model);
bool Create(PurchaseBindingModel model);
bool Update(PurchaseBindingModel model);
bool Delete(PurchaseBindingModel model);
bool DeliveryPurchase(PurchaseBindingModel model); bool DeliveryPurchase(PurchaseBindingModel model);
} }
} }