21 Commits

Author SHA1 Message Date
1f805fdff1 CustomMapper 2025-05-30 02:27:18 +04:00
983feed7f5 locale de and new tests 2025-05-29 14:38:33 +04:00
e0099c0646 locale en 2025-05-29 03:32:27 +04:00
c915093bca check locale 2025-05-29 03:20:21 +04:00
9a7e95e66b strings 2025-05-29 02:46:09 +04:00
325ae79304 Dates 2025-05-28 17:43:48 +04:00
f7147a67eb Numbers 2025-05-28 16:55:39 +04:00
52c8343003 Corrections 2025-05-28 15:11:27 +04:00
21ab6ed0eb CreateDocumentSalaryByPeriod 2025-05-27 23:48:15 +04:00
abbd84abc1 GetDataSalaryByPeriod 2025-05-27 22:53:10 +04:00
8a9d8727fe CreateDocumentSalesByPeriod 2025-05-27 22:08:13 +04:00
b06e0018d6 GetDataSaleByPeriod 2025-05-27 20:54:32 +04:00
a433ac5c79 CreateDocumentProductsByRestaurant 2025-05-27 19:09:06 +04:00
47cc5397a9 Reapply "MVC и тесты для Report"
This reverts commit 167bb1f2c5.
2025-05-27 15:19:32 +04:00
167bb1f2c5 Revert "MVC и тесты для Report"
This reverts commit a3726e4fab.
2025-05-27 15:17:21 +04:00
a3726e4fab MVC и тесты для Report 2025-05-27 15:16:46 +04:00
d206ca265c Prepare 2025-05-21 20:35:00 +04:00
1099d8c922 Сделана миграция на изменение в схеме таблицы Работник 2025-04-23 23:33:49 +04:00
5ef0ef450f Расчёт зарплаты 2025-04-23 23:28:58 +04:00
5c4f82e7b2 Изменения в должности 2025-04-23 19:06:31 +04:00
44ec32fe0f Первая миграция 2025-04-22 22:39:41 +04:00
146 changed files with 7410 additions and 1378 deletions

View File

@@ -1,22 +1,25 @@
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.StorageContracts;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using System.Text.Json;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class PostBuisnessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBuisnessLogicContract
internal class PostBuisnessLogicContract(IPostStorageContract postStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : IPostBuisnessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<PostDataModel> GetAllPosts()
{
_logger.LogInformation("GetAllPosts");
return _postStorageContract.GetList() ?? throw new NullListException();
return _postStorageContract.GetList();
}
public List<PostDataModel> GetAllDataOfPost(string postId)
@@ -28,9 +31,9 @@ internal class PostBuisnessLogicContract(IPostStorageContract postStorageContrac
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "PostId"));
}
return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException();
return _postStorageContract.GetPostWithHistory(postId);
}
public PostDataModel GetPostByData(string data)
@@ -42,16 +45,16 @@ internal class PostBuisnessLogicContract(IPostStorageContract postStorageContrac
}
if (data.IsGuid())
{
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
}
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data, _localizer);
}
public void InsertPost(PostDataModel postDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
postDataModel.Validate(_localizer);
_postStorageContract.AddElement(postDataModel);
}
@@ -59,7 +62,7 @@ internal class PostBuisnessLogicContract(IPostStorageContract postStorageContrac
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
postDataModel.Validate(_localizer);
_postStorageContract.UpdElement(postDataModel);
}
@@ -72,7 +75,7 @@ internal class PostBuisnessLogicContract(IPostStorageContract postStorageContrac
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_postStorageContract.DelElement(id);
}
@@ -86,7 +89,7 @@ internal class PostBuisnessLogicContract(IPostStorageContract postStorageContrac
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_postStorageContract.ResElement(id);
}

View File

@@ -1,23 +1,39 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using System.Text.Json;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class ProductBuisnessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBuisnessLogicContract
internal class ProductBuisnessLogicContract(IProductStorageContract productStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : IProductBuisnessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IProductStorageContract _productStorageContract = productStorageContract;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<ProductDataModel> GetAllProducts(bool onlyActive = true)
{
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
return _productStorageContract.GetList(onlyActive) ?? throw new NullListException();
return _productStorageContract.GetList(onlyActive);
}
public List<ProductDataModel> GetAllProductsByRestaurant(string restaurantId, bool onlyActive = true)
{
if (restaurantId.IsEmpty())
{
throw new ArgumentNullException(nameof(restaurantId));
}
if (!restaurantId.IsGuid())
{
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "RestaurantId"));
}
_logger.LogInformation("GetAllProducts params: {restaurantId}, {onlyActive}", restaurantId, onlyActive);
return _productStorageContract.GetList(onlyActive, restaurantId);
}
public List<ProductHistoryDataModel> GetProductHistoryByProduct(string productId)
@@ -29,9 +45,9 @@ internal class ProductBuisnessLogicContract(IProductStorageContract productStora
}
if (!productId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "ProductId"));
}
return _productStorageContract.GetHistoryByProductId(productId) ?? throw new NullListException();
return _productStorageContract.GetHistoryByProductId(productId);
}
public ProductDataModel GetProductByData(string data)
@@ -43,16 +59,16 @@ internal class ProductBuisnessLogicContract(IProductStorageContract productStora
}
if (data.IsGuid())
{
return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
}
return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data, _localizer);
}
public void InsertProduct(ProductDataModel productDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
productDataModel.Validate(_localizer);
_productStorageContract.AddElement(productDataModel);
}
@@ -60,7 +76,7 @@ internal class ProductBuisnessLogicContract(IProductStorageContract productStora
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
productDataModel.Validate(_localizer);
_productStorageContract.UpdElement(productDataModel);
}
@@ -73,7 +89,7 @@ internal class ProductBuisnessLogicContract(IProductStorageContract productStora
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_productStorageContract.DelElement(id);
}

View File

@@ -0,0 +1,120 @@
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SPiluSZharuBuisnessLogic.OfficePackage;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class ReportContract : IReportContract
{
private readonly IProductStorageContract _productStorageContract;
private readonly ISaleStorageContract _saleStorageContract;
private readonly ISalaryStorageContract _salaryStorageContract;
private readonly BaseWordBuilder _baseWordBuilder;
private readonly BaseExcelBuilder _baseExcelBuilder;
private readonly BasePdfBuilder _basePdfBuilder;
private readonly IStringLocalizer<Messages> _localizer;
private readonly ILogger _logger;
internal readonly string[] _documentHeader;
internal readonly string[] _tableHeader;
public ReportContract(IProductStorageContract productStorageContract, ISaleStorageContract saleStorageContract, ISalaryStorageContract salaryStorageContract, BaseWordBuilder baseWordBuilder, BaseExcelBuilder baseExcelBuilder, BasePdfBuilder basePdfBuilder, IStringLocalizer<Messages> localizer, ILogger logger)
{
_productStorageContract = productStorageContract;
_saleStorageContract = saleStorageContract;
_salaryStorageContract = salaryStorageContract;
_baseWordBuilder = baseWordBuilder;
_baseExcelBuilder = baseExcelBuilder;
_basePdfBuilder = basePdfBuilder;
_localizer = localizer;
_logger = logger;
_documentHeader = [_localizer["DocumentDocCaptionRestaurant"], _localizer["DocumentDocCaptionProduct"]];
_tableHeader = [_localizer["DocumentExcelCaptionDate"], _localizer["DocumentExcelCaptionSum"],
_localizer["DocumentExcelCaptionProduct"], _localizer["DocumentExcelCaptionCount"]];
}
public async Task<Stream> CreateDocumentProductsByRestaurantAsync(CancellationToken ct)
{
_logger.LogInformation("Create report ProductsByRestaurant");
var data = await GetDataByProductsAsync(ct);
return _baseWordBuilder
.AddHeader(_localizer["DocumentDocHeader"])
.AddParagraph(string.Format(_localizer["DocumentDocSubHeader"], DateTime.Now))
.AddTable([3000, 5000], [.. new List<string[]>() { _documentHeader }.Union([.. data.SelectMany(x => (new List<string[]>() { new string[] { x.RestaurantName, "" } }).Union(x.Products.Select(y => new string[] { "", y })))])])
.Build();
}
public async Task<Stream> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Create report SalesByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
var data = await GetDataBySalesAsync(dateStart, dateFinish, ct) ?? throw new InvalidOperationException("No found data");
return _baseExcelBuilder
.AddHeader(_localizer["DocumentExcelHeader"], 0, 4)
.AddParagraph(string.Format(_localizer["DocumentExcelSubHeader"], dateStart.ToLocalTime().ToShortDateString(), dateFinish.ToLocalTime().ToShortDateString()), 2)
.AddTable([10, 10, 10, 10], [.. new List<string[]>() { _tableHeader }.Union(data.SelectMany(x => (new List<string[]>() { new string[] { x.SaleDate.ToShortDateString(), x.Sum.ToString("N2"), "", "" } }).Union(x.Products!.Select(y => new string[] { "", "", y.ProductName, y.Count.ToString("N2") })).ToArray())).Union([["Всего", data.Sum(x => x.Sum).ToString("N2"), "", ""]])])
.Build();
}
public async Task<Stream> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Create report SalaryByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
var data = await GetDataBySalaryAsync(dateStart, dateFinish, ct) ?? throw new InvalidOperationException("No found data");
return _basePdfBuilder
.AddHeader(_localizer["DocumentPdfHeader"])
.AddParagraph(string.Format(_localizer["DocumentPdfSubHeader"], dateStart.ToLocalTime().ToShortDateString(), dateFinish.ToLocalTime().ToShortDateString()))
.AddPieChart("Начисления", [.. data.Select(x => (x.WorkerFIO, x.TotalSalary))])
.Build();
}
public Task<List<RestaurantProductDataModel>> GetDataProductsByRestaurantAsync(CancellationToken ct)
{
_logger.LogInformation("Get data ProductsByRestaurant");
return GetDataByProductsAsync(ct);
}
public Task<List<SaleDataModel>> GetDataSaleByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Get data SalesByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
return GetDataBySalesAsync(dateStart, dateFinish, ct);
}
public Task<List<WorkerSalaryByPeriodDataModel>> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Get data SalaryByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
return GetDataBySalaryAsync(dateStart, dateFinish, ct);
}
private async Task<List<RestaurantProductDataModel>> GetDataByProductsAsync(CancellationToken ct) => [.. (await _productStorageContract.GetListAsync(ct)).GroupBy(x => x.RestaurantName).Select(x => new RestaurantProductDataModel { RestaurantName = x.Key, Products = [.. x.Select(y => y.ProductName)] })];
private async Task<List<SaleDataModel>> GetDataBySalesAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
if (dateStart.IsDateNotOlder(dateFinish))
{
throw new IncorrectDatesException(dateStart, dateFinish, _localizer);
}
return [.. (await _saleStorageContract.GetListAsync(dateStart, dateFinish, ct)).OrderBy(x => x.SaleDate)];
}
private async Task<List<WorkerSalaryByPeriodDataModel>> GetDataBySalaryAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
if (dateStart.IsDateNotOlder(dateFinish))
{
throw new IncorrectDatesException(dateStart, dateFinish, _localizer);
}
return [.. (await _salaryStorageContract.GetListAsync(dateStart, dateFinish, ct)).GroupBy(x => x.WorkerId).Select(x => new WorkerSalaryByPeriodDataModel { WorkerFIO = x.First().WorkerName, TotalSalary = x.Sum(y => y.Salary), FromPeriod = x.Min(y => y.SalaryDate), ToPeriod = x.Max(y => y.SalaryDate) }).OrderBy(x => x.WorkerFIO)];
}
}

View File

@@ -1,22 +1,25 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using System.Text.Json;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class RestaurantBuisnessLogicContract(IRestaurantStorageContract restaurantStorageContract, ILogger logger) : IRestaurantBuisnessLogicContract
internal class RestaurantBuisnessLogicContract(IRestaurantStorageContract restaurantStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : IRestaurantBuisnessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IRestaurantStorageContract _restaurantStorageContract = restaurantStorageContract;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<RestaurantDataModel> GetAllRestaurants()
{
_logger.LogInformation("GetAllRestaurants");
return _restaurantStorageContract.GetList() ?? throw new NullListException();
return _restaurantStorageContract.GetList();
}
public RestaurantDataModel GetRestaurantByData(string data)
@@ -28,17 +31,17 @@ internal class RestaurantBuisnessLogicContract(IRestaurantStorageContract restau
}
if (data.IsGuid())
{
return _restaurantStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return _restaurantStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
}
return _restaurantStorageContract.GetElementByName(data) ?? _restaurantStorageContract.GetElementByOldName(data) ??
throw new ElementNotFoundException(data);
throw new ElementNotFoundException(data, _localizer);
}
public void InsertRestaurant(RestaurantDataModel restaurantDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(restaurantDataModel));
ArgumentNullException.ThrowIfNull(restaurantDataModel);
restaurantDataModel.Validate();
restaurantDataModel.Validate(_localizer);
_restaurantStorageContract.AddElement(restaurantDataModel);
}
@@ -46,7 +49,7 @@ internal class RestaurantBuisnessLogicContract(IRestaurantStorageContract restau
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(restaurantDataModel));
ArgumentNullException.ThrowIfNull(restaurantDataModel);
restaurantDataModel.Validate();
restaurantDataModel.Validate(_localizer);
_restaurantStorageContract.UpdElement(restaurantDataModel);
}
@@ -59,7 +62,7 @@ internal class RestaurantBuisnessLogicContract(IRestaurantStorageContract restau
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_restaurantStorageContract.DelElement(id);
}

View File

@@ -1,36 +1,43 @@
using SPiluSZharuContracts.BuisnessLogicContracts;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using Microsoft.Extensions.Logging;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class SalaryBuisnessLogicContract(ISalaryStorageContract salaryStorageContract, ISaleStorageContract saleStorageContract,
IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBuisnessLogicContract
IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, IStringLocalizer<Messages> localizer, ILogger logger, IConfigurationSalary сonfiguration) : ISalaryBuisnessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
private readonly IConfigurationSalary _salaryConfiguration = сonfiguration;
private readonly IStringLocalizer<Messages> _localizer = localizer;
private readonly Lock _lockObject = new();
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
return _salaryStorageContract.GetList(fromDate, toDate);
}
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
{
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
if (workerId.IsEmpty())
{
@@ -38,10 +45,10 @@ internal class SalaryBuisnessLogicContract(ISalaryStorageContract salaryStorageC
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
}
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, workerId);
return _salaryStorageContract.GetList(fromDate, toDate, workerId) ?? throw new NullListException();
return _salaryStorageContract.GetList(fromDate, toDate, workerId);
}
public void CalculateSalaryByMounth(DateTime date)
@@ -49,16 +56,71 @@ internal class SalaryBuisnessLogicContract(ISalaryStorageContract salaryStorageC
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var workers = _workerStorageContract.GetList() ?? throw new NullListException();
var workers = _workerStorageContract.GetList();
foreach (var worker in workers)
{
var sales = _saleStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ??
throw new NullListException();
var post = _postStorageContract.GetElementById(worker.PostId) ??
throw new NullListException();
var salary = post.Salary + sales * 0.1;
var sales = _saleStorageContract.GetList(startDate, finishDate, workerId: worker.Id);
var post = _postStorageContract.GetElementById(worker.PostId);
var salary = post.ConfigurationModel switch
{
null => 0,
DeliveryManPostConfiguration dpc => CalculateSalaryForDeliveryMan(sales, startDate, finishDate, dpc),
OperatorPostConfiguration opc => CalculateSalaryForOperator(startDate, finishDate, opc),
PostConfiguration pc => pc.Rate,
};
_logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, DateTime.SpecifyKind(finishDate, DateTimeKind.Utc), salary));
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
}
}
private double CalculateSalaryForDeliveryMan(List<SaleDataModel> sales, DateTime startDate, DateTime finishDate, DeliveryManPostConfiguration config)
{
var calcPercent = 0.0;
var dates = new List<DateTime>();
for (var date = startDate; date < finishDate; date = date.AddDays(1))
{
dates.Add(date);
}
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = _salaryConfiguration.MaxConcurrentThreads
};
Parallel.ForEach(dates, parallelOptions, date =>
{
var salesInDay = sales.Where(x => x.SaleDate.Date == date.Date).ToArray();
if (salesInDay.Length > 0)
{
lock (_lockObject)
{
calcPercent += (salesInDay.Sum(x => x.Sum) / salesInDay.Length) * config.SalePercent;
}
}
});
double calcBonusTask = 0;
try
{
calcBonusTask = sales.Where(x => x.Sum > _salaryConfiguration.ExtraSaleSum).Sum(x => x.Sum) * config.BonusForExtraSales;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in bonus calculation");
}
return config.Rate + calcPercent + calcBonusTask;
}
private double CalculateSalaryForOperator(DateTime startDate, DateTime finishDate, OperatorPostConfiguration config)
{
try
{
return config.Rate + config.PersonalCountTrendPremium * _workerStorageContract.GetWorkerTrend(startDate, finishDate);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in the operator payroll process");
return 0;
}
}
}

View File

@@ -1,27 +1,29 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using System.Text.Json;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBuisnessLogicContract
internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : ISaleBuisnessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
return _saleStorageContract.GetList(fromDate, toDate);
}
public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
@@ -29,7 +31,7 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
_logger.LogInformation("GetAllSales params: {workerId}, {fromDate}, {toDate}", workerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
if (workerId.IsEmpty())
{
@@ -37,9 +39,9 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
}
return _saleStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException();
return _saleStorageContract.GetList(fromDate, toDate, workerId: workerId);
}
public List<SaleDataModel> GetAllSalesByRestaurantByPeriod(string restaurantId, DateTime fromDate, DateTime toDate)
@@ -47,7 +49,7 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
_logger.LogInformation("GetAllSales params: {restaurantId}, {fromDate}, {toDate}", restaurantId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
if (restaurantId.IsEmpty())
{
@@ -55,9 +57,9 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
}
if (!restaurantId.IsGuid())
{
throw new ValidationException("The value in the field restaurantId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "RestaurantId"));
}
return _saleStorageContract.GetList(fromDate, toDate, restaurantId: restaurantId) ?? throw new NullListException();
return _saleStorageContract.GetList(fromDate, toDate, restaurantId: restaurantId);
}
public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
@@ -65,7 +67,7 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
_logger.LogInformation("GetAllSales params: {productId}, {fromDate}, {toDate}", productId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
if (productId.IsEmpty())
{
@@ -73,9 +75,9 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
}
if (!productId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "ProductId"));
}
return _saleStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException();
return _saleStorageContract.GetList(fromDate, toDate, productId: productId);
}
public SaleDataModel GetSaleByData(string data)
@@ -87,16 +89,16 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
return _saleStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return _saleStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
}
public void InsertSale(SaleDataModel saleDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
ArgumentNullException.ThrowIfNull(saleDataModel);
saleDataModel.Validate();
saleDataModel.Validate(_localizer);
_saleStorageContract.AddElement(saleDataModel);
}
@@ -109,7 +111,7 @@ internal class SaleBuisnessLogicContract(ISaleStorageContract saleStorageContrac
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_saleStorageContract.DelElement(id);
}

View File

@@ -1,23 +1,26 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace SPiluSZharuBuisnessLogic.Implementations;
internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBuisnessLogicContract
internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageContract, IStringLocalizer<Messages> localizer, ILogger logger) : IWorkerBuisnessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
{
_logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
return _workerStorageContract.GetList(onlyActive);
}
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true)
@@ -29,9 +32,9 @@ internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageC
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "PostId"));
}
return _workerStorageContract.GetList(onlyActive, postId) ?? throw new NullListException();
return _workerStorageContract.GetList(onlyActive, postId);
}
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
@@ -39,9 +42,9 @@ internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageC
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException();
return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate);
}
public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
@@ -49,9 +52,9 @@ internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageC
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
throw new IncorrectDatesException(fromDate, toDate, _localizer);
}
return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException();
return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate);
}
public WorkerDataModel GetWorkerByData(string data)
@@ -63,20 +66,20 @@ internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageC
}
if (data.IsGuid())
{
return _workerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return _workerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
}
if (Regex.IsMatch(data, @"^(8|\+7)(\s|\(|\-)?(\d{3})(\s|\)|\-)?(\d{3})(\s|\-)?(\d{2})(\s|\-)?(\d{2})$"))
{
return _workerStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data);
return _workerStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data, _localizer);
}
return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data, _localizer);
}
public void InsertWorker(WorkerDataModel workerDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel));
ArgumentNullException.ThrowIfNull(workerDataModel);
workerDataModel.Validate();
workerDataModel.Validate(_localizer);
_workerStorageContract.AddElement(workerDataModel);
}
@@ -84,7 +87,7 @@ internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageC
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel));
ArgumentNullException.ThrowIfNull(workerDataModel);
workerDataModel.Validate();
workerDataModel.Validate(_localizer);
_workerStorageContract.UpdElement(workerDataModel);
}
@@ -97,7 +100,7 @@ internal class WorkerBuisnessLogicContract(IWorkerStorageContract workerStorageC
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
throw new ValidationException(string.Format(_localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_workerStorageContract.DelElement(id);
}

View File

@@ -0,0 +1,12 @@
namespace SPiluSZharuBuisnessLogic.OfficePackage;
public abstract class BaseExcelBuilder
{
public abstract BaseExcelBuilder AddHeader(string header, int startIndex, int count);
public abstract BaseExcelBuilder AddParagraph(string text, int columnIndex);
public abstract BaseExcelBuilder AddTable(int[] columnsWidths, List<string[]> data);
public abstract Stream Build();
}

View File

@@ -0,0 +1,12 @@
namespace SPiluSZharuBuisnessLogic.OfficePackage;
public abstract class BasePdfBuilder
{
public abstract BasePdfBuilder AddHeader(string header);
public abstract BasePdfBuilder AddParagraph(string text);
public abstract BasePdfBuilder AddPieChart(string title, List<(string Caption, double Value)> data);
public abstract Stream Build();
}

View File

@@ -0,0 +1,12 @@
namespace SPiluSZharuBuisnessLogic.OfficePackage;
public abstract class BaseWordBuilder
{
public abstract BaseWordBuilder AddHeader(string header);
public abstract BaseWordBuilder AddParagraph(string text);
public abstract BaseWordBuilder AddTable(int[] widths, List<string[]> data);
public abstract Stream Build();
}

View File

@@ -0,0 +1,85 @@
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes.Charts;
using MigraDoc.Rendering;
using System.Text;
namespace SPiluSZharuBuisnessLogic.OfficePackage;
internal class MigraDocPdfBuilder : BasePdfBuilder
{
private readonly Document _document;
public MigraDocPdfBuilder()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
_document = new Document();
DefineStyles();
}
public override BasePdfBuilder AddHeader(string header)
{
_document.AddSection().AddParagraph(header, "NormalBold");
return this;
}
public override BasePdfBuilder AddParagraph(string text)
{
_document.LastSection.AddParagraph(text, "Normal");
return this;
}
public override BasePdfBuilder AddPieChart(string title, List<(string Caption, double Value)> data)
{
if (data == null || data.Count == 0)
{
return this;
}
var chart = new Chart(ChartType.Pie2D);
var series = chart.SeriesCollection.AddSeries();
series.Add(data.Select(x => x.Value).ToArray());
var xseries = chart.XValues.AddXSeries();
xseries.Add(data.Select(x => x.Caption).ToArray());
chart.DataLabel.Type = DataLabelType.Percent;
chart.DataLabel.Position = DataLabelPosition.OutsideEnd;
chart.Width = Unit.FromCentimeter(16);
chart.Height = Unit.FromCentimeter(12);
chart.TopArea.AddParagraph(title);
chart.XAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.HasMajorGridlines = true;
chart.PlotArea.LineFormat.Width = 1;
chart.PlotArea.LineFormat.Visible = true;
chart.TopArea.AddLegend();
_document.LastSection.Add(chart);
return this;
}
public override Stream Build()
{
var stream = new MemoryStream();
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
renderer.RenderDocument();
renderer.PdfDocument.Save(stream);
return stream;
}
private void DefineStyles()
{
var style = _document.Styles.AddStyle("NormalBold", "Normal");
style.Font.Bold = true;
}
}

View File

@@ -0,0 +1,303 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
namespace SPiluSZharuBuisnessLogic.OfficePackage;
internal class OpenXmlExcelBuilder : BaseExcelBuilder
{
private readonly SheetData _sheetData;
private readonly MergeCells _mergeCells;
private readonly Columns _columns;
private uint _rowIndex = 0;
public OpenXmlExcelBuilder()
{
_sheetData = new SheetData();
_mergeCells = new MergeCells();
_columns = new Columns();
_rowIndex = 1;
}
public override BaseExcelBuilder AddHeader(string header, int startIndex, int count)
{
CreateCell(startIndex, _rowIndex, header, StyleIndex.BoldTextWithoutBorder);
for (int i = startIndex + 1; i < startIndex + count; ++i)
{
CreateCell(i, _rowIndex, "", StyleIndex.SimpleTextWithoutBorder);
}
_mergeCells.Append(new MergeCell()
{
Reference =
new StringValue($"{GetExcelColumnName(startIndex)}{_rowIndex}:{GetExcelColumnName(startIndex + count - 1)}{_rowIndex}")
});
_rowIndex++;
return this;
}
public override BaseExcelBuilder AddParagraph(string text, int columnIndex)
{
CreateCell(columnIndex, _rowIndex++, text, StyleIndex.SimpleTextWithoutBorder);
return this;
}
public override BaseExcelBuilder AddTable(int[] columnsWidths, List<string[]> data)
{
if (columnsWidths == null || columnsWidths.Length == 0)
{
throw new ArgumentNullException(nameof(columnsWidths));
}
if (data == null || data.Count == 0)
{
throw new ArgumentNullException(nameof(data));
}
if (data.Any(x => x.Length != columnsWidths.Length))
{
throw new InvalidOperationException("widths.Length != data.Length");
}
uint counter = 1;
int coef = 2;
_columns.Append(columnsWidths.Select(x => new Column
{
Min = counter,
Max = counter++,
Width = x * coef,
CustomWidth = true
}));
for (var j = 0; j < data.First().Length; ++j)
{
CreateCell(j, _rowIndex, data.First()[j], StyleIndex.BoldTextWithBorder);
}
_rowIndex++;
for (var i = 1; i < data.Count - 1; ++i)
{
for (var j = 0; j < data[i].Length; ++j)
{
CreateCell(j, _rowIndex, data[i][j], StyleIndex.SimpleTextWithBorder);
}
_rowIndex++;
}
for (var j = 0; j < data.Last().Length; ++j)
{
CreateCell(j, _rowIndex, data.Last()[j], StyleIndex.BoldTextWithBorder);
}
_rowIndex++;
return this;
}
public override Stream Build()
{
var stream = new MemoryStream();
using var spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
var workbookpart = spreadsheetDocument.AddWorkbookPart();
GenerateStyle(workbookpart);
workbookpart.Workbook = new Workbook();
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
if (_columns.HasChildren)
{
worksheetPart.Worksheet.Append(_columns);
}
worksheetPart.Worksheet.Append(_sheetData);
var sheets = spreadsheetDocument.WorkbookPart!.Workbook.AppendChild(new Sheets());
var sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Лист 1"
};
sheets.Append(sheet);
if (_mergeCells.HasChildren)
{
worksheetPart.Worksheet.InsertAfter(_mergeCells, worksheetPart.Worksheet.Elements<SheetData>().First());
}
return stream;
}
private static void GenerateStyle(WorkbookPart workbookPart)
{
var workbookStylesPart = workbookPart.AddNewPart<WorkbookStylesPart>();
workbookStylesPart.Stylesheet = new Stylesheet();
var fonts = new Fonts() { Count = 2, KnownFonts = BooleanValue.FromBoolean(true) };
fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font
{
FontSize = new FontSize() { Val = 11 },
FontName = new FontName() { Val = "Calibri" },
FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 },
FontScheme = new FontScheme() { Val = new EnumValue<FontSchemeValues>(FontSchemeValues.Minor) }
});
fonts.Append(new DocumentFormat.OpenXml.Spreadsheet.Font
{
FontSize = new FontSize() { Val = 11 },
FontName = new FontName() { Val = "Calibri" },
FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 },
FontScheme = new FontScheme() { Val = new EnumValue<FontSchemeValues>(FontSchemeValues.Minor) },
Bold = new Bold()
});
workbookStylesPart.Stylesheet.Append(fonts);
// Default Fill
var fills = new Fills() { Count = 1 };
fills.Append(new Fill
{
PatternFill = new PatternFill() { PatternType = new EnumValue<PatternValues>(PatternValues.None) }
});
workbookStylesPart.Stylesheet.Append(fills);
// Default Border
var borders = new Borders() { Count = 2 };
borders.Append(new Border
{
LeftBorder = new LeftBorder(),
RightBorder = new RightBorder(),
TopBorder = new TopBorder(),
BottomBorder = new BottomBorder(),
DiagonalBorder = new DiagonalBorder()
});
borders.Append(new Border
{
LeftBorder = new LeftBorder() { Style = BorderStyleValues.Thin },
RightBorder = new RightBorder() { Style = BorderStyleValues.Thin },
TopBorder = new TopBorder() { Style = BorderStyleValues.Thin },
BottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin }
});
workbookStylesPart.Stylesheet.Append(borders);
// Default cell format and a date cell format
var cellFormats = new CellFormats() { Count = 4 };
cellFormats.Append(new CellFormat
{
NumberFormatId = 0,
FormatId = 0,
FontId = 0,
BorderId = 0,
FillId = 0,
Alignment = new Alignment()
{
Horizontal = HorizontalAlignmentValues.Left,
Vertical = VerticalAlignmentValues.Center,
WrapText = true
}
});
cellFormats.Append(new CellFormat
{
NumberFormatId = 0,
FormatId = 0,
FontId = 0,
BorderId = 1,
FillId = 0,
Alignment = new Alignment()
{
Horizontal = HorizontalAlignmentValues.Left,
Vertical = VerticalAlignmentValues.Center,
WrapText = true
}
});
cellFormats.Append(new CellFormat
{
NumberFormatId = 0,
FormatId = 0,
FontId = 1,
BorderId = 0,
FillId = 0,
Alignment = new Alignment()
{
Horizontal = HorizontalAlignmentValues.Center,
Vertical = VerticalAlignmentValues.Center,
WrapText = true
}
});
cellFormats.Append(new CellFormat
{
NumberFormatId = 0,
FormatId = 0,
FontId = 1,
BorderId = 1,
FillId = 0,
Alignment = new Alignment()
{
Horizontal = HorizontalAlignmentValues.Center,
Vertical = VerticalAlignmentValues.Center,
WrapText = true
}
});
workbookStylesPart.Stylesheet.Append(cellFormats);
}
private enum StyleIndex
{
SimpleTextWithoutBorder = 0,
SimpleTextWithBorder = 1,
BoldTextWithoutBorder = 2,
BoldTextWithBorder = 3
}
private void CreateCell(int columnIndex, uint rowIndex, string text, StyleIndex styleIndex)
{
var columnName = GetExcelColumnName(columnIndex);
var cellReference = columnName + rowIndex;
var row = _sheetData.Elements<Row>().FirstOrDefault(r => r.RowIndex! == rowIndex);
if (row == null)
{
row = new Row() { RowIndex = rowIndex };
_sheetData.Append(row);
}
var newCell = row.Elements<Cell>()
.FirstOrDefault(c => c.CellReference != null && c.CellReference.Value == columnName + rowIndex);
if (newCell == null)
{
Cell? refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if (cell.CellReference?.Value != null && cell.CellReference.Value.Length == cellReference.Length)
{
if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
{
refCell = cell;
break;
}
}
}
newCell = new Cell() { CellReference = cellReference };
row.InsertBefore(newCell, refCell);
}
newCell.CellValue = new CellValue(text);
newCell.DataType = CellValues.String;
newCell.StyleIndex = (uint)styleIndex;
}
private static string GetExcelColumnName(int columnNumber)
{
columnNumber += 1;
int dividend = columnNumber;
string columnName = string.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (dividend - modulo) / 26;
}
return columnName;
}
}

View File

@@ -0,0 +1,94 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace SPiluSZharuBuisnessLogic.OfficePackage;
internal class OpenXmlWordBuilder : BaseWordBuilder
{
private readonly Document _document;
private readonly Body _body;
public OpenXmlWordBuilder()
{
_document = new Document();
_body = _document.AppendChild(new Body());
}
public override BaseWordBuilder AddHeader(string header)
{
var paragraph = _body.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
run.AppendChild(new RunProperties(new Bold()));
run.AppendChild(new Text(header));
return this;
}
public override BaseWordBuilder AddParagraph(string text)
{
var paragraph = _body.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
run.AppendChild(new Text(text));
return this;
}
public override BaseWordBuilder AddTable(int[] widths, List<string[]> data)
{
if (widths == null || widths.Length == 0)
{
throw new ArgumentNullException(nameof(widths));
}
if (data == null || data.Count == 0)
{
throw new ArgumentNullException(nameof(data));
}
if (data.Any(x => x.Length != widths.Length))
{
throw new InvalidOperationException("widths.Length != data.Length");
}
var table = new Table();
table.AppendChild(new TableProperties(
new TableBorders(
new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 }
)
));
// Заголовок
var tr = new TableRow();
for (var j = 0; j < widths.Length; ++j)
{
tr.Append(new TableCell(
new TableCellProperties(new TableCellWidth() { Width = widths[j].ToString() }),
new Paragraph(new Run(new RunProperties(new Bold()), new Text(data.First()[j])))));
}
table.Append(tr);
// Данные
table.Append(data.Skip(1).Select(x =>
new TableRow(x.Select(y => new TableCell(new Paragraph(new Run(new Text(y))))))));
_body.Append(table);
return this;
}
public override Stream Build()
{
var stream = new MemoryStream();
using var wordDocument = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);
var mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = _document;
return stream;
}
}

View File

@@ -13,8 +13,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>
<ItemGroup>

View File

@@ -7,6 +7,8 @@ public interface IProductAdapter
{
ProductOperationResponse GetList(bool includeDeleted);
ProductOperationResponse GetRestaurantList(string id, bool includeDeleted);
ProductOperationResponse GetHistory(string id);
ProductOperationResponse GetElement(string data);

View File

@@ -0,0 +1,18 @@
using SPiluSZharuContracts.AdapterContracts.OperationResponses;
namespace SPiluSZharuContracts.AdapterContracts;
public interface IReportAdapter
{
Task<ReportOperationResponse> GetDataProductsByRestaurantAsync(CancellationToken ct);
Task<ReportOperationResponse> GetDataSaleByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<ReportOperationResponse> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<ReportOperationResponse> CreateDocumentProductsByRestaurantAsync(CancellationToken ct);
Task<ReportOperationResponse> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<ReportOperationResponse> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
}

View File

@@ -0,0 +1,19 @@
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.ViewModels;
namespace SPiluSZharuContracts.AdapterContracts.OperationResponses;
public class ReportOperationResponse : OperationResponse
{
public static ReportOperationResponse OK(List<RestaurantProductViewModel> data) => OK<ReportOperationResponse, List<RestaurantProductViewModel>>(data);
public static ReportOperationResponse OK(List<SaleViewModel> data) => OK<ReportOperationResponse, List<SaleViewModel>>(data);
public static ReportOperationResponse OK(List<WorkerSalaryByPeriodViewModel> data) => OK<ReportOperationResponse, List<WorkerSalaryByPeriodViewModel>>(data);
public static ReportOperationResponse OK(Stream data, string fileName) => OK<ReportOperationResponse, Stream>(data, fileName);
public static ReportOperationResponse BadRequest(string message) => BadRequest<ReportOperationResponse>(message);
public static ReportOperationResponse InternalServerError(string message) => InternalServerError<ReportOperationResponse>(message);
}

View File

@@ -1,4 +1,10 @@
namespace SPiluSZharuContracts.BindingModels;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using SPiluSZharuContracts.Mapper;
using System.Text.Json;
namespace SPiluSZharuContracts.BindingModels;
public class PostBindingModel
{
@@ -10,5 +16,27 @@ public class PostBindingModel
public string? PostType { get; set; }
public double Salary { get; set; }
[PostProcessing(MappingCallMethodName = "ParseConfiguration")]
public string? ConfigurationJson { get; set; }
private string ParseConfiguration(PostConfiguration model) =>
System.Text.Json.JsonSerializer.Serialize(model, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
private PostConfiguration? ParseJson(string json)
{
if (ConfigurationJson is null)
return null;
var obj = JToken.Parse(json);
if (obj is not null)
{
return obj.Value<string>("Type") switch
{
nameof(DeliveryManPostConfiguration) => JsonConvert.DeserializeObject<DeliveryManPostConfiguration>(json)!,
nameof(OperatorPostConfiguration) => JsonConvert.DeserializeObject<OperatorPostConfiguration>(json)!,
_ => JsonConvert.DeserializeObject<PostConfiguration>(json)!,
};
}
return null;
}
}

View File

@@ -8,5 +8,7 @@ public class ProductBindingModel
public string? ProductType { get; set; }
public string? RestaurantId { get; set; }
public double Price { get; set; }
}

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.BuisnessLogicContracts;
public interface IPostBuisnessLogicContract
internal interface IPostBuisnessLogicContract
{
List<PostDataModel> GetAllPosts();

View File

@@ -2,10 +2,12 @@
namespace SPiluSZharuContracts.BuisnessLogicContracts;
public interface IProductBuisnessLogicContract
internal interface IProductBuisnessLogicContract
{
List<ProductDataModel> GetAllProducts(bool onlyActive = true);
List<ProductDataModel> GetAllProductsByRestaurant(string restaurantId, bool onlyActive = true);
List<ProductHistoryDataModel> GetProductHistoryByProduct(string productId);
ProductDataModel GetProductByData(string data);

View File

@@ -0,0 +1,18 @@
using SPiluSZharuContracts.DataModels;
namespace SPiluSZharuContracts.BuisnessLogicContracts;
internal interface IReportContract
{
Task<List<RestaurantProductDataModel>> GetDataProductsByRestaurantAsync(CancellationToken ct);
Task<List<SaleDataModel>> GetDataSaleByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<List<WorkerSalaryByPeriodDataModel>> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<Stream> CreateDocumentProductsByRestaurantAsync(CancellationToken ct);
Task<Stream> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<Stream> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
}

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.BuisnessLogicContracts;
public interface IRestaurantBuisnessLogicContract
internal interface IRestaurantBuisnessLogicContract
{
List<RestaurantDataModel> GetAllRestaurants();

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.BuisnessLogicContracts;
public interface ISalaryBuisnessLogicContract
internal interface ISalaryBuisnessLogicContract
{
List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate);

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.BuisnessLogicContracts;
public interface ISaleBuisnessLogicContract
internal interface ISaleBuisnessLogicContract
{
List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate);

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.BuisnessLogicContracts;
public interface IWorkerBuisnessLogicContract
internal interface IWorkerBuisnessLogicContract
{
List<WorkerDataModel> GetAllWorkers(bool onlyActive = true);

View File

@@ -1,35 +1,79 @@
using SPiluSZharuContracts.Enums;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class PostDataModel(string postId, string postName, PostType postType, double salary) : IValidation
internal class PostDataModel(string postId, string postName, PostType postType, PostConfiguration configuration) : IValidation
{
[AlternativeName("PostId")]
public string Id { get; private set; } = postId;
public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
[AlternativeName("Configuration")]
[AlternativeName("ConfigurationJson")]
[PostProcessing(MappingCallMethodName = "ParseJson")]
public PostConfiguration ConfigurationModel { get; private set; } = configuration;
public void Validate()
public PostDataModel() : this(string.Empty, string.Empty, PostType.None, null) { }
public PostDataModel(string postId, string postName) : this(postId, postName, PostType.None, new PostConfiguration() { Rate = 10 }) { }
private PostConfiguration? ParseJson(object json)
{
if (json is PostConfiguration config)
{
return config;
}
if (json is string)
{
var obj = JToken.Parse((string)json);
var type = obj.Value<string>("Type");
switch (type)
{
case nameof(DeliveryManPostConfiguration):
ConfigurationModel = JsonConvert.DeserializeObject<DeliveryManPostConfiguration>((string)json);
break;
case nameof(OperatorPostConfiguration):
ConfigurationModel = JsonConvert.DeserializeObject<OperatorPostConfiguration>((string)json);
break;
default:
ConfigurationModel = JsonConvert.DeserializeObject<PostConfiguration>((string)json);
break;
}
return ConfigurationModel;
}
return null;
}
public void Validate(IStringLocalizer<Messages> localizer)
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
if (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "PostName"));
if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "PostType"));
if (Salary <= 0)
throw new ValidationException("Field Salary is empty");
if (ConfigurationModel is null)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotInitialized"], "ConfigurationModel"));
if (ConfigurationModel!.Rate <= 0)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Rate"));
}
}

View File

@@ -1,39 +1,62 @@
using SPiluSZharuContracts.Enums;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class ProductDataModel(string id, string productName, ProductType productType, double price, bool isDeleted) : IValidation
internal class ProductDataModel(string id, string productName, ProductType productType, string restaurantId, double price, bool isDeleted) : IValidation
{
private readonly RestaurantDataModel? _restaurant;
[AlternativeName("ProductId")]
public string Id { get; private set; } = id;
public string ProductName { get; private set; } = productName;
public ProductType ProductType { get; private set; } = productType;
public string RestaurantId { get; private set; } = restaurantId;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
public ProductDataModel(string id, string productName, ProductType productType, double price) : this(id, productName, productType, price, false) { }
public string RestaurantName => _restaurant?.RestaurantName ?? string.Empty;
public void Validate()
public ProductDataModel(string id, string productName, ProductType productType, string restaurantId, double price, bool isDeleted, RestaurantDataModel restaurant) : this(id, productName, productType, restaurantId, price, isDeleted)
{
_restaurant = restaurant;
}
public ProductDataModel(string id, string productName, ProductType productType, string restaurantId, double price) : this(id, productName, productType, restaurantId, price, false) { }
public ProductDataModel() : this(string.Empty, string.Empty, ProductType.None, string.Empty, 0, false) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
if (ProductName.IsEmpty())
throw new ValidationException("Field ProductName is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "ProductName"));
if (ProductType == ProductType.None)
throw new ValidationException("Field ProductType is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "ProductType"));
if (RestaurantId.IsEmpty())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "RestaurantId"));
if (!RestaurantId.IsGuid())
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "RestaurantId"));
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Price"));
}
}

View File

@@ -1,10 +1,12 @@
using SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class ProductHistoryDataModel(string productId, double oldPrice) : IValidation
internal class ProductHistoryDataModel(string productId, double oldPrice) : IValidation
{
private readonly ProductDataModel? _product;
@@ -22,15 +24,17 @@ public class ProductHistoryDataModel(string productId, double oldPrice) : IValid
_product = product;
}
public void Validate()
public ProductHistoryDataModel() : this(string.Empty, 0) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (ProductId.IsEmpty())
throw new ValidationException("Field ProductId is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "ProductId"));
if (!ProductId.IsGuid())
throw new ValidationException("The value in the field ProductId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "ProductId"));
if (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "OldPrice"));
}
}

View File

@@ -1,11 +1,15 @@
using SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class RestaurantDataModel(string id, string restaurantName, string? prevRestaurantName, string? prevPrevRestaurantName) : IValidation
internal class RestaurantDataModel(string id, string restaurantName, string? prevRestaurantName, string? prevPrevRestaurantName) : IValidation
{
[AlternativeName("RestaurantId")]
public string Id { get; private set; } = id;
public string RestaurantName { get; private set; } = restaurantName;
@@ -16,13 +20,17 @@ public class RestaurantDataModel(string id, string restaurantName, string? prevR
public RestaurantDataModel(string id, string restaurantName) : this(id, restaurantName, null, null) { }
public void Validate()
public RestaurantDataModel() : this(string.Empty, string.Empty, null, null) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
if (RestaurantName.IsEmpty())
throw new ValidationException("Field RestaurantName is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "RestaurantName"));
}
}

View File

@@ -0,0 +1,8 @@
namespace SPiluSZharuContracts.DataModels;
public class RestaurantProductDataModel
{
public required string RestaurantName { get; set; }
public required List<string> Products { get; set; }
}

View File

@@ -1,19 +1,23 @@
using SPiluSZharuContracts.Enums;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation
internal class SalaryDataModel(string workerId, DateTime salaryDate, double salary) : IValidation
{
private readonly WorkerDataModel? _worker;
public string WorkerId { get; private set; } = workerId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public DateTime SalaryDate { get; private set; } = salaryDate.ToUniversalTime();
public double Salary { get; private set; } = workerSalary;
[AlternativeName("WorkerSalary")]
public double Salary { get; private set; } = salary;
public string WorkerName => _worker?.FIO ?? string.Empty;
@@ -22,15 +26,17 @@ public class SalaryDataModel(string workerId, DateTime salaryDate, double worker
_worker = worker;
}
public void Validate()
public SalaryDataModel() : this(string.Empty, DateTime.Now, 0) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "WorkerId"));
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
if (Salary <= 0)
throw new ValidationException("Field Salary is less than or equal to 0");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Salary"));
}
}

View File

@@ -1,10 +1,13 @@
using SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class SaleDataModel : IValidation
internal class SaleDataModel : IValidation
{
private readonly RestaurantDataModel? _restaurant;
@@ -16,12 +19,13 @@ public class SaleDataModel : IValidation
public string? RestaurantId { get; private set; }
public DateTime SaleDate { get; private set; }
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public double Sum { get; private set; }
public bool IsCancel { get; private set; }
[AlternativeName("SaleProducts")]
public List<SaleProductDataModel>? Products { get; private set; }
public string RestaurantName => _restaurant?.RestaurantName ?? string.Empty;
@@ -47,27 +51,34 @@ public class SaleDataModel : IValidation
public SaleDataModel(string id, string workerId, string? restaurantId, List<SaleProductDataModel> products) : this(id, workerId, restaurantId, false, products) { }
public void Validate()
public SaleDataModel() : this(string.Empty, string.Empty, string.Empty, false, new List<SaleProductDataModel>()) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "WorkerId"));
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
if (!RestaurantId?.IsGuid() ?? !RestaurantId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field RestaurantId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "RestaurantId"));
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Sum"));
if ((Products?.Count ?? 0) == 0)
throw new ValidationException("The sale must include products");
if (Products is null)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotInitialized"], "Products"));
if (Products.Count == 0)
throw new ValidationException(localizer["ValidationExceptionMessageNoProductsInSale"]);
Products.ForEach(x => x.Validate(localizer));
}
}

View File

@@ -1,10 +1,12 @@
using SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Resources;
namespace SPiluSZharuContracts.DataModels;
public class SaleProductDataModel(string saleId, string productId, int count, double price) : IValidation
internal class SaleProductDataModel(string saleId, string productId, int count, double price) : IValidation
{
private readonly ProductDataModel? _product;
@@ -23,21 +25,26 @@ public class SaleProductDataModel(string saleId, string productId, int count, do
_product = product;
}
public void Validate()
public SaleProductDataModel() : this(string.Empty, string.Empty, 0, 0) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (SaleId.IsEmpty())
throw new ValidationException("Field SaleId is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "SaleId"));
if (!SaleId.IsGuid())
throw new ValidationException("The value in the field SaleId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "SaleId"));
if (ProductId.IsEmpty())
throw new ValidationException("Field ProductId is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "IProductIdd"));
if (!ProductId.IsGuid())
throw new ValidationException("The value in the field ProductId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "ProductId"));
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Count"));
if (Price <= 0)
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Price"));
}
}

View File

@@ -1,15 +1,19 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Extensions;
using SPiluSZharuContracts.Infrastructure;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using System.Text.RegularExpressions;
namespace SPiluSZharuContracts.DataModels;
public class WorkerDataModel(string id, string fio, string postId, string phoneNumber, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation
internal class WorkerDataModel(string id, string fio, string postId, string phoneNumber, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation
{
private readonly PostDataModel? _post;
[AlternativeName("WorkerId")]
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
@@ -18,9 +22,9 @@ public class WorkerDataModel(string id, string fio, string postId, string phoneN
public string PhoneNumber { get; private set; } = phoneNumber;
public DateTime BirthDate { get; private set; } = birthDate;
public DateTime BirthDate { get; private set; } = birthDate.ToUniversalTime();
public DateTime EmploymentDate { get; private set; } = employmentDate;
public DateTime EmploymentDate { get; private set; } = employmentDate.ToUniversalTime();
public bool IsDeleted { get; private set; } = isDeleted;
@@ -33,36 +37,40 @@ public class WorkerDataModel(string id, string fio, string postId, string phoneN
public WorkerDataModel(string id, string fio, string postId, string phoneNumber, DateTime birthDate, DateTime employmentDate) : this(id, fio, postId, phoneNumber, birthDate, employmentDate, false) { }
public void Validate()
public WorkerDataModel() : this(string.Empty, string.Empty, string.Empty, string.Empty, DateTime.MinValue, DateTime.MinValue, false) { }
public void Validate(IStringLocalizer<Messages> localizer)
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
if (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "FIO"));
if (PostId.IsEmpty())
throw new ValidationException("Field PostId is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "PostId"));
if (!PostId.IsGuid())
throw new ValidationException("The value in the field PostId is not a unique identifier");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "PostId"));
if (PhoneNumber.IsEmpty())
throw new ValidationException("Field PhoneNumber is empty");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "PhoneNumber"));
if (!Regex.IsMatch(PhoneNumber, @"^(8|\+7)(\s|\(|\-)?(\d{3})(\s|\)|\-)?(\d{3})(\s|\-)?(\d{2})(\s|\-)?(\d{2})$"))
throw new ValidationException("Field PhoneNumber is not a phone number");
throw new ValidationException(localizer["ValidationExceptionMessageIncorrectPhoneNumber"]);
if (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageMinorsBirthDate"], BirthDate.ToShortDateString()));
if (EmploymentDate.Date < BirthDate.Date)
throw new ValidationException("The date of employment cannot be less than the date of birth");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmploymentDateAndBirthDate"],
EmploymentDate.ToShortDateString(), BirthDate.ToShortDateString()));
if ((EmploymentDate - BirthDate).TotalDays / 365 < 16) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageMinorsEmploymentDate"],
EmploymentDate.ToShortDateString(), BirthDate.ToShortDateString()));
}
}

View File

@@ -0,0 +1,12 @@
namespace SPiluSZharuContracts.DataModels;
public class WorkerSalaryByPeriodDataModel
{
public required string WorkerFIO { get; set; }
public double TotalSalary { get; set; }
public DateTime FromPeriod { get; set; }
public DateTime ToPeriod { get; set; }
}

View File

@@ -1,6 +1,8 @@
namespace SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Resources;
public class ElementDeletedException : Exception
{
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
}
namespace SPiluSZharuContracts.Exceptions;
internal class ElementDeletedException(string id, IStringLocalizer<Messages> localizer) :
Exception(string.Format(localizer["ElementDeletedExceptionMessage"], id))
{ }

View File

@@ -1,14 +1,12 @@
namespace SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Resources;
public class ElementExistsException : Exception
namespace SPiluSZharuContracts.Exceptions;
internal class ElementExistsException(string paramName, string paramValue, IStringLocalizer<Messages> localizer) :
Exception(string.Format(localizer["ElementExistsExceptionMessage"], paramValue, paramName))
{
public string ParamName { get; private set; }
public string ParamName { get; private set; } = paramName;
public string ParamValue { get; private set; }
public ElementExistsException(string paramName, string paramValue) : base($"There is already an element with value{paramValue} of parameter {paramName}")
{
ParamName = paramName;
ParamValue = paramValue;
}
public string ParamValue { get; private set; } = paramValue;
}

View File

@@ -1,11 +1,10 @@
namespace SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Resources;
public class ElementNotFoundException : Exception
namespace SPiluSZharuContracts.Exceptions;
internal class ElementNotFoundException(string value, IStringLocalizer<Messages> localizer) :
Exception(string.Format(localizer["AdapterMessageElementNotFoundException"], value))
{
public string Value { get; private set; }
public ElementNotFoundException(string value) : base($"Element not found at value = {value}")
{
Value = value;
}
public string Value { get; private set; } = value;
}

View File

@@ -1,6 +1,8 @@
namespace SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Resources;
public class IncorrectDatesException : Exception
{
public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date.. StartDate: {start:dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { }
}
namespace SPiluSZharuContracts.Exceptions;
internal class IncorrectDatesException(DateTime start, DateTime end, IStringLocalizer<Messages> localizer) :
Exception(string.Format(localizer["IncorrectDatesExceptionMessage"], start.ToShortDateString(), end.ToShortDateString()))
{ }

View File

@@ -1,6 +0,0 @@
namespace SPiluSZharuContracts.Exceptions;
public class NullListException : Exception
{
public NullListException() : base("The returned list is null") { }
}

View File

@@ -1,6 +1,8 @@
namespace SPiluSZharuContracts.Exceptions;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Resources;
public class StorageException : Exception
{
public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex) { }
}
namespace SPiluSZharuContracts.Exceptions;
internal class StorageException(Exception ex, IStringLocalizer<Messages> localizer) :
Exception(string.Format(localizer["StorageExceptionMessage"], ex.Message), ex)
{ }

View File

@@ -0,0 +1,8 @@
namespace SPiluSZharuContracts.Infrastructure;
public interface IConfigurationSalary
{
double ExtraSaleSum { get; }
int MaxConcurrentThreads { get; }
}

View File

@@ -1,6 +1,9 @@
namespace SPiluSZharuContracts.Infrastructure;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.Resources;
public interface IValidation
namespace SPiluSZharuContracts.Infrastructure;
internal interface IValidation
{
void Validate();
void Validate(IStringLocalizer<Messages> localizer);
}

View File

@@ -10,6 +10,8 @@ public class OperationResponse
protected object? Result { get; set; }
protected string? FileName { get; set; }
public IActionResult GetResponse(HttpRequest request, HttpResponse response)
{
ArgumentNullException.ThrowIfNull(request);
@@ -21,12 +23,21 @@ public class OperationResponse
{
return new StatusCodeResult((int)StatusCode);
}
if (Result is Stream stream)
{
return new FileStreamResult(stream, "application/octet-stream")
{
FileDownloadName = FileName
};
}
return new ObjectResult(Result);
}
protected static TResult OK<TResult, TData>(TData data) where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.OK, Result = data };
protected static TResult OK<TResult, TData>(TData data, string fileName) where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.OK, Result = data, FileName = fileName };
protected static TResult NoContent<TResult>() where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.NoContent };
protected static TResult BadRequest<TResult>(string? errorMessage = null) where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.BadRequest, Result = errorMessage };

View File

@@ -0,0 +1,10 @@
namespace SPiluSZharuContracts.Infrastructure.PostConfiguration;
public class DeliveryManPostConfiguration : PostConfiguration
{
public override string Type => nameof(DeliveryManPostConfiguration);
public double SalePercent { get; set; }
public double BonusForExtraSales { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace SPiluSZharuContracts.Infrastructure.PostConfiguration;
public class OperatorPostConfiguration : PostConfiguration
{
public override string Type => nameof(OperatorPostConfiguration);
public double PersonalCountTrendPremium { get; set; }
}

View File

@@ -0,0 +1,12 @@
using System.Globalization;
namespace SPiluSZharuContracts.Infrastructure.PostConfiguration;
public class PostConfiguration
{
public virtual string Type => nameof(PostConfiguration);
public double Rate { get; set; }
public string CultureName { get; set; } = CultureInfo.CurrentCulture.Name;
}

View File

@@ -0,0 +1,7 @@
namespace SPiluSZharuContracts.Mapper;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
class AlternativeNameAttribute(string alternativeName) : Attribute
{
public string AlternativeName { get; } = alternativeName;
}

View File

@@ -0,0 +1,280 @@
using System.Collections;
using System.Reflection;
namespace SPiluSZharuContracts.Mapper;
internal static class CustomMapper
{
public static To MapObject<To>(object obj, To newObject)
{
ArgumentNullException.ThrowIfNull(obj);
ArgumentNullException.ThrowIfNull(newObject);
var typeFrom = obj.GetType();
var typeTo = newObject.GetType();
var propertiesFrom = typeFrom.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(x => x.CanRead)
.ToArray();
// свойств
foreach (var property in typeTo.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(x => x.CanWrite))
{
if (property.GetCustomAttribute<IgnoreMappingAttribute>() is not null)
continue;
var propertyFrom = TryGetPropertyFrom(property, propertiesFrom);
if (propertyFrom is null)
{
FindAndMapDefaultValue(property, newObject);
continue;
}
var fromValue = propertyFrom.GetValue(obj);
var postProcessingAttribute = property.GetCustomAttribute<PostProcessingAttribute>();
if (postProcessingAttribute is not null)
{
var value = PostProcessing(fromValue, postProcessingAttribute, newObject);
if (value is not null)
{
property.SetValue(newObject, value);
}
continue;
}
if (propertyFrom.PropertyType.IsGenericType && propertyFrom.PropertyType.Name.StartsWith("List") && fromValue is not null)
{
fromValue = MapListOfObjects(property, fromValue);
}
if (propertyFrom.PropertyType.IsEnum && property.PropertyType == typeof(string) && fromValue != null)
{
fromValue = fromValue.ToString();
}
else if (!propertyFrom.PropertyType.IsEnum && property.PropertyType.IsEnum && fromValue is not null)
{
if (fromValue is string stringValue && !string.IsNullOrEmpty(stringValue))
fromValue = Enum.Parse(property.PropertyType, stringValue);
else
fromValue = Enum.ToObject(property.PropertyType, fromValue);
}
if (fromValue is not null)
{
if (propertyFrom.PropertyType.IsClass
&& property.PropertyType.IsClass
&& propertyFrom.PropertyType != typeof(string)
&& property.PropertyType != typeof(string)
&& !property.PropertyType.IsAssignableFrom(propertyFrom.PropertyType))
{
try
{
var nestedInstance = Activator.CreateInstance(property.PropertyType);
if (nestedInstance != null)
{
var nestedMapped = MapObject(fromValue, nestedInstance);
property.SetValue(newObject, nestedMapped);
continue;
}
}
catch
{
// ignore
}
}
property.SetValue(newObject, fromValue);
}
}
// полей
var fieldsTo = typeTo.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var fieldsFrom = typeFrom.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var field in fieldsTo)
{
if (field.Name.Contains("k__BackingField"))
continue;
if (field.GetCustomAttribute<IgnoreMappingAttribute>() is not null)
continue;
var sourceField = fieldsFrom.FirstOrDefault(f => f.Name == field.Name);
object? fromValue = null;
if (sourceField is not null)
{
fromValue = sourceField.GetValue(obj);
}
else
{
var propertyName = field.Name.Trim('_').Substring(0, 1).ToUpper() + field.Name.Substring(2);
var sourceProperty = typeFrom.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (sourceProperty is not null && sourceProperty.CanRead)
{
fromValue = sourceProperty.GetValue(obj);
}
}
if (fromValue is null)
continue;
if (field.FieldType.IsClass && field.FieldType != typeof(string))
{
try
{
var nested = Activator.CreateInstance(field.FieldType)!;
var mapped = MapObject(fromValue, nested);
RemoveReadOnly(field);
field.SetValue(newObject, mapped);
continue;
}
catch
{
// ignore
}
}
RemoveReadOnly(field);
field.SetValue(newObject, fromValue);
}
var classPostProcessing = typeTo.GetCustomAttribute<PostProcessingAttribute>();
if (classPostProcessing is not null && classPostProcessing.MappingCallMethodName is not null)
{
var methodInfo = typeTo.GetMethod(classPostProcessing.MappingCallMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
methodInfo?.Invoke(newObject, []);
}
return newObject;
}
private static void RemoveReadOnly(FieldInfo field)
{
if (!field.IsInitOnly)
return;
var attr = typeof(FieldInfo).GetField("m_fieldAttributes", BindingFlags.Instance | BindingFlags.NonPublic);
if (attr != null)
{
var current = (FieldAttributes)attr.GetValue(field)!;
attr.SetValue(field, current & ~FieldAttributes.InitOnly);
}
}
public static To MapObject<To>(object obj) => MapObject(obj, Activator.CreateInstance<To>()!);
public static To? MapObjectWithNull<To>(object? obj) => obj is null ? default : MapObject(obj, Activator.CreateInstance<To>());
private static PropertyInfo? TryGetPropertyFrom(PropertyInfo propertyTo, PropertyInfo[] propertiesFrom)
{
var customAttribute = propertyTo.GetCustomAttributes<AlternativeNameAttribute>()?
.ToArray()
.FirstOrDefault(x => propertiesFrom.Any(y => y.Name == x.AlternativeName));
if (customAttribute is not null)
{
return propertiesFrom.FirstOrDefault(x => x.Name == customAttribute.AlternativeName);
}
return propertiesFrom.FirstOrDefault(x => x.Name == propertyTo.Name);
}
private static object? PostProcessing<T>(object? value, PostProcessingAttribute postProcessingAttribute, T newObject)
{
if (value is null || newObject is null)
{
return null;
}
if (!string.IsNullOrEmpty(postProcessingAttribute.MappingCallMethodName))
{
var methodInfo =
newObject.GetType().GetMethod(postProcessingAttribute.MappingCallMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
if (methodInfo is not null)
{
return methodInfo.Invoke(newObject, [value]);
}
}
else if (postProcessingAttribute.ActionType != PostProcessingType.None)
{
switch (postProcessingAttribute.ActionType)
{
case PostProcessingType.ToUniversalTime:
return ToUniversalTime(value);
case PostProcessingType.ToLocalTime:
return ToLocalTime(value);
}
}
return null;
}
private static object? ToLocalTime(object? obj)
{
if (obj is DateTime date)
return date.ToLocalTime();
return obj;
}
private static object? ToUniversalTime(object? obj)
{
if (obj is DateTime date)
return date.ToUniversalTime();
return obj;
}
private static void FindAndMapDefaultValue<T>(PropertyInfo property, T newObject)
{
var defaultValueAttribute = property.GetCustomAttribute<DefaultValueAttribute>();
if (defaultValueAttribute is null)
{
return;
}
if (defaultValueAttribute.DefaultValue is not null)
{
property.SetValue(newObject, defaultValueAttribute.DefaultValue);
return;
}
var value = defaultValueAttribute.Func switch
{
DefaultValueFunc.UtcNow => DateTime.UtcNow,
_ => (object?)null,
};
if (value is not null)
{
property.SetValue(newObject, value);
}
}
private static object? MapListOfObjects(PropertyInfo propertyTo, object list)
{
var listResult = Activator.CreateInstance(propertyTo.PropertyType);
var elementType = propertyTo.PropertyType.GenericTypeArguments[0];
foreach (var elem in (IEnumerable)list)
{
object? newElem;
if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal) || elementType == typeof(DateTime))
{
newElem = elem;
}
else
{
newElem = MapObject(elem, Activator.CreateInstance(elementType)!);
}
if (newElem is not null)
{
propertyTo.PropertyType.GetMethod("Add")!.Invoke(listResult, [newElem]);
}
}
return listResult;
}
}
enum DefaultValueFunc
{
None,
UtcNow
}

View File

@@ -0,0 +1,11 @@
namespace SPiluSZharuContracts.Mapper;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
class DefaultValueAttribute : Attribute
{
public object? DefaultValue { get; set; }
public string? FuncName { get; set; }
public DefaultValueFunc Func { get; set; } = DefaultValueFunc.None;
}

View File

@@ -0,0 +1,6 @@
namespace SPiluSZharuContracts.Mapper;
[AttributeUsage(AttributeTargets.Property)]
class IgnoreMappingAttribute : Attribute
{
}

View File

@@ -0,0 +1,9 @@
namespace SPiluSZharuContracts.Mapper;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Field)]
class PostProcessingAttribute : Attribute
{
public string? MappingCallMethodName { get; set; }
public PostProcessingType ActionType { get; set; } = PostProcessingType.None;
}

View File

@@ -0,0 +1,10 @@
namespace SPiluSZharuContracts.Mapper;
enum PostProcessingType
{
None = -1,
ToUniversalTime = 1,
ToLocalTime = 2
}

View File

@@ -0,0 +1,396 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Этот код создан программой.
// Исполняемая версия:4.0.30319.42000
//
// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае
// повторной генерации кода.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SPiluSZharuContracts.Resources {
using System;
/// <summary>
/// Класс ресурса со строгой типизацией для поиска локализованных строк и т.д.
/// </summary>
// Этот класс создан автоматически классом StronglyTypedResourceBuilder
// с помощью такого средства, как ResGen или Visual Studio.
// Чтобы добавить или удалить член, измените файл .ResX и снова запустите ResGen
// с параметром /str или перестройте свой проект VS.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Messages {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Messages() {
}
/// <summary>
/// Возвращает кэшированный экземпляр ResourceManager, использованный этим классом.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SPiluSZharuContracts.Resources.Messages", typeof(Messages).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Перезаписывает свойство CurrentUICulture текущего потока для всех
/// обращений к ресурсу с помощью этого класса ресурса со строгой типизацией.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Элемент по данным: {0} был удален.
/// </summary>
internal static string AdapterMessageElementDeletedException {
get {
return ResourceManager.GetString("AdapterMessageElementDeletedException", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Не найден элемент по данным: {0}.
/// </summary>
internal static string AdapterMessageElementNotFoundException {
get {
return ResourceManager.GetString("AdapterMessageElementNotFoundException", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Данные пусты.
/// </summary>
internal static string AdapterMessageEmptyDate {
get {
return ResourceManager.GetString("AdapterMessageEmptyDate", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Неправильные даты: {0}.
/// </summary>
internal static string AdapterMessageIncorrectDatesException {
get {
return ResourceManager.GetString("AdapterMessageIncorrectDatesException", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Ошибка при обработке данных: {0}.
/// </summary>
internal static string AdapterMessageInvalidOperationException {
get {
return ResourceManager.GetString("AdapterMessageInvalidOperationException", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Ошибка при работе с хранилищем данных: {0}.
/// </summary>
internal static string AdapterMessageStorageException {
get {
return ResourceManager.GetString("AdapterMessageStorageException", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Переданы неверные данные: {0}.
/// </summary>
internal static string AdapterMessageValidationException {
get {
return ResourceManager.GetString("AdapterMessageValidationException", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Товар.
/// </summary>
internal static string DocumentDocCaptionProduct {
get {
return ResourceManager.GetString("DocumentDocCaptionProduct", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Ресторан.
/// </summary>
internal static string DocumentDocCaptionRestaurant {
get {
return ResourceManager.GetString("DocumentDocCaptionRestaurant", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Продукты по ресторанам.
/// </summary>
internal static string DocumentDocHeader {
get {
return ResourceManager.GetString("DocumentDocHeader", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Сформировано на дату {0}.
/// </summary>
internal static string DocumentDocSubHeader {
get {
return ResourceManager.GetString("DocumentDocSubHeader", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Кол-во.
/// </summary>
internal static string DocumentExcelCaptionCount {
get {
return ResourceManager.GetString("DocumentExcelCaptionCount", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Дата.
/// </summary>
internal static string DocumentExcelCaptionDate {
get {
return ResourceManager.GetString("DocumentExcelCaptionDate", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Товар.
/// </summary>
internal static string DocumentExcelCaptionProduct {
get {
return ResourceManager.GetString("DocumentExcelCaptionProduct", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Сумма.
/// </summary>
internal static string DocumentExcelCaptionSum {
get {
return ResourceManager.GetString("DocumentExcelCaptionSum", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Всего.
/// </summary>
internal static string DocumentExcelCaptionTotal {
get {
return ResourceManager.GetString("DocumentExcelCaptionTotal", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Продажи за период.
/// </summary>
internal static string DocumentExcelHeader {
get {
return ResourceManager.GetString("DocumentExcelHeader", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на c {0} по {1}.
/// </summary>
internal static string DocumentExcelSubHeader {
get {
return ResourceManager.GetString("DocumentExcelSubHeader", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Начисления.
/// </summary>
internal static string DocumentPdfDiagramCaption {
get {
return ResourceManager.GetString("DocumentPdfDiagramCaption", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Зарплатная ведомость.
/// </summary>
internal static string DocumentPdfHeader {
get {
return ResourceManager.GetString("DocumentPdfHeader", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на за период с {0} по {1}.
/// </summary>
internal static string DocumentPdfSubHeader {
get {
return ResourceManager.GetString("DocumentPdfSubHeader", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Нельзя изменить удаленный элемент (идентификатор: {0}).
/// </summary>
internal static string ElementDeletedExceptionMessage {
get {
return ResourceManager.GetString("ElementDeletedExceptionMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Уже существует элемент со значением {0} параметра {1}.
/// </summary>
internal static string ElementExistsExceptionMessage {
get {
return ResourceManager.GetString("ElementExistsExceptionMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Элемент не найден по значению = {0}.
/// </summary>
internal static string ElementNotFoundExceptionMessage {
get {
return ResourceManager.GetString("ElementNotFoundExceptionMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Дата окончания должна быть позже даты начала. Дата начала: {0}. ​​Дата окончания: {1}.
/// </summary>
internal static string IncorrectDatesExceptionMessage {
get {
return ResourceManager.GetString("IncorrectDatesExceptionMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Недостаточно данных для обработки: {0}.
/// </summary>
internal static string NotEnoughDataToProcessExceptionMessage {
get {
return ResourceManager.GetString("NotEnoughDataToProcessExceptionMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Не найдены данные.
/// </summary>
internal static string NotFoundDataMessage {
get {
return ResourceManager.GetString("NotFoundDataMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Ошибка при работе в хранилище: {0}.
/// </summary>
internal static string StorageExceptionMessage {
get {
return ResourceManager.GetString("StorageExceptionMessage", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Дата трудоустройства не может быть раньше даты рождения ({0}, {1}).
/// </summary>
internal static string ValidationExceptionMessageEmploymentDateAndBirthDate {
get {
return ResourceManager.GetString("ValidationExceptionMessageEmploymentDateAndBirthDate", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Значение в поле {0} пусто.
/// </summary>
internal static string ValidationExceptionMessageEmptyField {
get {
return ResourceManager.GetString("ValidationExceptionMessageEmptyField", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Значение в поле Телефонный номер не является телефонным номером.
/// </summary>
internal static string ValidationExceptionMessageIncorrectPhoneNumber {
get {
return ResourceManager.GetString("ValidationExceptionMessageIncorrectPhoneNumber", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Значение в поле {0} меньше или равно 0.
/// </summary>
internal static string ValidationExceptionMessageLessOrEqualZero {
get {
return ResourceManager.GetString("ValidationExceptionMessageLessOrEqualZero", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Несовершеннолетние не могут быть приняты на работу (Дата рождения: {0}).
/// </summary>
internal static string ValidationExceptionMessageMinorsBirthDate {
get {
return ResourceManager.GetString("ValidationExceptionMessageMinorsBirthDate", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Несовершеннолетние не могут быть приняты на работу (Дата трудоустройства {0}, Дата рождения: {1}).
/// </summary>
internal static string ValidationExceptionMessageMinorsEmploymentDate {
get {
return ResourceManager.GetString("ValidationExceptionMessageMinorsEmploymentDate", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на В продаже должен быть хотя бы один товар.
/// </summary>
internal static string ValidationExceptionMessageNoProductsInSale {
get {
return ResourceManager.GetString("ValidationExceptionMessageNoProductsInSale", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Значение в поле {0} не является типом уникального идентификатора.
/// </summary>
internal static string ValidationExceptionMessageNotAId {
get {
return ResourceManager.GetString("ValidationExceptionMessageNotAId", resourceCulture);
}
}
/// <summary>
/// Ищет локализованную строку, похожую на Значение в поле {0} не проиницализировано.
/// </summary>
internal static string ValidationExceptionMessageNotInitialized {
get {
return ResourceManager.GetString("ValidationExceptionMessageNotInitialized", resourceCulture);
}
}
}
}

View File

@@ -0,0 +1,231 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AdapterMessageElementDeletedException" xml:space="preserve">
<value>Element nach Daten: {0} wurde gelöscht</value>
</data>
<data name="AdapterMessageElementNotFoundException" xml:space="preserve">
<value>Element nach Daten nicht gefunden: {0}</value>
</data>
<data name="AdapterMessageEmptyDate" xml:space="preserve">
<value>Daten sind leer</value>
</data>
<data name="AdapterMessageIncorrectDatesException" xml:space="preserve">
<value>Falsche Daten: {0}</value>
</data>
<data name="AdapterMessageInvalidOperationException" xml:space="preserve">
<value>Fehler bei der Verarbeitung von Daten: {0}</value>
</data>
<data name="AdapterMessageStorageException" xml:space="preserve">
<value>Fehler beim Arbeiten mit der Datenspeicherung: {0}</value>
</data>
<data name="AdapterMessageValidationException" xml:space="preserve">
<value>Falsch übermittelte Daten: {0}</value>
</data>
<data name="DocumentDocCaptionProduct" xml:space="preserve">
<value>Produkt</value>
</data>
<data name="DocumentDocCaptionRestaurant" xml:space="preserve">
<value>Restaurant</value>
</data>
<data name="DocumentDocHeader" xml:space="preserve">
<value>Produkte nach Hersteller</value>
</data>
<data name="DocumentDocSubHeader" xml:space="preserve">
<value>Generiert am Datum {0}</value>
</data>
<data name="DocumentExcelCaptionCount" xml:space="preserve">
<value>Zählen</value>
</data>
<data name="DocumentExcelCaptionDate" xml:space="preserve">
<value>Datum</value>
</data>
<data name="DocumentExcelCaptionProduct" xml:space="preserve">
<value>Produkt</value>
</data>
<data name="DocumentExcelCaptionSum" xml:space="preserve">
<value>Summe</value>
</data>
<data name="DocumentExcelCaptionTotal" xml:space="preserve">
<value>Insgesamt</value>
</data>
<data name="DocumentExcelHeader" xml:space="preserve">
<value>Verkäufe für den Zeitraum</value>
</data>
<data name="DocumentExcelSubHeader" xml:space="preserve">
<value>von {0} bis {1}</value>
</data>
<data name="DocumentPdfDiagramCaption" xml:space="preserve">
<value>Ckstellungen</value>
</data>
<data name="DocumentPdfHeader" xml:space="preserve">
<value>Gehaltsabrechnung</value>
</data>
<data name="DocumentPdfSubHeader" xml:space="preserve">
<value>für den Zeitraum von {0} bis {1}</value>
</data>
<data name="ElementDeletedExceptionMessage" xml:space="preserve">
<value>Gelöschtes Element kann nicht geändert werden (ID: {0})</value>
</data>
<data name="ElementExistsExceptionMessage" xml:space="preserve">
<value>Es gibt bereits ein Element mit dem Wert {0} des Parameters {1}</value>
</data>
<data name="ElementNotFoundExceptionMessage" xml:space="preserve">
<value>Element bei Wert = {0} nicht gefunden</value>
</data>
<data name="IncorrectDatesExceptionMessage" xml:space="preserve">
<value>Das Enddatum muss nach dem Startdatum liegen. Anfangsdatum: {0}. Enddatum: {1}</value>
</data>
<data name="NotEnoughDataToProcessExceptionMessage" xml:space="preserve">
<value>Nicht genügend Daten zum Verarbeiten: {0}</value>
</data>
<data name="NotFoundDataMessage" xml:space="preserve">
<value>Keine Daten gefunden</value>
</data>
<data name="StorageExceptionMessage" xml:space="preserve">
<value>Fehler beim Arbeiten im Speicher: {0}</value>
</data>
<data name="ValidationExceptionMessageEmploymentDateAndBirthDate" xml:space="preserve">
<value>Das Beschäftigungsdatum darf nicht vor dem Geburtsdatum liegen ({0}, {1})</value>
</data>
<data name="ValidationExceptionMessageEmptyField" xml:space="preserve">
<value>Der Wert im Feld {0} ist leer</value>
</data>
<data name="ValidationExceptionMessageIncorrectPhoneNumber" xml:space="preserve">
<value>Der Wert im Feld Telefonnummer ist keine Telefonnummer.</value>
</data>
<data name="ValidationExceptionMessageLessOrEqualZero" xml:space="preserve">
<value>Der Wert im Feld {0} ist kleiner oder gleich 0</value>
</data>
<data name="ValidationExceptionMessageMinorsBirthDate" xml:space="preserve">
<value>Minderjährige können nicht eingestellt werden (Geburtsdatum = {0})</value>
</data>
<data name="ValidationExceptionMessageMinorsEmploymentDate" xml:space="preserve">
<value>Minderjährige können nicht eingestellt werden (Beschäftigungsdatum: {0}, Geburtsdatum {1})</value>
</data>
<data name="ValidationExceptionMessageNoProductsInSale" xml:space="preserve">
<value>Es muss mindestens ein Artikel im Angebot sein</value>
</data>
<data name="ValidationExceptionMessageNotAId" xml:space="preserve">
<value>Der Wert im Feld {0} ist kein eindeutiger Bezeichnertyp.</value>
</data>
<data name="ValidationExceptionMessageNotInitialized" xml:space="preserve">
<value>Der Wert im Feld {0} ist nicht initialisiert</value>
</data>
</root>

View File

@@ -0,0 +1,231 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AdapterMessageElementDeletedException" xml:space="preserve">
<value>Element by data: {0} was deleted</value>
</data>
<data name="AdapterMessageElementNotFoundException" xml:space="preserve">
<value>Not found element by data: {0}</value>
</data>
<data name="AdapterMessageEmptyDate" xml:space="preserve">
<value>Data is empty</value>
</data>
<data name="AdapterMessageIncorrectDatesException" xml:space="preserve">
<value>Incorrect dates: {0}</value>
</data>
<data name="AdapterMessageInvalidOperationException" xml:space="preserve">
<value>Error processing data: {0}</value>
</data>
<data name="AdapterMessageStorageException" xml:space="preserve">
<value>Error while working with data storage: {0}</value>
</data>
<data name="AdapterMessageValidationException" xml:space="preserve">
<value>Incorrect data transmitted: {0}</value>
</data>
<data name="DocumentDocCaptionProduct" xml:space="preserve">
<value>Product</value>
</data>
<data name="DocumentDocCaptionRestaurant" xml:space="preserve">
<value>Restaurant</value>
</data>
<data name="DocumentDocHeader" xml:space="preserve">
<value>Products by Manufacturer</value>
</data>
<data name="DocumentDocSubHeader" xml:space="preserve">
<value>Generated on date {0}</value>
</data>
<data name="DocumentExcelCaptionCount" xml:space="preserve">
<value>Count</value>
</data>
<data name="DocumentExcelCaptionDate" xml:space="preserve">
<value>Date</value>
</data>
<data name="DocumentExcelCaptionProduct" xml:space="preserve">
<value>Product</value>
</data>
<data name="DocumentExcelCaptionSum" xml:space="preserve">
<value>Sum</value>
</data>
<data name="DocumentExcelCaptionTotal" xml:space="preserve">
<value>Total</value>
</data>
<data name="DocumentExcelHeader" xml:space="preserve">
<value>Sales for the period</value>
</data>
<data name="DocumentExcelSubHeader" xml:space="preserve">
<value>from {0} to {1}</value>
</data>
<data name="DocumentPdfDiagramCaption" xml:space="preserve">
<value>Accruals</value>
</data>
<data name="DocumentPdfHeader" xml:space="preserve">
<value>Payroll</value>
</data>
<data name="DocumentPdfSubHeader" xml:space="preserve">
<value>for the period from {0} to {1}</value>
</data>
<data name="ElementDeletedExceptionMessage" xml:space="preserve">
<value>Cannot modify a deleted item (id: {0})</value>
</data>
<data name="ElementExistsExceptionMessage" xml:space="preserve">
<value>There is already an element with value {0} of parameter {1}</value>
</data>
<data name="ElementNotFoundExceptionMessage" xml:space="preserve">
<value>Element not found at value = {0}</value>
</data>
<data name="IncorrectDatesExceptionMessage" xml:space="preserve">
<value>The end date must be later than the start date. StartDate: {0}. EndDate: {1}</value>
</data>
<data name="NotEnoughDataToProcessExceptionMessage" xml:space="preserve">
<value>Not enough data to process: {0}</value>
</data>
<data name="NotFoundDataMessage" xml:space="preserve">
<value>No data found</value>
</data>
<data name="StorageExceptionMessage" xml:space="preserve">
<value>Error while working in storage: {0}</value>
</data>
<data name="ValidationExceptionMessageEmploymentDateAndBirthDate" xml:space="preserve">
<value>Date of employment cannot be earlier than date of birth ({0}, {1})</value>
</data>
<data name="ValidationExceptionMessageEmptyField" xml:space="preserve">
<value>The value in field {0} is empty</value>
</data>
<data name="ValidationExceptionMessageIncorrectPhoneNumber" xml:space="preserve">
<value>The value in the Phone Number field is not a phone number.</value>
</data>
<data name="ValidationExceptionMessageLessOrEqualZero" xml:space="preserve">
<value>The value in field {0} is less than or equal to 0</value>
</data>
<data name="ValidationExceptionMessageMinorsBirthDate" xml:space="preserve">
<value>Minors cannot be hired (BirthDate = {0})</value>
</data>
<data name="ValidationExceptionMessageMinorsEmploymentDate" xml:space="preserve">
<value>Minors cannot be hired (EmploymentDate: {0}, BirthDate {1})</value>
</data>
<data name="ValidationExceptionMessageNoProductsInSale" xml:space="preserve">
<value>There must be at least one item on sale</value>
</data>
<data name="ValidationExceptionMessageNotAId" xml:space="preserve">
<value>The value in the {0} field is not a unique identifier type.</value>
</data>
<data name="ValidationExceptionMessageNotInitialized" xml:space="preserve">
<value>The value in field {0} is not initialized</value>
</data>
</root>

View File

@@ -0,0 +1,231 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AdapterMessageElementDeletedException" xml:space="preserve">
<value>Элемент по данным: {0} был удален</value>
</data>
<data name="AdapterMessageElementNotFoundException" xml:space="preserve">
<value>Не найден элемент по данным: {0}</value>
</data>
<data name="AdapterMessageEmptyDate" xml:space="preserve">
<value>Данные пусты</value>
</data>
<data name="AdapterMessageIncorrectDatesException" xml:space="preserve">
<value>Неправильные даты: {0}</value>
</data>
<data name="AdapterMessageInvalidOperationException" xml:space="preserve">
<value>Ошибка при обработке данных: {0}</value>
</data>
<data name="AdapterMessageStorageException" xml:space="preserve">
<value>Ошибка при работе с хранилищем данных: {0}</value>
</data>
<data name="AdapterMessageValidationException" xml:space="preserve">
<value>Переданы неверные данные: {0}</value>
</data>
<data name="DocumentDocCaptionProduct" xml:space="preserve">
<value>Товар</value>
</data>
<data name="DocumentDocCaptionRestaurant" xml:space="preserve">
<value>Ресторан</value>
</data>
<data name="DocumentDocHeader" xml:space="preserve">
<value>Продукты по ресторанам</value>
</data>
<data name="DocumentDocSubHeader" xml:space="preserve">
<value>Сформировано на дату {0}</value>
</data>
<data name="DocumentExcelCaptionCount" xml:space="preserve">
<value>Кол-во</value>
</data>
<data name="DocumentExcelCaptionDate" xml:space="preserve">
<value>Дата</value>
</data>
<data name="DocumentExcelCaptionProduct" xml:space="preserve">
<value>Товар</value>
</data>
<data name="DocumentExcelCaptionSum" xml:space="preserve">
<value>Сумма</value>
</data>
<data name="DocumentExcelCaptionTotal" xml:space="preserve">
<value>Всего</value>
</data>
<data name="DocumentExcelHeader" xml:space="preserve">
<value>Продажи за период</value>
</data>
<data name="DocumentExcelSubHeader" xml:space="preserve">
<value>c {0} по {1}</value>
</data>
<data name="DocumentPdfDiagramCaption" xml:space="preserve">
<value>Начисления</value>
</data>
<data name="DocumentPdfHeader" xml:space="preserve">
<value>Зарплатная ведомость</value>
</data>
<data name="DocumentPdfSubHeader" xml:space="preserve">
<value>за период с {0} по {1}</value>
</data>
<data name="ElementDeletedExceptionMessage" xml:space="preserve">
<value>Нельзя изменить удаленный элемент (идентификатор: {0})</value>
</data>
<data name="ElementExistsExceptionMessage" xml:space="preserve">
<value>Уже существует элемент со значением {0} параметра {1}</value>
</data>
<data name="ElementNotFoundExceptionMessage" xml:space="preserve">
<value>Элемент не найден по значению = {0}</value>
</data>
<data name="IncorrectDatesExceptionMessage" xml:space="preserve">
<value>Дата окончания должна быть позже даты начала. Дата начала: {0}. ​​Дата окончания: {1}</value>
</data>
<data name="NotEnoughDataToProcessExceptionMessage" xml:space="preserve">
<value>Недостаточно данных для обработки: {0}</value>
</data>
<data name="NotFoundDataMessage" xml:space="preserve">
<value>Не найдены данные</value>
</data>
<data name="StorageExceptionMessage" xml:space="preserve">
<value>Ошибка при работе в хранилище: {0}</value>
</data>
<data name="ValidationExceptionMessageEmploymentDateAndBirthDate" xml:space="preserve">
<value>Дата трудоустройства не может быть раньше даты рождения ({0}, {1})</value>
</data>
<data name="ValidationExceptionMessageEmptyField" xml:space="preserve">
<value>Значение в поле {0} пусто</value>
</data>
<data name="ValidationExceptionMessageIncorrectPhoneNumber" xml:space="preserve">
<value>Значение в поле Телефонный номер не является телефонным номером</value>
</data>
<data name="ValidationExceptionMessageLessOrEqualZero" xml:space="preserve">
<value>Значение в поле {0} меньше или равно 0</value>
</data>
<data name="ValidationExceptionMessageMinorsBirthDate" xml:space="preserve">
<value>Несовершеннолетние не могут быть приняты на работу (Дата рождения: {0})</value>
</data>
<data name="ValidationExceptionMessageMinorsEmploymentDate" xml:space="preserve">
<value>Несовершеннолетние не могут быть приняты на работу (Дата трудоустройства {0}, Дата рождения: {1})</value>
</data>
<data name="ValidationExceptionMessageNoProductsInSale" xml:space="preserve">
<value>В продаже должен быть хотя бы один товар</value>
</data>
<data name="ValidationExceptionMessageNotAId" xml:space="preserve">
<value>Значение в поле {0} не является типом уникального идентификатора</value>
</data>
<data name="ValidationExceptionMessageNotInitialized" xml:space="preserve">
<value>Значение в поле {0} не проиницализировано</value>
</data>
</root>

View File

@@ -9,8 +9,32 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageReference Include="Moq" Version="4.20.72" />
</ItemGroup>
<ItemGroup>
<Compile Update="Resources\Messages.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Messages.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resources\Messages.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Messages.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="SPiluSZharuBuisnessLogic" />
<InternalsVisibleTo Include="SPiluSZharuDatabase" />
<InternalsVisibleTo Include="SPiluSZharuWebApi" />
<InternalsVisibleTo Include="SPiluSZharuTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
</Project>

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.StorageContracts;
public interface IPostStorageContract
internal interface IPostStorageContract
{
List<PostDataModel> GetList();

View File

@@ -2,9 +2,11 @@
namespace SPiluSZharuContracts.StorageContracts;
public interface IProductStorageContract
internal interface IProductStorageContract
{
List<ProductDataModel> GetList(bool onlyActive = true);
List<ProductDataModel> GetList(bool onlyActive = true, string? restaurantId = null);
Task<List<ProductDataModel>> GetListAsync(CancellationToken ct);
List<ProductHistoryDataModel> GetHistoryByProductId(string productId);

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.StorageContracts;
public interface IRestaurantStorageContract
internal interface IRestaurantStorageContract
{
List<RestaurantDataModel> GetList();

View File

@@ -2,9 +2,11 @@
namespace SPiluSZharuContracts.StorageContracts;
public interface ISalaryStorageContract
internal interface ISalaryStorageContract
{
List<SalaryDataModel> GetList(DateTime? startDate, DateTime? endDate, string? workerId = null);
Task<List<SalaryDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct);
void AddElement(SalaryDataModel salaryDataModel);
}

View File

@@ -2,11 +2,13 @@
namespace SPiluSZharuContracts.StorageContracts;
public interface ISaleStorageContract
internal interface ISaleStorageContract
{
List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null,
string? workerId = null, string? restaurantId = null, string? productId = null);
Task<List<SaleDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct);
SaleDataModel? GetElementById(string id);
void AddElement(SaleDataModel saleDataModel);

View File

@@ -2,7 +2,7 @@
namespace SPiluSZharuContracts.StorageContracts;
public interface IWorkerStorageContract
internal interface IWorkerStorageContract
{
List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null,
DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
@@ -18,4 +18,6 @@ public interface IWorkerStorageContract
void UpdElement(WorkerDataModel workerDataModel);
void DelElement(string id);
int GetWorkerTrend(DateTime fromPeriod, DateTime toPeriod);
}

View File

@@ -1,12 +1,27 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using SPiluSZharuContracts.Mapper;
using System.Text.Json;
namespace SPiluSZharuContracts.ViewModels;
public class PostViewModel
{
[AlternativeName("PostId")]
public required string Id { get; set; }
public required string PostName { get; set; }
public required string PostType { get; set; }
public double Salary { get; set; }
[AlternativeName("ConfigurationModel")]
[PostProcessing(MappingCallMethodName = "ParseConfiguration")]
public required string Configuration { get; set; }
private string ParseConfiguration(PostConfiguration? model)
{
if (model == null)
return string.Empty;
return JsonSerializer.Serialize(model, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
}

View File

@@ -1,4 +1,6 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class ProductHistoryViewModel
{
@@ -6,5 +8,6 @@ public class ProductHistoryViewModel
public double OldPrice { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime ChangeDate { get; set; }
}

View File

@@ -1,11 +1,18 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class ProductViewModel
{
[AlternativeName("ProductId")]
public required string Id { get; set; }
public required string ProductName { get; set; }
public required string RestaurantId { get; set; }
public required string RestaurantName { get; set; }
public required string ProductType { get; set; }
public double Price { get; set; }

View File

@@ -0,0 +1,8 @@
namespace SPiluSZharuContracts.ViewModels;
public class RestaurantProductViewModel
{
public required string RestaurantName { get; set; }
public required List<string> Products { get; set; }
}

View File

@@ -1,7 +1,10 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class RestaurantViewModel
{
[AlternativeName("RestaurantId")]
public required string Id { get; set; }
public required string RestaurantName { get; set; }

View File

@@ -1,4 +1,6 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class SalaryViewModel
{
@@ -6,7 +8,9 @@ public class SalaryViewModel
public required string WorkerFIO { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime SalaryDate { get; set; }
[AlternativeName("WorkerSalary")]
public double Salary { get; set; }
}

View File

@@ -1,7 +1,10 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class SaleViewModel
{
[AlternativeName("SaleId")]
public required string Id { get; set; }
public required string WorkerId { get; set; }
@@ -12,6 +15,7 @@ public class SaleViewModel
public string? RestaurantName { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime SaleDate { get; set; }
public double Sum { get; set; }

View File

@@ -0,0 +1,16 @@
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class WorkerSalaryByPeriodViewModel
{
public required string WorkerFIO { get; set; }
public double TotalSalary { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime FromPeriod { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime ToPeriod { get; set; }
}

View File

@@ -1,7 +1,10 @@
namespace SPiluSZharuContracts.ViewModels;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuContracts.ViewModels;
public class WorkerViewModel
{
[AlternativeName("WorkerId")]
public required string Id { get; set; }
public required string FIO { get; set; }
@@ -14,7 +17,9 @@ public class WorkerViewModel
public bool IsDeleted { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime BirthDate { get; set; }
[PostProcessing(ActionType = PostProcessingType.ToLocalTime)]
public DateTime EmploymentDate { get; set; }
}

View File

@@ -0,0 +1,8 @@
using SPiluSZharuContracts.Infrastructure;
namespace SPiluSZharuDatabase;
class DefaultConfigurationDatabase : IConfigurationDatabase
{
public string ConnectionString => "";
}

View File

@@ -1,44 +1,30 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Npgsql;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuDatabase.Models;
namespace SPiluSZharuDatabase.Implementations;
internal class PostStorageContract : IPostStorageContract
internal class PostStorageContract(SPiluSZharuDbContext dbContext, IStringLocalizer<Messages> localizer) : IPostStorageContract
{
private readonly SPiluSZharuDbContext _dbContext;
private readonly Mapper _mapper;
public PostStorageContract(SPiluSZharuDbContext dbContext)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Post, PostDataModel>()
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
cfg.CreateMap<PostDataModel, Post>()
.ForMember(x => x.Id, x => x.Ignore())
.ForMember(x => x.PostId, x => x.MapFrom(src => src.Id))
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
});
_mapper = new Mapper(config);
}
private readonly SPiluSZharuDbContext _dbContext = dbContext;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<PostDataModel> GetList()
{
try
{
return [.. _dbContext.Posts.Select(x => _mapper.Map<PostDataModel>(x))];
return [.. _dbContext.Posts.Select(x => CustomMapper.MapObject<PostDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -46,12 +32,12 @@ internal class PostStorageContract : IPostStorageContract
{
try
{
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => CustomMapper.MapObject<PostDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -59,12 +45,12 @@ internal class PostStorageContract : IPostStorageContract
{
try
{
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
return CustomMapper.MapObjectWithNull<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -72,12 +58,12 @@ internal class PostStorageContract : IPostStorageContract
{
try
{
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
return CustomMapper.MapObjectWithNull<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -85,23 +71,27 @@ internal class PostStorageContract : IPostStorageContract
{
try
{
_dbContext.Posts.Add(_mapper.Map<Post>(postDataModel));
var post = MapToEntity(postDataModel);
post.IsActual = true;
post.ChangeDate = DateTime.UtcNow;
_dbContext.Posts.Add(post);
_dbContext.SaveChanges();
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("PostName", postDataModel.PostName);
throw new ElementExistsException("PostName", postDataModel.PostName, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("PostId", postDataModel.Id);
throw new ElementExistsException("PostId", postDataModel.Id, _localizer);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -112,14 +102,16 @@ internal class PostStorageContract : IPostStorageContract
var transaction = _dbContext.Database.BeginTransaction();
try
{
var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id);
var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id, _localizer);
if (!element.IsActual)
{
throw new ElementDeletedException(postDataModel.Id);
throw new ElementDeletedException(postDataModel.Id, _localizer);
}
element.IsActual = false;
_dbContext.SaveChanges();
var newElement = _mapper.Map<Post>(postDataModel);
var newElement = MapToEntity(postDataModel);
newElement.IsActual = true;
newElement.ChangeDate = DateTime.UtcNow;
_dbContext.Posts.Add(newElement);
_dbContext.SaveChanges();
transaction.Commit();
@@ -133,7 +125,7 @@ internal class PostStorageContract : IPostStorageContract
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("PostName", postDataModel.PostName);
throw new ElementExistsException("PostName", postDataModel.PostName, _localizer);
}
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
{
@@ -143,7 +135,7 @@ internal class PostStorageContract : IPostStorageContract
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -151,10 +143,10 @@ internal class PostStorageContract : IPostStorageContract
{
try
{
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
var element = GetPostById(id) ?? throw new ElementNotFoundException(id, _localizer);
if (!element.IsActual)
{
throw new ElementDeletedException(id);
throw new ElementDeletedException(id, _localizer);
}
element.IsActual = false;
_dbContext.SaveChanges();
@@ -170,7 +162,7 @@ internal class PostStorageContract : IPostStorageContract
{
try
{
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
var element = GetPostById(id) ?? throw new ElementNotFoundException(id, _localizer);
element.IsActual = true;
_dbContext.SaveChanges();
}
@@ -181,5 +173,13 @@ internal class PostStorageContract : IPostStorageContract
}
}
private Post MapToEntity(PostDataModel model)
{
var post = CustomMapper.MapObject<Post>(model);
post.PostId = model.Id;
post.Configuration = model.ConfigurationModel;
return post;
}
private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
}

View File

@@ -1,46 +1,52 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Npgsql;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuDatabase.Models;
namespace SPiluSZharuDatabase.Implementations;
internal class ProductStorageContract : IProductStorageContract
internal class ProductStorageContract(SPiluSZharuDbContext dbContext, IStringLocalizer<Messages> localizer) : IProductStorageContract
{
private readonly SPiluSZharuDbContext _dbContext;
private readonly Mapper _mapper;
private readonly SPiluSZharuDbContext _dbContext = dbContext;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public ProductStorageContract(SPiluSZharuDbContext dbContext)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Product, ProductDataModel>();
cfg.CreateMap<ProductDataModel, Product>()
.ForMember(x => x.IsDeleted, x => x.MapFrom(src => false));
cfg.CreateMap<ProductHistory, ProductHistoryDataModel>();
});
_mapper = new Mapper(config);
}
public List<ProductDataModel> GetList(bool onlyActive = true)
public List<ProductDataModel> GetList(bool onlyActive = true, string? restaurantId = null)
{
try
{
var query = _dbContext.Products.AsQueryable();
var query = _dbContext.Products.Include(x => x.Restaurant).AsQueryable();
if (onlyActive)
{
query = query.Where(x => !x.IsDeleted);
}
return [.. query.Select(x => _mapper.Map<ProductDataModel>(x))];
if (restaurantId is not null)
{
query = query.Where(x => x.RestaurantId == restaurantId);
}
return [.. query.Select(x => CustomMapper.MapObject<ProductDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
public async Task<List<ProductDataModel>> GetListAsync(CancellationToken ct)
{
try
{
return [.. await _dbContext.Products.Include(x => x.Restaurant).Select(x => CustomMapper.MapObject<ProductDataModel>(x)).ToListAsync(ct)];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex, _localizer);
}
}
@@ -48,12 +54,12 @@ internal class ProductStorageContract : IProductStorageContract
{
try
{
return [.. _dbContext.ProductHistories.Include(x => x.Product).Where(x => x.ProductId == productId).OrderByDescending(x => x.ChangeDate).Select(x => _mapper.Map<ProductHistoryDataModel>(x))];
return [.. _dbContext.ProductHistories.Include(x => x.Product).Where(x => x.ProductId == productId).OrderByDescending(x => x.ChangeDate).Select(x => CustomMapper.MapObject<ProductHistoryDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -61,12 +67,12 @@ internal class ProductStorageContract : IProductStorageContract
{
try
{
return _mapper.Map<ProductDataModel>(GetProductById(id));
return CustomMapper.MapObjectWithNull<ProductDataModel>(GetProductById(id));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -74,12 +80,12 @@ internal class ProductStorageContract : IProductStorageContract
{
try
{
return _mapper.Map<ProductDataModel>(_dbContext.Products.FirstOrDefault(x => x.ProductName == name && !x.IsDeleted));
return CustomMapper.MapObjectWithNull<ProductDataModel>(_dbContext.Products.Include(x => x.Restaurant).FirstOrDefault(x => x.ProductName == name && !x.IsDeleted));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -87,23 +93,30 @@ internal class ProductStorageContract : IProductStorageContract
{
try
{
_dbContext.Products.Add(_mapper.Map<Product>(productDataModel));
var product = CustomMapper.MapObject<Product>(productDataModel);
product.IsDeleted = false;
_dbContext.Products.Add(product);
_dbContext.SaveChanges();
}
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", productDataModel.Id);
throw new ElementExistsException("Id", productDataModel.Id, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Products_ProductName_IsDeleted" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("ProductName", productDataModel.ProductName);
throw new ElementExistsException("ProductName", productDataModel.ProductName, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "PK_Products" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", productDataModel.Id, _localizer);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -114,13 +127,15 @@ internal class ProductStorageContract : IProductStorageContract
var transaction = _dbContext.Database.BeginTransaction();
try
{
var element = GetProductById(productDataModel.Id) ?? throw new ElementNotFoundException(productDataModel.Id);
var element = GetProductById(productDataModel.Id) ?? throw new ElementNotFoundException(productDataModel.Id, _localizer);
if (element.Price != productDataModel.Price)
{
_dbContext.ProductHistories.Add(new ProductHistory() { ProductId = element.Id, OldPrice = element.Price });
_dbContext.SaveChanges();
}
_dbContext.Products.Update(_mapper.Map(productDataModel, element));
var product = CustomMapper.MapObject<Product>(productDataModel, element);
product.IsDeleted = false;
_dbContext.Products.Update(product);
_dbContext.SaveChanges();
transaction.Commit();
}
@@ -133,7 +148,7 @@ internal class ProductStorageContract : IProductStorageContract
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Products_ProductName_IsDeleted" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("ProductName", productDataModel.ProductName);
throw new ElementExistsException("ProductName", productDataModel.ProductName, _localizer);
}
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
{
@@ -143,7 +158,7 @@ internal class ProductStorageContract : IProductStorageContract
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -151,7 +166,7 @@ internal class ProductStorageContract : IProductStorageContract
{
try
{
var element = GetProductById(id) ?? throw new ElementNotFoundException(id);
var element = GetProductById(id) ?? throw new ElementNotFoundException(id, _localizer);
element.IsDeleted = true;
_dbContext.SaveChanges();
}
@@ -163,9 +178,9 @@ internal class ProductStorageContract : IProductStorageContract
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
private Product? GetProductById(string id) => _dbContext.Products.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
private Product? GetProductById(string id) => _dbContext.Products.Include(x => x.Restaurant).FirstOrDefault(x => x.Id == id && !x.IsDeleted);
}

View File

@@ -1,8 +1,10 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Npgsql;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuDatabase.Models;
@@ -11,28 +13,24 @@ namespace SPiluSZharuDatabase.Implementations;
internal class RestaurantStorageContract : IRestaurantStorageContract
{
private readonly SPiluSZharuDbContext _dbContext;
private readonly Mapper _mapper;
private readonly IStringLocalizer<Messages> _localizer;
public RestaurantStorageContract(SPiluSZharuDbContext dbContext)
public RestaurantStorageContract(SPiluSZharuDbContext dbContext, IStringLocalizer<Messages> localizer)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(SPiluSZharuDbContext).Assembly);
});
_mapper = new Mapper(config);
_localizer = localizer;
}
public List<RestaurantDataModel> GetList()
{
try
{
return [.. _dbContext.Restaurants.Select(x => _mapper.Map<RestaurantDataModel>(x))];
return [.. _dbContext.Restaurants.Select(x => CustomMapper.MapObject<RestaurantDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -40,12 +38,12 @@ internal class RestaurantStorageContract : IRestaurantStorageContract
{
try
{
return _mapper.Map<RestaurantDataModel>(GetRestaurantById(id));
return CustomMapper.MapObjectWithNull<RestaurantDataModel>(GetRestaurantById(id));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -53,12 +51,12 @@ internal class RestaurantStorageContract : IRestaurantStorageContract
{
try
{
return _mapper.Map<RestaurantDataModel>(_dbContext.Restaurants.FirstOrDefault(x => x.RestaurantName == name));
return CustomMapper.MapObjectWithNull<RestaurantDataModel>(_dbContext.Restaurants.FirstOrDefault(x => x.RestaurantName == name));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -66,50 +64,55 @@ internal class RestaurantStorageContract : IRestaurantStorageContract
{
try
{
return _mapper.Map<RestaurantDataModel>(_dbContext.Restaurants.FirstOrDefault(x => x.PrevRestaurantName == name ||
return CustomMapper.MapObjectWithNull<RestaurantDataModel>(_dbContext.Restaurants.FirstOrDefault(x => x.PrevRestaurantName == name ||
x.PrevPrevRestaurantName == name));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
public void AddElement(RestaurantDataModel RestaurantDataModel)
public void AddElement(RestaurantDataModel restaurantDataModel)
{
try
{
_dbContext.Restaurants.Add(_mapper.Map<Restaurant>(RestaurantDataModel));
_dbContext.Restaurants.Add(CustomMapper.MapObject<Restaurant>(restaurantDataModel));
_dbContext.SaveChanges();
}
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", RestaurantDataModel.Id);
throw new ElementExistsException("Id", restaurantDataModel.Id, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Restaurants_RestaurantName" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("RestaurantName", RestaurantDataModel.RestaurantName);
throw new ElementExistsException("RestaurantName", restaurantDataModel.RestaurantName, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "PK_Restaurants" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", restaurantDataModel.Id, _localizer);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
public void UpdElement(RestaurantDataModel RestaurantDataModel)
public void UpdElement(RestaurantDataModel restaurantDataModel)
{
try
{
var element = GetRestaurantById(RestaurantDataModel.Id) ?? throw new ElementNotFoundException(RestaurantDataModel.Id);
if (element.RestaurantName != RestaurantDataModel.RestaurantName)
var element = GetRestaurantById(restaurantDataModel.Id) ?? throw new ElementNotFoundException(restaurantDataModel.Id, _localizer);
if (element.RestaurantName != restaurantDataModel.RestaurantName)
{
element.PrevPrevRestaurantName = element.PrevRestaurantName;
element.PrevRestaurantName = element.RestaurantName;
element.RestaurantName = RestaurantDataModel.RestaurantName;
element.RestaurantName = restaurantDataModel.RestaurantName;
}
_dbContext.Restaurants.Update(element);
_dbContext.SaveChanges();
@@ -122,12 +125,12 @@ internal class RestaurantStorageContract : IRestaurantStorageContract
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Restaurants_RestaurantName" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("RestaurantName", RestaurantDataModel.RestaurantName);
throw new ElementExistsException("RestaurantName", restaurantDataModel.RestaurantName, _localizer);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -135,7 +138,7 @@ internal class RestaurantStorageContract : IRestaurantStorageContract
{
try
{
var element = GetRestaurantById(id) ?? throw new ElementNotFoundException(id);
var element = GetRestaurantById(id) ?? throw new ElementNotFoundException(id, _localizer);
_dbContext.Restaurants.Remove(element);
_dbContext.SaveChanges();
}
@@ -147,7 +150,7 @@ internal class RestaurantStorageContract : IRestaurantStorageContract
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}

View File

@@ -1,29 +1,18 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuDatabase.Models;
namespace SPiluSZharuDatabase.Implementations;
internal class SalaryStorageContract : ISalaryStorageContract
internal class SalaryStorageContract(SPiluSZharuDbContext dbContext, IStringLocalizer<Messages> localizer) : ISalaryStorageContract
{
private readonly SPiluSZharuDbContext _dbContext;
private readonly Mapper _mapper;
public SalaryStorageContract(SPiluSZharuDbContext dbContext)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Worker, WorkerDataModel>();
cfg.CreateMap<Salary, SalaryDataModel>();
cfg.CreateMap<SalaryDataModel, Salary>()
.ForMember(dest => dest.WorkerSalary, opt => opt.MapFrom(src => src.Salary));
});
_mapper = new Mapper(config);
}
private readonly SPiluSZharuDbContext _dbContext = dbContext;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<SalaryDataModel> GetList(DateTime? startDate, DateTime? endDate, string? workerId = null)
{
@@ -36,12 +25,25 @@ internal class SalaryStorageContract : ISalaryStorageContract
query = query.Where(x => x.SalaryDate <= DateTime.SpecifyKind(endDate ?? DateTime.UtcNow, DateTimeKind.Utc));
if (workerId != null)
query = query.Where(x => x.WorkerId == workerId);
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
return [.. query.Select(x => CustomMapper.MapObject<SalaryDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
public async Task<List<SalaryDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct)
{
try
{
return [.. await _dbContext.Salaries.Include(x => x.Worker).Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate).Select(x => CustomMapper.MapObject<SalaryDataModel>(x)).ToListAsync(ct)];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex, _localizer);
}
}
@@ -49,13 +51,22 @@ internal class SalaryStorageContract : ISalaryStorageContract
{
try
{
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
var salary = MapToEntity(salaryDataModel);
_dbContext.Salaries.Add(salary);
_dbContext.SaveChanges();
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
private Salary MapToEntity(SalaryDataModel dataModel)
{
var salary = CustomMapper.MapObject<Salary>(dataModel);
salary.WorkerSalary = dataModel.Salary;
return salary;
}
}

View File

@@ -1,35 +1,18 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuDatabase.Models;
using System;
namespace SPiluSZharuDatabase.Implementations;
internal class SaleStorageContract : ISaleStorageContract
internal class SaleStorageContract(SPiluSZharuDbContext dbContext, IStringLocalizer<Messages> localizer) : ISaleStorageContract
{
private readonly SPiluSZharuDbContext _dbContext;
private readonly Mapper _mapper;
public SaleStorageContract(SPiluSZharuDbContext dbContext)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Restaurant, RestaurantDataModel>();
cfg.CreateMap<Product, ProductDataModel>();
cfg.CreateMap<Worker, WorkerDataModel>();
cfg.CreateMap<SaleProduct, SaleProductDataModel>();
cfg.CreateMap<SaleProductDataModel, SaleProduct>();
cfg.CreateMap<Sale, SaleDataModel>();
cfg.CreateMap<SaleDataModel, Sale>()
.ForMember(x => x.IsCancel, x => x.MapFrom(src => false))
.ForMember(x => x.SaleProducts, x => x.MapFrom(src => src.Products));
});
_mapper = new Mapper(config);
}
private readonly SPiluSZharuDbContext _dbContext = dbContext;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null, string? restaurantId = null, string? productId = null)
{
@@ -52,12 +35,25 @@ internal class SaleStorageContract : ISaleStorageContract
{
query = query.Where(x => x.SaleProducts!.Any(y => y.ProductId == productId));
}
return [.. query.Select(x => _mapper.Map<SaleDataModel>(x))];
return [.. query.Select(x => CustomMapper.MapObject<SaleDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
public async Task<List<SaleDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct)
{
try
{
return [.. await _dbContext.Sales.Include(x => x.SaleProducts)!.ThenInclude(x => x.Product).Where(x => x.SaleDate >= startDate && x.SaleDate < endDate).Select(x => CustomMapper.MapObject<SaleDataModel>(x)).ToListAsync(ct)];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex, _localizer);
}
}
@@ -65,12 +61,12 @@ internal class SaleStorageContract : ISaleStorageContract
{
try
{
return _mapper.Map<SaleDataModel>(GetSaleById(id));
return CustomMapper.MapObjectWithNull<SaleDataModel>(GetSaleById(id));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -78,13 +74,15 @@ internal class SaleStorageContract : ISaleStorageContract
{
try
{
_dbContext.Sales.Add(_mapper.Map<Sale>(saleDataModel));
var sale = MapToEntity(saleDataModel);
_dbContext.Sales.Add(sale);
_dbContext.SaveChanges();
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -92,10 +90,10 @@ internal class SaleStorageContract : ISaleStorageContract
{
try
{
var element = GetSaleById(id) ?? throw new ElementNotFoundException(id);
var element = GetSaleById(id) ?? throw new ElementNotFoundException(id, _localizer);
if (element.IsCancel)
{
throw new ElementDeletedException(id);
throw new ElementDeletedException(id, _localizer);
}
element.IsCancel = true;
_dbContext.SaveChanges();
@@ -108,9 +106,26 @@ internal class SaleStorageContract : ISaleStorageContract
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
private Sale MapToEntity(SaleDataModel dataModel)
{
var sale = CustomMapper.MapObject<Sale>(dataModel);
sale.IsCancel = false;
sale.SaleProducts = dataModel.Products?
.Select(p => new SaleProduct
{
ProductId = p.ProductId,
Count = p.Count,
Price = p.Price,
SaleId = sale.Id
})
.ToList();
return sale;
}
private Sale? GetSaleById(string id) => _dbContext.Sales.Include(x => x.Restaurant).Include(x => x.Worker).Include(x => x.SaleProducts)!.ThenInclude(x => x.Product).FirstOrDefault(x => x.Id == id);
}

View File

@@ -1,27 +1,19 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Npgsql;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Mapper;
using SPiluSZharuContracts.Resources;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuDatabase.Models;
namespace SPiluSZharuDatabase.Implementations;
internal class WorkerStorageContract : IWorkerStorageContract
internal class WorkerStorageContract(SPiluSZharuDbContext dbContext, IStringLocalizer<Messages> localizer) : IWorkerStorageContract
{
private readonly SPiluSZharuDbContext _dbContext;
private readonly Mapper _mapper;
public WorkerStorageContract(SPiluSZharuDbContext dbContext)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Post, PostDataModel>()
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
cfg.AddMaps(typeof(SPiluSZharuDbContext).Assembly);
});
_mapper = new Mapper(config);
}
private readonly SPiluSZharuDbContext _dbContext = dbContext;
private readonly IStringLocalizer<Messages> _localizer = localizer;
public List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
{
@@ -44,12 +36,12 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
}
return [.. JoinPost(query).Select(x => _mapper.Map<WorkerDataModel>(x))];
return [.. JoinPost(query).Select(x => CustomMapper.MapObject<WorkerDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -57,12 +49,12 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
return _mapper.Map<WorkerDataModel>(GetWorkerById(id));
return CustomMapper.MapObjectWithNull<WorkerDataModel>(GetWorkerById(id));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -70,12 +62,12 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
return _mapper.Map<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
return CustomMapper.MapObjectWithNull<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -83,12 +75,12 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
return _mapper.Map<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.PhoneNumber == phoneNumber && !x.IsDeleted)));
return CustomMapper.MapObjectWithNull<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.PhoneNumber == phoneNumber && !x.IsDeleted)));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -96,18 +88,28 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
_dbContext.Workers.Add(_mapper.Map<Worker>(workerDataModel));
_dbContext.Workers.Add(CustomMapper.MapObject<Worker>(workerDataModel));
_dbContext.SaveChanges();
}
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", workerDataModel.Id);
throw new ElementExistsException("Id", workerDataModel.Id, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Workers_PhoneNumber" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("PhoneNumber", workerDataModel.PhoneNumber, _localizer);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "PK_Workers" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", workerDataModel.Id, _localizer);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -115,8 +117,8 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
var element = GetWorkerById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id);
_dbContext.Workers.Update(_mapper.Map(workerDataModel, element));
var element = GetWorkerById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id, _localizer);
_dbContext.Workers.Update(CustomMapper.MapObject(workerDataModel, element));
_dbContext.SaveChanges();
}
catch (ElementNotFoundException)
@@ -124,10 +126,15 @@ internal class WorkerStorageContract : IWorkerStorageContract
_dbContext.ChangeTracker.Clear();
throw;
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Workers_PhoneNumber" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("PhoneNumber", workerDataModel.PhoneNumber, _localizer);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
@@ -135,8 +142,9 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
var element = GetWorkerById(id) ?? throw new ElementNotFoundException(id);
var element = GetWorkerById(id) ?? throw new ElementNotFoundException(id, _localizer);
element.IsDeleted = true;
element.DateOfDelete = DateTime.UtcNow;
_dbContext.SaveChanges();
}
catch (ElementNotFoundException)
@@ -147,7 +155,22 @@ internal class WorkerStorageContract : IWorkerStorageContract
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
throw new StorageException(ex, _localizer);
}
}
public int GetWorkerTrend(DateTime fromPeriod, DateTime toPeriod)
{
try
{
var countWorkersOnBegining = _dbContext.Workers.Count(x => x.EmploymentDate < fromPeriod && (!x.IsDeleted || x.DateOfDelete > fromPeriod));
var countWorkersOnEnding = _dbContext.Workers.Count(x => x.EmploymentDate < toPeriod && (!x.IsDeleted || x.DateOfDelete > toPeriod));
return countWorkersOnEnding - countWorkersOnBegining;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex, _localizer);
}
}
@@ -158,5 +181,5 @@ internal class WorkerStorageContract : IWorkerStorageContract
.SelectMany(xy => xy.Post.DefaultIfEmpty(), (x, y) => x.Worker.AddPost(y));
private Worker? AddPost(Worker? worker)
=> worker?.AddPost(_dbContext.Posts.FirstOrDefault(x => x.PostId == worker.PostId && x.IsActual));
=> worker == null ? null : worker.AddPost(_dbContext.Posts.FirstOrDefault(x => x.PostId == worker.PostId && x.IsActual));
}

View File

@@ -0,0 +1,324 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SPiluSZharuDatabase;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
[DbContext(typeof(SPiluSZharuDbContext))]
[Migration("20250422183326_FirstMigration")]
partial class FirstMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SPiluSZharuDatabase.Models.Post", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<bool>("IsActual")
.HasColumnType("boolean");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("PostType")
.HasColumnType("integer");
b.Property<double>("Salary")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("PostId", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.HasIndex("PostName", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.ToTable("Posts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("ProductName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("ProductType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ProductName", "IsDeleted")
.IsUnique()
.HasFilter("\"IsDeleted\" = FALSE");
b.ToTable("Products");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.Property<string>("ProductId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("ProductHistories");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Restaurant", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("PrevPrevRestaurantName")
.HasColumnType("text");
b.Property<string>("PrevRestaurantName")
.HasColumnType("text");
b.Property<string>("RestaurantName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantName")
.IsUnique();
b.ToTable("Restaurants");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("WorkerSalary")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("WorkerId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<string>("RestaurantId")
.HasColumnType("text");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantId");
b.HasIndex("WorkerId");
b.ToTable("Sales");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("ProductId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "ProductId");
b.HasIndex("ProductId");
b.ToTable("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("BirthDate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime>("EmploymentDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Workers");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("ProductHistories")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Salaries")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Restaurant", "Restaurant")
.WithMany()
.HasForeignKey("RestaurantId");
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Sales")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Restaurant");
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("SaleProducts")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SPiluSZharuDatabase.Models.Sale", "Sale")
.WithMany("SaleProducts")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
b.Navigation("Sale");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Navigation("ProductHistories");
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,257 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
/// <inheritdoc />
public partial class FirstMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Posts",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
PostId = table.Column<string>(type: "text", nullable: false),
PostName = table.Column<string>(type: "text", nullable: false),
PostType = table.Column<int>(type: "integer", nullable: false),
Salary = table.Column<double>(type: "double precision", nullable: false),
IsActual = table.Column<bool>(type: "boolean", nullable: false),
ChangeDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Posts", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
ProductName = table.Column<string>(type: "text", nullable: false),
ProductType = table.Column<int>(type: "integer", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false),
IsDeleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Restaurants",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
RestaurantName = table.Column<string>(type: "text", nullable: false),
PrevRestaurantName = table.Column<string>(type: "text", nullable: true),
PrevPrevRestaurantName = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Restaurants", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Workers",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
FIO = table.Column<string>(type: "text", nullable: false),
PostId = table.Column<string>(type: "text", nullable: false),
PhoneNumber = table.Column<string>(type: "text", nullable: false),
BirthDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
EmploymentDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
IsDeleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Workers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ProductHistories",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
ProductId = table.Column<string>(type: "text", nullable: false),
OldPrice = table.Column<double>(type: "double precision", nullable: false),
ChangeDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ProductHistories", x => x.Id);
table.ForeignKey(
name: "FK_ProductHistories_Products_ProductId",
column: x => x.ProductId,
principalTable: "Products",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Salaries",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
WorkerId = table.Column<string>(type: "text", nullable: false),
SalaryDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
WorkerSalary = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Salaries", x => x.Id);
table.ForeignKey(
name: "FK_Salaries_Workers_WorkerId",
column: x => x.WorkerId,
principalTable: "Workers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Sales",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
WorkerId = table.Column<string>(type: "text", nullable: false),
RestaurantId = table.Column<string>(type: "text", nullable: true),
SaleDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
Sum = table.Column<double>(type: "double precision", nullable: false),
IsCancel = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Sales", x => x.Id);
table.ForeignKey(
name: "FK_Sales_Restaurants_RestaurantId",
column: x => x.RestaurantId,
principalTable: "Restaurants",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Sales_Workers_WorkerId",
column: x => x.WorkerId,
principalTable: "Workers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SaleProducts",
columns: table => new
{
SaleId = table.Column<string>(type: "text", nullable: false),
ProductId = table.Column<string>(type: "text", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SaleProducts", x => new { x.SaleId, x.ProductId });
table.ForeignKey(
name: "FK_SaleProducts_Products_ProductId",
column: x => x.ProductId,
principalTable: "Products",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_SaleProducts_Sales_SaleId",
column: x => x.SaleId,
principalTable: "Sales",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Posts_PostId_IsActual",
table: "Posts",
columns: new[] { "PostId", "IsActual" },
unique: true,
filter: "\"IsActual\" = TRUE");
migrationBuilder.CreateIndex(
name: "IX_Posts_PostName_IsActual",
table: "Posts",
columns: new[] { "PostName", "IsActual" },
unique: true,
filter: "\"IsActual\" = TRUE");
migrationBuilder.CreateIndex(
name: "IX_ProductHistories_ProductId",
table: "ProductHistories",
column: "ProductId");
migrationBuilder.CreateIndex(
name: "IX_Products_ProductName_IsDeleted",
table: "Products",
columns: new[] { "ProductName", "IsDeleted" },
unique: true,
filter: "\"IsDeleted\" = FALSE");
migrationBuilder.CreateIndex(
name: "IX_Restaurants_RestaurantName",
table: "Restaurants",
column: "RestaurantName",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Salaries_WorkerId",
table: "Salaries",
column: "WorkerId");
migrationBuilder.CreateIndex(
name: "IX_SaleProducts_ProductId",
table: "SaleProducts",
column: "ProductId");
migrationBuilder.CreateIndex(
name: "IX_Sales_RestaurantId",
table: "Sales",
column: "RestaurantId");
migrationBuilder.CreateIndex(
name: "IX_Sales_WorkerId",
table: "Sales",
column: "WorkerId");
migrationBuilder.CreateIndex(
name: "IX_Workers_PhoneNumber",
table: "Workers",
column: "PhoneNumber",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Posts");
migrationBuilder.DropTable(
name: "ProductHistories");
migrationBuilder.DropTable(
name: "Salaries");
migrationBuilder.DropTable(
name: "SaleProducts");
migrationBuilder.DropTable(
name: "Products");
migrationBuilder.DropTable(
name: "Sales");
migrationBuilder.DropTable(
name: "Restaurants");
migrationBuilder.DropTable(
name: "Workers");
}
}
}

View File

@@ -0,0 +1,325 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SPiluSZharuDatabase;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
[DbContext(typeof(SPiluSZharuDbContext))]
[Migration("20250423150347_ChangeFieldsInPost")]
partial class ChangeFieldsInPost
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SPiluSZharuDatabase.Models.Post", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("Configuration")
.IsRequired()
.HasColumnType("jsonb");
b.Property<bool>("IsActual")
.HasColumnType("boolean");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("PostType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PostId", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.HasIndex("PostName", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.ToTable("Posts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("ProductName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("ProductType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ProductName", "IsDeleted")
.IsUnique()
.HasFilter("\"IsDeleted\" = FALSE");
b.ToTable("Products");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.Property<string>("ProductId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("ProductHistories");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Restaurant", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("PrevPrevRestaurantName")
.HasColumnType("text");
b.Property<string>("PrevRestaurantName")
.HasColumnType("text");
b.Property<string>("RestaurantName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantName")
.IsUnique();
b.ToTable("Restaurants");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("WorkerSalary")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("WorkerId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<string>("RestaurantId")
.HasColumnType("text");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantId");
b.HasIndex("WorkerId");
b.ToTable("Sales");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("ProductId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "ProductId");
b.HasIndex("ProductId");
b.ToTable("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("BirthDate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime>("EmploymentDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Workers");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("ProductHistories")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Salaries")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Restaurant", "Restaurant")
.WithMany()
.HasForeignKey("RestaurantId");
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Sales")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Restaurant");
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("SaleProducts")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SPiluSZharuDatabase.Models.Sale", "Sale")
.WithMany("SaleProducts")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
b.Navigation("Sale");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Navigation("ProductHistories");
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
/// <inheritdoc />
public partial class ChangeFieldsInPost : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Salary",
table: "Posts");
migrationBuilder.AddColumn<string>(
name: "Configuration",
table: "Posts",
type: "jsonb",
nullable: false,
defaultValue: "{\"Rate\": 0, \"Type\": \"PostConfiguration\"}");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Configuration",
table: "Posts");
migrationBuilder.AddColumn<double>(
name: "Salary",
table: "Posts",
type: "double precision",
nullable: false,
defaultValue: 0.0);
}
}
}

View File

@@ -0,0 +1,328 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SPiluSZharuDatabase;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
[DbContext(typeof(SPiluSZharuDbContext))]
[Migration("20250423193249_AddDateOfDeleteInWorker")]
partial class AddDateOfDeleteInWorker
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SPiluSZharuDatabase.Models.Post", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("Configuration")
.IsRequired()
.HasColumnType("jsonb");
b.Property<bool>("IsActual")
.HasColumnType("boolean");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("PostType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PostId", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.HasIndex("PostName", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.ToTable("Posts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("ProductName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("ProductType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ProductName", "IsDeleted")
.IsUnique()
.HasFilter("\"IsDeleted\" = FALSE");
b.ToTable("Products");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.Property<string>("ProductId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("ProductHistories");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Restaurant", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("PrevPrevRestaurantName")
.HasColumnType("text");
b.Property<string>("PrevRestaurantName")
.HasColumnType("text");
b.Property<string>("RestaurantName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantName")
.IsUnique();
b.ToTable("Restaurants");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("WorkerSalary")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("WorkerId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<string>("RestaurantId")
.HasColumnType("text");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantId");
b.HasIndex("WorkerId");
b.ToTable("Sales");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("ProductId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "ProductId");
b.HasIndex("ProductId");
b.ToTable("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("BirthDate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime?>("DateOfDelete")
.HasColumnType("timestamp without time zone");
b.Property<DateTime>("EmploymentDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Workers");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("ProductHistories")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Salaries")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Restaurant", "Restaurant")
.WithMany()
.HasForeignKey("RestaurantId");
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Sales")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Restaurant");
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("SaleProducts")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SPiluSZharuDatabase.Models.Sale", "Sale")
.WithMany("SaleProducts")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
b.Navigation("Sale");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Navigation("ProductHistories");
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,29 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
/// <inheritdoc />
public partial class AddDateOfDeleteInWorker : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "DateOfDelete",
table: "Workers",
type: "timestamp without time zone",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DateOfDelete",
table: "Workers");
}
}
}

View File

@@ -0,0 +1,325 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SPiluSZharuDatabase;
#nullable disable
namespace SPiluSZharuDatabase.Migrations
{
[DbContext(typeof(SPiluSZharuDbContext))]
partial class SPiluSZharuDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SPiluSZharuDatabase.Models.Post", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("Configuration")
.IsRequired()
.HasColumnType("jsonb");
b.Property<bool>("IsActual")
.HasColumnType("boolean");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("PostType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PostId", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.HasIndex("PostName", "IsActual")
.IsUnique()
.HasFilter("\"IsActual\" = TRUE");
b.ToTable("Posts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("ProductName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("ProductType")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ProductName", "IsDeleted")
.IsUnique()
.HasFilter("\"IsDeleted\" = FALSE");
b.ToTable("Products");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.Property<string>("ProductId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("ProductHistories");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Restaurant", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("PrevPrevRestaurantName")
.HasColumnType("text");
b.Property<string>("PrevRestaurantName")
.HasColumnType("text");
b.Property<string>("RestaurantName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantName")
.IsUnique();
b.ToTable("Restaurants");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("WorkerSalary")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("WorkerId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<string>("RestaurantId")
.HasColumnType("text");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.Property<string>("WorkerId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RestaurantId");
b.HasIndex("WorkerId");
b.ToTable("Sales");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("ProductId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "ProductId");
b.HasIndex("ProductId");
b.ToTable("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("BirthDate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime?>("DateOfDelete")
.HasColumnType("timestamp without time zone");
b.Property<DateTime>("EmploymentDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsDeleted")
.HasColumnType("boolean");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PostId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Workers");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.ProductHistory", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("ProductHistories")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Salary", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Salaries")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Restaurant", "Restaurant")
.WithMany()
.HasForeignKey("RestaurantId");
b.HasOne("SPiluSZharuDatabase.Models.Worker", "Worker")
.WithMany("Sales")
.HasForeignKey("WorkerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Restaurant");
b.Navigation("Worker");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.SaleProduct", b =>
{
b.HasOne("SPiluSZharuDatabase.Models.Product", "Product")
.WithMany("SaleProducts")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SPiluSZharuDatabase.Models.Sale", "Sale")
.WithMany("SaleProducts")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
b.Navigation("Sale");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Product", b =>
{
b.Navigation("ProductHistories");
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Sale", b =>
{
b.Navigation("SaleProducts");
});
modelBuilder.Entity("SPiluSZharuDatabase.Models.Worker", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -1,4 +1,6 @@
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuDatabase.Models;
@@ -12,9 +14,12 @@ internal class Post
public PostType PostType { get; set; }
public double Salary { get; set; }
[AlternativeName("ConfigurationModel")]
public required PostConfiguration Configuration { get; set; }
[DefaultValue(DefaultValue = true)]
public bool IsActual { get; set; }
[DefaultValue(FuncName = "UtcNow")]
public DateTime ChangeDate { get; set; }
}

View File

@@ -11,10 +11,14 @@ internal class Product
public ProductType ProductType { get; set; }
public required string RestaurantId { get; set; }
public double Price { get; set; }
public bool IsDeleted { get; set; }
public Restaurant? Restaurant { get; set; }
[ForeignKey("ProductId")]
public List<ProductHistory>? ProductHistories { get; set; }

View File

@@ -1,5 +1,6 @@
using AutoMapper;
using SPiluSZharuContracts.DataModels;
using System.ComponentModel.DataAnnotations.Schema;
namespace SPiluSZharuDatabase.Models;
@@ -13,4 +14,7 @@ internal class Restaurant
public string? PrevRestaurantName { get; set; }
public string? PrevPrevRestaurantName { get; set; }
[ForeignKey("RestaurantId")]
public List<Product>? Products { get; set; }
}

View File

@@ -1,4 +1,6 @@
namespace SPiluSZharuDatabase.Models;
using SPiluSZharuContracts.Mapper;
namespace SPiluSZharuDatabase.Models;
internal class Salary
{

View File

@@ -1,5 +1,6 @@
using AutoMapper;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Mapper;
using System.ComponentModel.DataAnnotations.Schema;
namespace SPiluSZharuDatabase.Models;
@@ -20,7 +21,10 @@ internal class Worker
public bool IsDeleted { get; set; }
public DateTime? DateOfDelete { get; set; }
[NotMapped]
[IgnoreMapping]
public Post? Post { get; set; }
[ForeignKey("WorkerId")]

View File

@@ -9,6 +9,11 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
</ItemGroup>

View File

@@ -1,7 +1,9 @@
using SPiluSZharuContracts.Infrastructure;
using Microsoft.EntityFrameworkCore;
using SPiluSZharuDatabase.Models;
using System;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace SPiluSZharuDatabase;
@@ -12,8 +14,6 @@ internal class SPiluSZharuDbContext : DbContext
public SPiluSZharuDbContext(IConfigurationDatabase configurationDatabase)
{
_configurationDatabase = configurationDatabase;
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@@ -43,9 +43,23 @@ internal class SPiluSZharuDbContext : DbContext
.IsUnique()
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
modelBuilder
.Entity<Product>()
.HasOne(e => e.Restaurant)
.WithMany(e => e.Products)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<SaleProduct>().HasKey(x => new { x.SaleId, x.ProductId });
modelBuilder.Entity<Worker>().HasIndex(x => x.PhoneNumber).IsUnique();
modelBuilder.Entity<Post>()
.Property(x => x.Configuration)
.HasColumnType("jsonb")
.HasConversion(
x => SerializePostConfiguration(x),
x => DeserialzePostConfiguration(x)
);
}
public DbSet<Restaurant> Restaurants { get; set; }
@@ -63,4 +77,13 @@ internal class SPiluSZharuDbContext : DbContext
public DbSet<SaleProduct> SaleProducts { get; set; }
public DbSet<Worker> Workers { get; set; }
private static string SerializePostConfiguration(PostConfiguration postConfiguration) => JsonConvert.SerializeObject(postConfiguration);
private static PostConfiguration DeserialzePostConfiguration(string jsonString) => JToken.Parse(jsonString).Value<string>("Type") switch
{
nameof(OperatorPostConfiguration) => JsonConvert.DeserializeObject<OperatorPostConfiguration>(jsonString)!,
nameof(DeliveryManPostConfiguration) => JsonConvert.DeserializeObject<DeliveryManPostConfiguration>(jsonString)!,
_ => JsonConvert.DeserializeObject<PostConfiguration>(jsonString)!,
};
}

View File

@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore.Design;
namespace SPiluSZharuDatabase;
internal class SampleContextFactory : IDesignTimeDbContextFactory<SPiluSZharuDbContext>
{
public SPiluSZharuDbContext CreateDbContext(string[] args)
{
return new SPiluSZharuDbContext(new DefaultConfigurationDatabase());
}
}

View File

@@ -1,10 +1,12 @@
using SPiluSZharuBuisnessLogic.Implementations;
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuBuisnessLogic.Implementations;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.Infrastructure.PostConfiguration;
using SPiluSZharuContracts.StorageContracts;
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuTests.Infrastructure;
namespace SPiluSZharuTests.BuisnessLogicContractsTests;
@@ -18,7 +20,7 @@ internal class PostBuisnessLogicContractTests
public void OneTimeSetUp()
{
_postStorageContract = new Mock<IPostStorageContract>();
_postBuisnessLogicContract = new PostBuisnessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
_postBuisnessLogicContract = new PostBuisnessLogicContract(_postStorageContract.Object, StringLocalizerMockCreator.GetObject(), new Mock<ILogger>().Object);
}
[SetUp]
@@ -33,9 +35,9 @@ internal class PostBuisnessLogicContractTests
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.Operator, 10),
new(Guid.NewGuid().ToString(), "name 2", PostType.Operator, 10),
new(Guid.NewGuid().ToString(), "name 3", PostType.Operator, 10),
new(Guid.NewGuid().ToString(),"name 1", PostType.Operator, new PostConfiguration() { Rate = 10 }),
new(Guid.NewGuid().ToString(), "name 2", PostType.Operator, new PostConfiguration() { Rate = 10 }),
new(Guid.NewGuid().ToString(), "name 3", PostType.Operator, new PostConfiguration() { Rate = 10 }),
};
_postStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act
@@ -64,19 +66,11 @@ internal class PostBuisnessLogicContractTests
});
}
[Test]
public void GetAllPosts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.GetAllPosts(), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllPosts_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.GetAllPosts(), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetList(), Times.Once);
@@ -89,8 +83,8 @@ internal class PostBuisnessLogicContractTests
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>()
{
new(postId, "name 1", PostType.Operator, 10),
new(postId, "name 2", PostType.Operator, 10)
new(postId, "name 1", PostType.Operator, new PostConfiguration() { Rate = 10 }),
new(postId, "name 2", PostType.Operator, new PostConfiguration() { Rate = 10 })
};
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -131,19 +125,11 @@ internal class PostBuisnessLogicContractTests
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
@@ -154,7 +140,7 @@ internal class PostBuisnessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.Operator, 10);
var record = new PostDataModel(id, "name", PostType.Operator, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _postBuisnessLogicContract.GetPostByData(id);
@@ -169,7 +155,7 @@ internal class PostBuisnessLogicContractTests
{
//Arrange
var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Operator, 10);
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Operator, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
//Act
var element = _postBuisnessLogicContract.GetPostByData(postName);
@@ -211,8 +197,8 @@ internal class PostBuisnessLogicContractTests
public void GetPostByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
_postStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _postBuisnessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
@@ -225,11 +211,11 @@ internal class PostBuisnessLogicContractTests
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, 10);
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary;
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
});
//Act
_postBuisnessLogicContract.InsertPost(record);
@@ -242,9 +228,9 @@ internal class PostBuisnessLogicContractTests
public void InsertPost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, 10)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _postBuisnessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -260,7 +246,7 @@ internal class PostBuisnessLogicContractTests
public void InsertPost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.OrderPicker, 10)), Throws.TypeOf<ValidationException>());
Assert.That(() => _postBuisnessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
@@ -268,9 +254,9 @@ internal class PostBuisnessLogicContractTests
public void InsertPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, 10)), Throws.TypeOf<StorageException>());
Assert.That(() => _postBuisnessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -279,11 +265,11 @@ internal class PostBuisnessLogicContractTests
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, 10);
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary;
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
});
//Act
_postBuisnessLogicContract.UpdatePost(record);
@@ -296,9 +282,9 @@ internal class PostBuisnessLogicContractTests
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException("", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, 10)), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -306,9 +292,9 @@ internal class PostBuisnessLogicContractTests
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.OrderPicker, 10)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -324,7 +310,7 @@ internal class PostBuisnessLogicContractTests
public void UpdatePost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.OrderPicker, 10)), Throws.TypeOf<ValidationException>());
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
@@ -332,9 +318,9 @@ internal class PostBuisnessLogicContractTests
public void UpdatePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, 10)), Throws.TypeOf<StorageException>());
Assert.That(() => _postBuisnessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.OrderPicker, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -357,7 +343,7 @@ internal class PostBuisnessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id, StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
@@ -384,7 +370,7 @@ internal class PostBuisnessLogicContractTests
public void DeletePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
@@ -409,7 +395,7 @@ internal class PostBuisnessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id, StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
@@ -436,7 +422,7 @@ internal class PostBuisnessLogicContractTests
public void RestorePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _postBuisnessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);

View File

@@ -1,10 +1,12 @@
using SPiluSZharuBuisnessLogic.Implementations;
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuBuisnessLogic.Implementations;
using SPiluSZharuContracts.BuisnessLogicContracts;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.StorageContracts;
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuTests.Infrastructure;
namespace SPiluSZharuTests.BuisnessLogicContractsTests;
@@ -18,7 +20,7 @@ internal class ProductBuisnessLogicContractTests
public void OneTimeSetUp()
{
_productStorageContract = new Mock<IProductStorageContract>();
_productBuisnessLogicContract = new ProductBuisnessLogicContract(_productStorageContract.Object, new Mock<ILogger>().Object);
_productBuisnessLogicContract = new ProductBuisnessLogicContract(_productStorageContract.Object, StringLocalizerMockCreator.GetObject(), new Mock<ILogger>().Object);
}
[SetUp]
@@ -33,11 +35,11 @@ internal class ProductBuisnessLogicContractTests
//Arrange
var listOriginal = new List<ProductDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1", ProductType.Pizza, 10, false),
new(Guid.NewGuid().ToString(), "name 2", ProductType.Pizza, 10, true),
new(Guid.NewGuid().ToString(), "name 3", ProductType.Pizza, 10, false),
new(Guid.NewGuid().ToString(), "name 1", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
new(Guid.NewGuid().ToString(), "name 2", ProductType.Pizza, Guid.NewGuid().ToString(), 10, true),
new(Guid.NewGuid().ToString(), "name 3", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
};
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var listOnlyActive = _productBuisnessLogicContract.GetAllProducts(true);
var list = _productBuisnessLogicContract.GetAllProducts(false);
@@ -49,15 +51,15 @@ internal class ProductBuisnessLogicContractTests
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_productStorageContract.Verify(x => x.GetList(true), Times.Once);
_productStorageContract.Verify(x => x.GetList(false), Times.Once);
_productStorageContract.Verify(x => x.GetList(true, null), Times.Once);
_productStorageContract.Verify(x => x.GetList(false, null), Times.Once);
}
[Test]
public void GetAllProducts_ReturnEmptyList_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns([]);
//Act
var listOnlyActive = _productBuisnessLogicContract.GetAllProducts(true);
var list = _productBuisnessLogicContract.GetAllProducts(false);
@@ -69,25 +71,91 @@ internal class ProductBuisnessLogicContractTests
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllProducts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null), Times.Exactly(2));
}
[Test]
public void GetAllProducts_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllProductsByRestaurant_ReturnListOfRecords_Test()
{
//Arrange
var restaurantId = Guid.NewGuid().ToString();
var listOriginal = new List<ProductDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1",ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
new(Guid.NewGuid().ToString(), "name 2", ProductType.Pizza, Guid.NewGuid().ToString(), 10, true),
new(Guid.NewGuid().ToString(), "name 3", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
};
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var listOnlyActive = _productBuisnessLogicContract.GetAllProductsByRestaurant(restaurantId, true);
var list = _productBuisnessLogicContract.GetAllProductsByRestaurant(restaurantId, false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_productStorageContract.Verify(x => x.GetList(true, restaurantId), Times.Once);
_productStorageContract.Verify(x => x.GetList(false, restaurantId), Times.Once);
}
[Test]
public void GetAllProductsByRestaurant_ReturnEmptyList_Test()
{
//Arrange
var restaurantId = Guid.NewGuid().ToString();
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns([]);
//Act
var listOnlyActive = _productBuisnessLogicContract.GetAllProductsByRestaurant(restaurantId, true);
var list = _productBuisnessLogicContract.GetAllProductsByRestaurant(restaurantId, false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), restaurantId), Times.Exactly(2));
}
[Test]
public void GetAllProductsByRestaurant_RestaurantIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetAllProductsByRestaurant(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productBuisnessLogicContract.GetAllProductsByRestaurant(string.Empty, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllProductsByRestaurant_RestaurantIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetAllProductsByRestaurant("restaurantId", It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllProductsByRestaurant_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetAllProductsByRestaurant(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
}
[Test]
@@ -140,19 +208,11 @@ internal class ProductBuisnessLogicContractTests
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductHistoryByProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
@@ -163,7 +223,7 @@ internal class ProductBuisnessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new ProductDataModel(id, "name", ProductType.Pizza, 10, false);
var record = new ProductDataModel(id, "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
_productStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _productBuisnessLogicContract.GetProductByData(id);
@@ -178,7 +238,7 @@ internal class ProductBuisnessLogicContractTests
{
//Arrange
var name = "name";
var record = new ProductDataModel(Guid.NewGuid().ToString(), name, ProductType.Pizza, 10, false);
var record = new ProductDataModel(Guid.NewGuid().ToString(), name, ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
_productStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
//Act
var element = _productBuisnessLogicContract.GetProductByData(name);
@@ -218,8 +278,8 @@ internal class ProductBuisnessLogicContractTests
public void GetProductByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
_productStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _productBuisnessLogicContract.GetProductByData("name"), Throws.TypeOf<StorageException>());
@@ -232,7 +292,7 @@ internal class ProductBuisnessLogicContractTests
{
//Arrange
var flag = false;
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false);
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
.Callback((ProductDataModel x) =>
{
@@ -250,9 +310,9 @@ internal class ProductBuisnessLogicContractTests
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _productBuisnessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ElementExistsException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
}
@@ -268,7 +328,7 @@ internal class ProductBuisnessLogicContractTests
public void InsertProduct_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.InsertProduct(new ProductDataModel("id", "name", ProductType.Pizza, 10, false)), Throws.TypeOf<ValidationException>());
Assert.That(() => _productBuisnessLogicContract.InsertProduct(new ProductDataModel("id", "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
}
@@ -276,9 +336,9 @@ internal class ProductBuisnessLogicContractTests
public void InsertProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false)), Throws.TypeOf<StorageException>());
Assert.That(() => _productBuisnessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
}
@@ -287,7 +347,7 @@ internal class ProductBuisnessLogicContractTests
{
//Arrange
var flag = false;
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false);
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>()))
.Callback((ProductDataModel x) =>
{
@@ -305,9 +365,9 @@ internal class ProductBuisnessLogicContractTests
public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementNotFoundException(""));
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementNotFoundException("", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false)), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
}
@@ -315,9 +375,9 @@ internal class ProductBuisnessLogicContractTests
public void UpdateProduct_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ElementExistsException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
}
@@ -333,7 +393,7 @@ internal class ProductBuisnessLogicContractTests
public void UpdateProduct_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new ProductDataModel("id", "name", ProductType.Pizza, 10, false)), Throws.TypeOf<ValidationException>());
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new ProductDataModel("id", "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
}
@@ -341,9 +401,9 @@ internal class ProductBuisnessLogicContractTests
public void UpdateProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, 10, false)), Throws.TypeOf<StorageException>());
Assert.That(() => _productBuisnessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
}
@@ -366,7 +426,7 @@ internal class ProductBuisnessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id, StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
@@ -393,7 +453,7 @@ internal class ProductBuisnessLogicContractTests
public void DeleteProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _productBuisnessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);

View File

@@ -0,0 +1,365 @@
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuBuisnessLogic.Implementations;
using SPiluSZharuBuisnessLogic.OfficePackage;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Enums;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.StorageContracts;
using SPiluSZharuTests.Infrastructure;
namespace SPiluSZharuTests.BuisnessLogicContractsTests;
[TestFixture]
internal class ReportContractTests
{
private ReportContract _reportContract;
private Mock<IProductStorageContract> _productStorageContract;
private Mock<ISaleStorageContract> _saleStorageContract;
private Mock<ISalaryStorageContract> _salaryStorageContract;
private Mock<BaseWordBuilder> _baseWordBuilder;
private Mock<BaseExcelBuilder> _baseExcelBuilder;
private Mock<BasePdfBuilder> _basePdfBuilder;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_productStorageContract = new Mock<IProductStorageContract>();
_saleStorageContract = new Mock<ISaleStorageContract>();
_salaryStorageContract = new Mock<ISalaryStorageContract>();
_baseWordBuilder = new Mock<BaseWordBuilder>();
_baseExcelBuilder = new Mock<BaseExcelBuilder>();
_basePdfBuilder = new Mock<BasePdfBuilder>();
_reportContract = new ReportContract(_productStorageContract.Object, _saleStorageContract.Object, _salaryStorageContract.Object, _baseWordBuilder.Object, _baseExcelBuilder.Object, _basePdfBuilder.Object, StringLocalizerMockCreator.GetObject(), new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_productStorageContract.Reset();
_saleStorageContract.Reset();
_salaryStorageContract.Reset();
}
[Test]
public async Task GetDataProductsByRestaurant_ShouldSuccess_Test()
{
//Arrange
var restaurant1 = new RestaurantDataModel(Guid.NewGuid().ToString(), "name1");
var restaurant2 = new RestaurantDataModel(Guid.NewGuid().ToString(), "name2");
_productStorageContract.Setup(x => x.GetListAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<ProductDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1.1", ProductType.Pizza, restaurant1.Id, 10, false, restaurant1),
new(Guid.NewGuid().ToString(), "name 2.1", ProductType.Pizza, restaurant2.Id, 10, false, restaurant2),
new(Guid.NewGuid().ToString(), "name 1.2", ProductType.Pizza, restaurant1.Id, 10, false, restaurant1),
new(Guid.NewGuid().ToString(), "name 1.3", ProductType.Pizza, restaurant1.Id, 10, false, restaurant1),
new(Guid.NewGuid().ToString(), "name 2.2", ProductType.Pizza, restaurant2.Id, 10, false, restaurant2)
}));
//Act
var data = await _reportContract.GetDataProductsByRestaurantAsync(CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
Assert.Multiple(() =>
{
Assert.That(data.First(x => x.RestaurantName == restaurant1.RestaurantName).Products, Has.Count.EqualTo(3));
Assert.That(data.First(x => x.RestaurantName == restaurant2.RestaurantName).Products, Has.Count.EqualTo(2));
});
_productStorageContract.Verify(x => x.GetListAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataProductsByRestaurant_WhenNoRecords_ShouldSuccess_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetListAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<ProductDataModel>()));
//Act
var data = await _reportContract.GetDataProductsByRestaurantAsync(CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(0));
_productStorageContract.Verify(x => x.GetListAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public void GetDataProductsByRestaurant_WhenStorageThrowError_ShouldFail_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetListAsync(It.IsAny<CancellationToken>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(async () => await _reportContract.GetDataProductsByRestaurantAsync(CancellationToken.None), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetListAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataSalesByPeriod_ShouldSuccess_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, [new SaleProductDataModel("", Guid.NewGuid().ToString(), 10, 10)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, [new SaleProductDataModel("", Guid.NewGuid().ToString(), 10, 10)])
}));
//Act
var data = await _reportContract.GetDataSaleByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataSalesByPeriod_WhenNoRecords_ShouldSuccess_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SaleDataModel>()));
//Act
var data = await _reportContract.GetDataSaleByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public void GetDataSalesByPeriod_WhenIncorrectDates_ShouldFail_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(async () => await _reportContract.GetDataSaleByPeriodAsync(date, date, CancellationToken.None), Throws.TypeOf<IncorrectDatesException>());
Assert.That(async () => await _reportContract.GetDataSaleByPeriodAsync(date, DateTime.UtcNow.AddDays(-1), CancellationToken.None), Throws.TypeOf<IncorrectDatesException>());
}
[Test]
public void GetDataBySalesByPeriod_WhenStorageThrowError_ShouldFail_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(async () => await _reportContract.GetDataSaleByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataSalaryByPeriod_ShouldSuccess_Test()
{
//Arrange
var startDate = DateTime.UtcNow.AddDays(-20);
var endDate = DateTime.UtcNow.AddDays(5);
var worker1 = new WorkerDataModel(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), "+7(777)777-77-77", DateTime.UtcNow.AddYears(-20), DateTime.UtcNow.AddDays(-3));
var worker2 = new WorkerDataModel(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), "+7(999)999-99-99", DateTime.UtcNow.AddYears(-20), DateTime.UtcNow.AddDays(-3));
_salaryStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SalaryDataModel>()
{
new(worker1.Id, DateTime.UtcNow.AddDays(-10), 100),
new(worker1.Id, endDate, 1000),
new(worker1.Id, startDate, 1000),
new(worker2.Id, DateTime.UtcNow.AddDays(-10), 100),
new(worker2.Id, DateTime.UtcNow.AddDays(-5), 200)
}));
//Act
var data = await _reportContract.GetDataSalaryByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
var totalSalary = data.First();
Assert.Multiple(() =>
{
Assert.That(totalSalary, Is.Not.Null);
Assert.That(totalSalary.TotalSalary, Is.EqualTo(2100));
});
_salaryStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(),
It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataSalaryByPeriod_WhenNoRecords_ShouldSuccess_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SalaryDataModel>()));
//Act
var data = await _reportContract.GetDataSalaryByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public void GetDataSalaryByPeriod_WhenIncorrectDates_ShouldFail_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(async () => await _reportContract.GetDataSalaryByPeriodAsync(date, date, CancellationToken.None), Throws.TypeOf<IncorrectDatesException>());
Assert.That(async () => await _reportContract.GetDataSalaryByPeriodAsync(date, DateTime.UtcNow.AddDays(-1), CancellationToken.None), Throws.TypeOf<IncorrectDatesException>());
}
[Test]
public void GetDataSalaryByPeriod_WhenStorageThrowError_ShouldFail_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(async () => await _reportContract.GetDataSalaryByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None), Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task CreateDocumentProductsByRestaurant_ShouldSuccess_Test()
{
//Arrange
var restaurant1 = new RestaurantDataModel(Guid.NewGuid().ToString(), "name1");
var restaurant2 = new RestaurantDataModel(Guid.NewGuid().ToString(), "name2");
_productStorageContract.Setup(x => x.GetListAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<ProductDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1.1", ProductType.Pizza, restaurant1.Id, 10, false, restaurant1),
new(Guid.NewGuid().ToString(), "name 2.1", ProductType.Pizza, restaurant2.Id, 10, false, restaurant2),
new(Guid.NewGuid().ToString(), "name 1.2", ProductType.Pizza, restaurant1.Id, 10, false, restaurant1),
new(Guid.NewGuid().ToString(), "name 1.3", ProductType.Pizza, restaurant1.Id, 10, false, restaurant1),
new(Guid.NewGuid().ToString(), "name 2.2", ProductType.Pizza, restaurant2.Id, 10, false, restaurant2)
}));
_baseWordBuilder.Setup(x => x.AddHeader(It.IsAny<string>())).Returns(_baseWordBuilder.Object);
_baseWordBuilder.Setup(x => x.AddParagraph(It.IsAny<string>())).Returns(_baseWordBuilder.Object);
var countRows = 0;
string[] firstRow = [];
string[] secondRow = [];
_baseWordBuilder.Setup(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()))
.Callback((int[] widths, List<string[]> data) =>
{
countRows = data.Count;
firstRow = data[1];
secondRow = data[2];
})
.Returns(_baseWordBuilder.Object);
//Act
var data = await _reportContract.CreateDocumentProductsByRestaurantAsync(CancellationToken.None);
//Assert
_productStorageContract.Verify(x => x.GetListAsync(It.IsAny<CancellationToken>()), Times.Once);
_baseWordBuilder.Verify(x => x.AddHeader(It.IsAny<string>()), Times.Once);
_baseWordBuilder.Verify(x => x.AddParagraph(It.IsAny<string>()), Times.Once);
_baseWordBuilder.Verify(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()), Times.Once);
_baseWordBuilder.Verify(x => x.Build(), Times.Once);
Assert.Multiple(() =>
{
Assert.That(countRows, Is.EqualTo(8));
Assert.That(firstRow, Has.Length.EqualTo(2));
Assert.That(secondRow, Has.Length.EqualTo(2));
});
Assert.Multiple(() =>
{
Assert.That(firstRow[0], Is.EqualTo(restaurant1.RestaurantName));
Assert.That(firstRow[1], Is.Empty);
Assert.That(secondRow[0], Is.Empty);
Assert.That(secondRow[1], Is.EqualTo("name 1.1"));
});
}
[Test]
public async Task CreateDocumentSalesByPeriod_ShouldeSuccess_Test()
{
var product1 = new ProductDataModel(Guid.NewGuid().ToString(), "name 1", ProductType.Pizza, Guid.NewGuid().ToString(), 10);
var product2 = new ProductDataModel(Guid.NewGuid().ToString(), "name 2", ProductType.Pizza, Guid.NewGuid().ToString(), 10);
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, [new SaleProductDataModel("", product1.Id, 10, 10, product1), new SaleProductDataModel("", product2.Id, 10, 10, product2)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, [new SaleProductDataModel("", product2.Id, 10, 10, product2)])
}));
_baseExcelBuilder.Setup(x => x.AddHeader(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).Returns(_baseExcelBuilder.Object);
_baseExcelBuilder.Setup(x => x.AddParagraph(It.IsAny<string>(), It.IsAny<int>())).Returns(_baseExcelBuilder.Object);
var countRows = 0;
string[] firstRow = [];
string[] secondRow = [];
_baseExcelBuilder.Setup(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()))
.Callback((int[] widths, List<string[]> data) =>
{
countRows = data.Count;
firstRow = data[1];
secondRow = data[2];
})
.Returns(_baseExcelBuilder.Object);
//Act
var data = await _reportContract.CreateDocumentSalesByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.Multiple(() =>
{
Assert.That(countRows, Is.EqualTo(7));
Assert.That(firstRow, Is.Not.EqualTo(default(string[])));
Assert.That(secondRow, Is.Not.EqualTo(default(string[])));
});
Assert.Multiple(() =>
{
Assert.That(firstRow[0], Is.Not.Empty);
Assert.That(firstRow[1], Is.EqualTo(200.ToString("N2")));
Assert.That(firstRow[2], Is.Empty);
Assert.That(firstRow[3], Is.Empty);
});
Assert.Multiple(() =>
{
Assert.That(secondRow[0], Is.Empty);
Assert.That(secondRow[1], Is.Empty);
Assert.That(secondRow[2], Is.EqualTo(product1.ProductName));
Assert.That(secondRow[3], Is.EqualTo(10.ToString("N2")));
});
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
_baseExcelBuilder.Verify(x => x.AddHeader(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Once);
_baseExcelBuilder.Verify(x => x.AddParagraph(It.IsAny<string>(), It.IsAny<int>()), Times.Once);
_baseExcelBuilder.Verify(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()), Times.Once);
_baseExcelBuilder.Verify(x => x.Build(), Times.Once);
}
[Test]
public async Task CreateDocumentSalaryByPeriod_ShouldSuccess_Test()
{
//Arrange
var startDate = DateTime.UtcNow.AddDays(-20);
var endDate = DateTime.UtcNow.AddDays(5);
var worker1 = new WorkerDataModel(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), "+7(777)777-77-77", DateTime.UtcNow.AddYears(-20), DateTime.UtcNow.AddDays(-3));
var worker2 = new WorkerDataModel(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), "+7(999)999-99-99", DateTime.UtcNow.AddYears(-20), DateTime.UtcNow.AddDays(-3));
_salaryStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SalaryDataModel>()
{
new(worker1.Id, DateTime.UtcNow.AddDays(-10), 100, worker1),
new(worker1.Id, endDate, 1000, worker1),
new(worker1.Id, startDate, 1000, worker1),
new(worker2.Id, DateTime.UtcNow.AddDays(-10), 100, worker2),
new(worker2.Id, DateTime.UtcNow.AddDays(-5), 200, worker2)
}));
_basePdfBuilder.Setup(x => x.AddHeader(It.IsAny<string>())).Returns(_basePdfBuilder.Object);
_basePdfBuilder.Setup(x => x.AddParagraph(It.IsAny<string>())).Returns(_basePdfBuilder.Object);
var countRows = 0;
(string, double) firstRow = default;
(string, double) secondRow = default;
_basePdfBuilder.Setup(x => x.AddPieChart(It.IsAny<string>(), It.IsAny<List<(string, double)>>()))
.Callback((string header, List<(string, double)> data) =>
{
countRows = data.Count;
firstRow = data[0];
secondRow = data[1];
})
.Returns(_basePdfBuilder.Object);
//Act
var data = await _reportContract.CreateDocumentSalaryByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.Multiple(() =>
{
Assert.That(countRows, Is.EqualTo(2));
Assert.That(firstRow, Is.Not.EqualTo(default((string, double))));
Assert.That(secondRow, Is.Not.EqualTo(default((string, double))));
});
Assert.Multiple(() =>
{
Assert.That(firstRow.Item1, Is.EqualTo(worker1.FIO));
Assert.That(firstRow.Item2, Is.EqualTo(2100));
Assert.That(secondRow.Item1, Is.EqualTo(worker2.FIO));
Assert.That(secondRow.Item2, Is.EqualTo(300));
});
_salaryStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
_basePdfBuilder.Verify(x => x.AddHeader(It.IsAny<string>()), Times.Once);
_basePdfBuilder.Verify(x => x.AddParagraph(It.IsAny<string>()), Times.Once);
_basePdfBuilder.Verify(x => x.AddPieChart(It.IsAny<string>(), It.IsAny<List<(string, double)>>()), Times.Once);
_basePdfBuilder.Verify(x => x.Build(), Times.Once);
}
}

View File

@@ -1,9 +1,10 @@
using SPiluSZharuBuisnessLogic.Implementations;
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuBuisnessLogic.Implementations;
using SPiluSZharuContracts.DataModels;
using SPiluSZharuContracts.Exceptions;
using SPiluSZharuContracts.StorageContracts;
using Microsoft.Extensions.Logging;
using Moq;
using SPiluSZharuTests.Infrastructure;
namespace SPiluSZharuTests.BuisnessLogicContractsTests;
@@ -17,7 +18,7 @@ internal class RestaurantBuisnessLogicContractTests
public void OneTimeSetUp()
{
_restaurantStorageContract = new Mock<IRestaurantStorageContract>();
_restaurantBuisnessLogicContract = new RestaurantBuisnessLogicContract(_restaurantStorageContract.Object, new Mock<ILogger>().Object);
_restaurantBuisnessLogicContract = new RestaurantBuisnessLogicContract(_restaurantStorageContract.Object, StringLocalizerMockCreator.GetObject(), new Mock<ILogger>().Object);
}
[SetUp]
@@ -57,19 +58,11 @@ internal class RestaurantBuisnessLogicContractTests
_restaurantStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllRestaurants_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.GetAllRestaurants(), Throws.TypeOf<NullListException>());
_restaurantStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllRestaurants_StorageThrowError_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.GetAllRestaurants(), Throws.TypeOf<StorageException>());
_restaurantStorageContract.Verify(x => x.GetList(), Times.Once);
@@ -156,8 +149,8 @@ internal class RestaurantBuisnessLogicContractTests
public void GetRestaurantByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
_restaurantStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.GetRestaurantByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _restaurantBuisnessLogicContract.GetRestaurantByData("name"), Throws.TypeOf<StorageException>());
@@ -170,7 +163,7 @@ internal class RestaurantBuisnessLogicContractTests
public void GetRestaurantByData_GetByOldName_StorageThrowError_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.GetElementByOldName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.GetElementByOldName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.GetRestaurantByData("name"), Throws.TypeOf<StorageException>());
_restaurantStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
@@ -199,7 +192,7 @@ internal class RestaurantBuisnessLogicContractTests
public void InsertRestaurant_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.AddElement(It.IsAny<RestaurantDataModel>())).Throws(new ElementExistsException("Data", "Data"));
_restaurantStorageContract.Setup(x => x.AddElement(It.IsAny<RestaurantDataModel>())).Throws(new ElementExistsException("Data", "Data", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.InsertRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf<ElementExistsException>());
_restaurantStorageContract.Verify(x => x.AddElement(It.IsAny<RestaurantDataModel>()), Times.Once);
@@ -225,7 +218,7 @@ internal class RestaurantBuisnessLogicContractTests
public void InsertRestaurant_StorageThrowError_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.AddElement(It.IsAny<RestaurantDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.AddElement(It.IsAny<RestaurantDataModel>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.InsertRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf<StorageException>());
_restaurantStorageContract.Verify(x => x.AddElement(It.IsAny<RestaurantDataModel>()), Times.Once);
@@ -253,7 +246,7 @@ internal class RestaurantBuisnessLogicContractTests
public void UpdateRestaurant_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny<RestaurantDataModel>())).Throws(new ElementNotFoundException(""));
_restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny<RestaurantDataModel>())).Throws(new ElementNotFoundException("", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.UpdateRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf<ElementNotFoundException>());
_restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny<RestaurantDataModel>()), Times.Once);
@@ -263,7 +256,7 @@ internal class RestaurantBuisnessLogicContractTests
public void UpdateRestaurant_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny<RestaurantDataModel>())).Throws(new ElementExistsException("Data", "Data"));
_restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny<RestaurantDataModel>())).Throws(new ElementExistsException("Data", "Data", StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.UpdateRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf<ElementExistsException>());
_restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny<RestaurantDataModel>()), Times.Once);
@@ -289,7 +282,7 @@ internal class RestaurantBuisnessLogicContractTests
public void UpdateRestaurant_StorageThrowError_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny<RestaurantDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny<RestaurantDataModel>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.UpdateRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf<StorageException>());
_restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny<RestaurantDataModel>()), Times.Once);
@@ -314,7 +307,7 @@ internal class RestaurantBuisnessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
_restaurantStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
_restaurantStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id, StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.DeleteRestaurant(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_restaurantStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
@@ -341,7 +334,7 @@ internal class RestaurantBuisnessLogicContractTests
public void DeleteRestaurant_StorageThrowError_ThrowException_Test()
{
//Arrange
_restaurantStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_restaurantStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException(), StringLocalizerMockCreator.GetObject()));
//Act&Assert
Assert.That(() => _restaurantBuisnessLogicContract.DeleteRestaurant(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_restaurantStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);

Some files were not shown because too many files have changed in this diff Show More