Compare commits
33 Commits
Task_2_Bui
...
Task_5_Sal
| Author | SHA1 | Date | |
|---|---|---|---|
| 50a0807cdf | |||
| 4abc5ebcfb | |||
| 681ae1d484 | |||
| 1b9cbc274d | |||
| 55c269a934 | |||
| 7e8e7a5ee8 | |||
| b4c6d0110a | |||
| f9cfe3dc00 | |||
| 5c1b458ffd | |||
| 2b92a2b9bd | |||
| c960ed7320 | |||
| 4393d5097e | |||
| f1c2d1d3d8 | |||
| cea9386132 | |||
| 620832b1a8 | |||
| f886b16532 | |||
| db9da65e76 | |||
| 629e20e498 | |||
| f89577598a | |||
| 99fa16d354 | |||
| 0706b6e6e5 | |||
| 472ad965a8 | |||
| 75d8b547cb | |||
| c0f3362f42 | |||
| d92732140c | |||
| 5924d86f90 | |||
| 03d8022433 | |||
| c1a09db223 | |||
| e64854e1f9 | |||
| 70b42a97f4 | |||
| bb93edd951 | |||
| d28bb63707 | |||
| f54c8ecd02 |
@@ -7,7 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiContrakts", "Sl
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiTests", "SladkieBulkiTests\SladkieBulkiTests.csproj", "{12312C3E-A15B-4563-BD04-BB814149B2F6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SladkieBulkiBusinessLogic", "SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj", "{004CA71C-5308-44C7-82DA-4098BA6F5F1C}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiBusinessLogic", "SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj", "{004CA71C-5308-44C7-82DA-4098BA6F5F1C}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiDatabase", "SladkieBulkiDatabase\SladkieBulkiDatabase.csproj", "{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SladkieBulkiWedApi", "SladkieBulkiWedApi\SladkieBulkiWedApi.csproj", "{8623B6C7-0506-4C36-A337-CA363B92FFA2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -27,6 +31,14 @@ Global
|
||||
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8623B6C7-0506-4C36-A337-CA363B92FFA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8623B6C7-0506-4C36-A337-CA363B92FFA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8623B6C7-0506-4C36-A337-CA363B92FFA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8623B6C7-0506-4C36-A337-CA363B92FFA2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -13,10 +13,10 @@ internal class PostBusinessLogicContract(IPostStorageContract postStorageContrac
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
||||
|
||||
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
|
||||
public List<PostDataModel> GetAllPosts()
|
||||
{
|
||||
_logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
|
||||
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
||||
_logger.LogInformation("GetAllPosts");
|
||||
return _postStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetAllDataOfPost(string postId)
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SladkieBulkiContrakts.BusinessLogicsContracts;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.Extensions;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SladkieBulkiBusinessLogic.Implementations;
|
||||
|
||||
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, IProductionStorageContract productionStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract
|
||||
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, IProductionStorageContract productionStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger, IConfigurationSalary сonfiguration) : ISalaryBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly ISalaryStorageContract _salaryStorageContract =
|
||||
@@ -22,6 +21,9 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC
|
||||
postStorageContract;
|
||||
private readonly IWorkerStorageContract _workerStorageContract =
|
||||
workerStorageContract;
|
||||
private readonly IConfigurationSalary _salaryConfiguration = сonfiguration;
|
||||
private readonly object _lockObject = new();
|
||||
|
||||
|
||||
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
@@ -59,13 +61,87 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC
|
||||
var workers = _workerStorageContract.GetList() ?? throw new NullListException();
|
||||
foreach (var worker in workers)
|
||||
{
|
||||
var sales = _productionStorageContract.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 = _productionStorageContract.GetList(startDate, finishDate, workerId: worker.Id) ?? throw new NullListException();
|
||||
var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException();
|
||||
var salary = post.ConfigurationModel switch
|
||||
{
|
||||
null => 0,
|
||||
ManufacturerPostConfiguration cpc => CalculateSalaryForCashier(sales, startDate, finishDate, cpc),
|
||||
PackerPostConfiguration spc => CalculateSalaryForSupervisor(startDate, finishDate, spc),
|
||||
PostConfiguration pc => pc.Rate,
|
||||
};
|
||||
_logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary);
|
||||
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double CalculateSalaryForCashier(List<ProductionDataModel> sales, DateTime startDate, DateTime finishDate, ManufacturerPostConfiguration config)
|
||||
{
|
||||
|
||||
int maxConcurrency = сonfiguration.MaxParallelThreads;
|
||||
var semaphore = new SemaphoreSlim(maxConcurrency);
|
||||
|
||||
var tasks = new List<Task>();
|
||||
var calcPercent = 0.0;
|
||||
|
||||
for (var date = startDate; date < finishDate; date = date.AddDays(1))
|
||||
{
|
||||
var dateCopy = date; // Нужно, чтобы замыкание захватило корректную дату
|
||||
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
await semaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
var salesInDay = sales
|
||||
.Where(x => x.ProductionDate >= dateCopy && x.ProductionDate < dateCopy.AddDays(1))
|
||||
.ToArray();
|
||||
|
||||
if (salesInDay.Length > 0)
|
||||
{
|
||||
lock (_lockObject)
|
||||
{
|
||||
calcPercent += (salesInDay.Sum(x => x.Sum) / salesInDay.Length) * config.SalePercent;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
var calcBonusTask = Task.Run(() =>
|
||||
{
|
||||
return sales.Where(x => x.Sum > _salaryConfiguration.ExtraSaleSum).Sum(x => x.Sum) * config.BonusForExtraSales;
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
Task.WaitAll(tasks.ToArray()); // Ждём завершения всех задач по дням
|
||||
calcBonusTask.Wait(); // Ждём завершения бонуса
|
||||
}
|
||||
catch (AggregateException agEx)
|
||||
{
|
||||
foreach (var ex in agEx.InnerExceptions)
|
||||
_logger.LogError(ex, "Error in the cashier payroll process");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return config.Rate + calcPercent + calcBonusTask.Result;
|
||||
}
|
||||
|
||||
private double CalculateSalaryForSupervisor(DateTime startDate, DateTime finishDate, PackerPostConfiguration config)
|
||||
{
|
||||
try
|
||||
{
|
||||
return config.Rate + config.PersonalCountPack * _workerStorageContract.GetWorkerTrend(startDate, finishDate);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in the supervisor payroll process");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ using System.Text.Json;
|
||||
|
||||
namespace SladkieBulkiBusinessLogic.Implementations;
|
||||
|
||||
internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract,ILogger logger) : IWorkerBusinessLogicContract
|
||||
internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract
|
||||
{
|
||||
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
||||
|
||||
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
|
||||
{
|
||||
@@ -96,4 +96,4 @@ internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageC
|
||||
}
|
||||
_workerStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SladkieBulkiWedApi" />
|
||||
<InternalsVisibleTo Include="SladkieBulkiTests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts;
|
||||
|
||||
public interface IIngredientAdapter
|
||||
{
|
||||
IngredientOperationResponse GetList();
|
||||
|
||||
IngredientOperationResponse GetElement(string data);
|
||||
|
||||
IngredientOperationResponse RegisterIngredient(IngredientBindingModel ingredientModel);
|
||||
|
||||
IngredientOperationResponse ChangeIngredientInfo(IngredientBindingModel ingredientModel);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts;
|
||||
|
||||
public interface IPostAdapter
|
||||
{
|
||||
PostOperationResponse GetList();
|
||||
|
||||
PostOperationResponse GetHistory(string id);
|
||||
|
||||
PostOperationResponse GetElement(string data);
|
||||
|
||||
PostOperationResponse RegisterPost(PostBindingModel postModel);
|
||||
|
||||
PostOperationResponse ChangePostInfo(PostBindingModel postModel);
|
||||
|
||||
PostOperationResponse RemovePost(string id);
|
||||
|
||||
PostOperationResponse RestorePost(string id);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts;
|
||||
|
||||
public interface IProductAdapter
|
||||
{
|
||||
ProductOperationResponse GetList();
|
||||
ProductOperationResponse GetElement(string data);
|
||||
ProductOperationResponse RegisterProduct(ProductBindingModel model);
|
||||
ProductOperationResponse ChangeProductInfo(ProductBindingModel model);
|
||||
ProductOperationResponse DeleteProduct(string id);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts;
|
||||
|
||||
public interface IProductionAdapter
|
||||
{
|
||||
ProductionOperationResponse GetList(DateTime? from = null, DateTime? to = null, string? workerId = null, string? productId = null);
|
||||
ProductionOperationResponse GetElement(string id);
|
||||
ProductionOperationResponse RegisterProduction(ProductionBindingModel model);
|
||||
ProductionOperationResponse CancelProduction(string id);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts;
|
||||
public interface ISalaryAdapter
|
||||
{
|
||||
SalaryOperationResponse GetListByPeriod(DateTime fromDate, DateTime toDate);
|
||||
|
||||
SalaryOperationResponse GetListByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId);
|
||||
|
||||
SalaryOperationResponse CalculateSalary(DateTime date);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts;
|
||||
|
||||
public interface IWorkerAdapter
|
||||
{
|
||||
WorkerOperationResponse GetList(bool includeDeleted);
|
||||
|
||||
WorkerOperationResponse GetPostList(string id, bool includeDeleted);
|
||||
|
||||
WorkerOperationResponse GetListByBirthDate(DateTime fromDate, DateTime toDate, bool includeDeleted);
|
||||
|
||||
WorkerOperationResponse GetListByEmploymentDate(DateTime fromDate, DateTime toDate, bool includeDeleted);
|
||||
|
||||
WorkerOperationResponse GetElement(string data);
|
||||
|
||||
WorkerOperationResponse RegisterWorker(WorkerBindingModel workerModel);
|
||||
|
||||
WorkerOperationResponse ChangeWorkerInfo(WorkerBindingModel workerModel);
|
||||
|
||||
WorkerOperationResponse RemoveWorker(string id);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class IngredientOperationResponse : OperationResponse
|
||||
{
|
||||
public static IngredientOperationResponse OK(List<IngredientViewModel> data) => OK<IngredientOperationResponse, List<IngredientViewModel>>(data);
|
||||
|
||||
public static IngredientOperationResponse OK(IngredientViewModel data) => OK<IngredientOperationResponse, IngredientViewModel>(data);
|
||||
|
||||
public static IngredientOperationResponse NoContent() => NoContent<IngredientOperationResponse>();
|
||||
|
||||
public static IngredientOperationResponse NotFound(string message) => NotFound<IngredientOperationResponse>(message);
|
||||
|
||||
public static IngredientOperationResponse BadRequest(string message) => BadRequest<IngredientOperationResponse>(message);
|
||||
|
||||
public static IngredientOperationResponse InternalServerError(string message) => InternalServerError<IngredientOperationResponse>(message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class PostOperationResponse : OperationResponse
|
||||
{
|
||||
public static PostOperationResponse OK(List<PostViewModel> data) => OK<PostOperationResponse, List<PostViewModel>>(data);
|
||||
|
||||
public static PostOperationResponse OK(PostViewModel data) => OK<PostOperationResponse, PostViewModel>(data);
|
||||
|
||||
public static PostOperationResponse NoContent() => NoContent<PostOperationResponse>();
|
||||
|
||||
public static PostOperationResponse NotFound(string message) => NotFound<PostOperationResponse>(message);
|
||||
|
||||
public static PostOperationResponse BadRequest(string message) => BadRequest<PostOperationResponse>(message);
|
||||
|
||||
public static PostOperationResponse InternalServerError(string message) => InternalServerError<PostOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class ProductOperationResponse : OperationResponse
|
||||
{
|
||||
public static ProductOperationResponse OK(List<ProductViewModel> data) =>
|
||||
OK<ProductOperationResponse, List<ProductViewModel>>(data);
|
||||
|
||||
public static ProductOperationResponse OK(ProductViewModel data) =>
|
||||
OK<ProductOperationResponse, ProductViewModel>(data);
|
||||
|
||||
public static ProductOperationResponse NoContent() =>
|
||||
NoContent<ProductOperationResponse>();
|
||||
|
||||
public static ProductOperationResponse NotFound(string message) =>
|
||||
NotFound<ProductOperationResponse>(message);
|
||||
|
||||
public static ProductOperationResponse BadRequest(string message) =>
|
||||
BadRequest<ProductOperationResponse>(message);
|
||||
|
||||
public static ProductOperationResponse InternalServerError(string message) =>
|
||||
InternalServerError<ProductOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
|
||||
|
||||
public class ProductionOperationResponse : OperationResponse
|
||||
{
|
||||
public static ProductionOperationResponse OK(List<ProductionViewModel> data) =>
|
||||
OK<ProductionOperationResponse, List<ProductionViewModel>>(data);
|
||||
|
||||
public static ProductionOperationResponse OK(ProductionViewModel data) =>
|
||||
OK<ProductionOperationResponse, ProductionViewModel>(data);
|
||||
|
||||
public static ProductionOperationResponse NoContent() =>
|
||||
NoContent<ProductionOperationResponse>();
|
||||
|
||||
public static ProductionOperationResponse NotFound(string message) =>
|
||||
NotFound<ProductionOperationResponse>(message);
|
||||
|
||||
public static ProductionOperationResponse BadRequest(string message) =>
|
||||
BadRequest<ProductionOperationResponse>(message);
|
||||
|
||||
public static ProductionOperationResponse InternalServerError(string message) =>
|
||||
InternalServerError<ProductionOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
public class SalaryOperationResponse : OperationResponse
|
||||
{
|
||||
public static SalaryOperationResponse OK(List<SalaryViewModel> data) => OK<SalaryOperationResponse, List<SalaryViewModel>>(data);
|
||||
|
||||
public static SalaryOperationResponse NoContent() => NoContent<SalaryOperationResponse>();
|
||||
|
||||
public static SalaryOperationResponse NotFound(string message) => NotFound<SalaryOperationResponse>(message);
|
||||
|
||||
public static SalaryOperationResponse BadRequest(string message) => BadRequest<SalaryOperationResponse>(message);
|
||||
|
||||
public static SalaryOperationResponse InternalServerError(string message) => InternalServerError<SalaryOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
namespace SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
|
||||
public class WorkerOperationResponse : OperationResponse
|
||||
{
|
||||
public static WorkerOperationResponse OK(List<WorkerViewModel> data) => OK<WorkerOperationResponse, List<WorkerViewModel>>(data);
|
||||
|
||||
public static WorkerOperationResponse OK(WorkerViewModel data) => OK<WorkerOperationResponse, WorkerViewModel>(data);
|
||||
|
||||
public static WorkerOperationResponse NoContent() => NoContent<WorkerOperationResponse>();
|
||||
|
||||
public static WorkerOperationResponse NotFound(string message) => NotFound<WorkerOperationResponse>(message);
|
||||
|
||||
public static WorkerOperationResponse BadRequest(string message) => BadRequest<WorkerOperationResponse>(message);
|
||||
|
||||
public static WorkerOperationResponse InternalServerError(string message) => InternalServerError<WorkerOperationResponse>(message);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.BindingModels;
|
||||
|
||||
public class IngredientBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
|
||||
public string? NameIngredients { get; set; }
|
||||
|
||||
public string? SizeInit { get; set; }
|
||||
|
||||
public double InitPrice { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace SladkieBulkiContrakts.BindingModels;
|
||||
|
||||
public class PostBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
|
||||
public string? PostId => Id;
|
||||
|
||||
public string? PostName { get; set; }
|
||||
|
||||
public string? PostType { get; set; }
|
||||
|
||||
public string? ConfigurationJson { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.BindingModels;
|
||||
|
||||
|
||||
public class ProductBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public double UnitPrice { get; set; }
|
||||
public string? ProductType { get; set; }
|
||||
public List<IngredientBindingModel> Ingredients { get; set; } = new();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.BindingModels;
|
||||
public class ProductionBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public DateTime ProductionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public double Sum { get; set; }
|
||||
public string WorkerId { get; set; } = default!;
|
||||
public string ProductId { get; set; } = default!;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace SladkieBulkiContrakts.BindingModels;
|
||||
|
||||
public class WorkerBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? FIO { get; set; }
|
||||
public DateTime BirthDate { get; set; }
|
||||
public DateTime EmploymentDate { get; set; }
|
||||
public string? PostId { get; set; }
|
||||
public string? Email { get; set; }
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace SladkieBulkiContrakts.BusinessLogicsContracts;
|
||||
|
||||
public interface IPostBusinessLogicContract
|
||||
{
|
||||
List<PostDataModel> GetAllPosts(bool onlyActive);
|
||||
List<PostDataModel> GetAllPosts();
|
||||
List<PostDataModel> GetAllDataOfPost(string postId);
|
||||
PostDataModel GetPostByData(string data);
|
||||
void InsertPost(PostDataModel postDataModel);
|
||||
|
||||
@@ -8,10 +8,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class IngredientDataModel(string id, string nameIngridients, string sizeInit, double initPrice) : IValidation
|
||||
public class IngredientDataModel(string id, string NameIngredients, string sizeInit, double initPrice) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string NameIngridients { get; private set; } = nameIngridients;
|
||||
public string NameIngredients { get; private set; } = NameIngredients;
|
||||
public string SizeInit { get; private set; } = sizeInit;
|
||||
public double InitPrice { get; private set; } = initPrice;
|
||||
|
||||
@@ -21,8 +21,8 @@ public class IngredientDataModel(string id, string nameIngridients, string sizeI
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (NameIngridients.IsEmpty())
|
||||
throw new ValidationException("Field NameIngridients is empty");
|
||||
if (NameIngredients.IsEmpty())
|
||||
throw new ValidationException("Field NameIngredients is empty");
|
||||
if (SizeInit.IsEmpty())
|
||||
throw new ValidationException("Field SizeInit is empty");
|
||||
if (InitPrice <= 0)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Extensions;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -10,14 +13,29 @@ using System.Xml;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation
|
||||
public class PostDataModel(string postId, string postName, PostType postType, PostConfiguration configuration) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
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;
|
||||
public bool IsActual { get; private set; } = isActual;
|
||||
public DateTime ChangeDate { get; private set; } = changeDate;
|
||||
|
||||
public PostConfiguration ConfigurationModel { get; private set; } = configuration;
|
||||
|
||||
public PostDataModel(string postId, string postName, PostType postType, string configurationJson) : this(postId, postName, postType, (PostConfiguration)null)
|
||||
{
|
||||
var obj = JToken.Parse(configurationJson);
|
||||
if (obj is not null)
|
||||
{
|
||||
ConfigurationModel = obj.Value<string>("Type") switch
|
||||
{
|
||||
nameof(ManufacturerPostConfiguration) => JsonConvert.DeserializeObject<ManufacturerPostConfiguration>(configurationJson)!,
|
||||
nameof(PackerPostConfiguration) => JsonConvert.DeserializeObject<PackerPostConfiguration>(configurationJson)!,
|
||||
_ => JsonConvert.DeserializeObject<PostConfiguration>(configurationJson)!,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
@@ -33,7 +51,10 @@ public class PostDataModel(string id, string postName, PostType postType, double
|
||||
if (PostType == PostType.None)
|
||||
throw new ValidationException("Field PostType is empty");
|
||||
|
||||
if (Salary <= 0)
|
||||
throw new ValidationException("Field Salary is empty");
|
||||
if (ConfigurationModel is null)
|
||||
throw new ValidationException("Field ConfigurationModel is not initialized");
|
||||
|
||||
if (ConfigurationModel!.Rate <= 0)
|
||||
throw new ValidationException("Field Rate is less or equal zero");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,72 @@
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.Extensions;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class ProductDataModel (string id, string name, string description, ProductType productType, List<ProductIngredientDataModel> ingredients, int unitPrice, bool isDeleted) : IValidation
|
||||
|
||||
|
||||
public class ProductDataModel : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string Id { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public ProductType ProductType { get; private set; }
|
||||
public List<ProductIngredientDataModel> Ingredients { get; private set; }
|
||||
public double UnitPrice { get; private set; }
|
||||
public bool IsDeleted { get; private set; }
|
||||
|
||||
public string Name { get; private set; } = name;
|
||||
// 👇 Пустой конструктор для сериализации, EF, AutoMapper
|
||||
public ProductDataModel()
|
||||
{
|
||||
Ingredients = new List<ProductIngredientDataModel>();
|
||||
}
|
||||
|
||||
public string Description { get; private set; } = description;
|
||||
|
||||
public ProductType ProductType { get; private set; } = productType;
|
||||
|
||||
public List<ProductIngredientDataModel> Ingredients { get; private set; } = ingredients;
|
||||
|
||||
public int UnitPrice { get; private set; } = unitPrice;
|
||||
|
||||
public bool IsDeleted { get; private set; } = isDeleted;
|
||||
public ProductDataModel(string id, string name, string description, ProductType productType, List<ProductIngredientDataModel> ingredients, double unitPrice, bool isDeleted)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Description = description;
|
||||
ProductType = productType;
|
||||
Ingredients = ingredients ?? new List<ProductIngredientDataModel>();
|
||||
UnitPrice = unitPrice;
|
||||
IsDeleted = isDeleted;
|
||||
}
|
||||
public ProductDataModel(
|
||||
string id,
|
||||
string name,
|
||||
string description,
|
||||
ProductType productType,
|
||||
List<ProductIngredientDataModel> ingredients,
|
||||
double unitPrice)
|
||||
: this(id, name, description, productType, ingredients, unitPrice, false) { }
|
||||
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (Name.IsEmpty())
|
||||
throw new ValidationException("Field Name is empty");
|
||||
if (Description.IsEmpty())
|
||||
throw new ValidationException("Field Description is empty");
|
||||
if (ProductType == ProductType.None)
|
||||
throw new ValidationException("Field ProductType is empty");
|
||||
if (UnitPrice <= 0)
|
||||
throw new ValidationException("Field UnitPrice must be greater than zero");
|
||||
if (Ingredients == null || Ingredients.Count == 0)
|
||||
throw new ValidationException("No ingredients defined");
|
||||
}
|
||||
// Валидация
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
|
||||
if (Name.IsEmpty())
|
||||
throw new ValidationException("Field Name is empty");
|
||||
|
||||
if (Description.IsEmpty())
|
||||
throw new ValidationException("Field Description is empty");
|
||||
|
||||
if (ProductType == ProductType.None)
|
||||
throw new ValidationException("Field ProductType is empty");
|
||||
|
||||
if (UnitPrice <= 0)
|
||||
throw new ValidationException("Field UnitPrice must be greater than zero");
|
||||
|
||||
if (Ingredients == null || Ingredients.Count == 0)
|
||||
throw new ValidationException("No ingredients defined");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,16 +10,22 @@ namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation
|
||||
{
|
||||
private readonly WorkerDataModel? _worker;
|
||||
public string WorkerId { get; private set; } = workerId;
|
||||
public DateTime SalaryDate { get; private set; } = salaryDate;
|
||||
public double Salary { get; private set; } = workerSalary;
|
||||
public double WorkerSalary { get; private set; } = workerSalary;
|
||||
public string WorkerFIO => _worker?.FIO ?? string.Empty;
|
||||
public SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary, WorkerDataModel? worker) : this(workerId, salaryDate, workerSalary)
|
||||
{
|
||||
_worker = worker;
|
||||
}
|
||||
public void Validate()
|
||||
{
|
||||
if (WorkerId.IsEmpty())
|
||||
throw new ValidationException("Field WorkerId is empty");
|
||||
if (!WorkerId.IsGuid())
|
||||
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
|
||||
if (Salary <= 0)
|
||||
if (WorkerSalary <= 0)
|
||||
throw new ValidationException("Field Salary is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,13 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
using System.Xml;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Numerics;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted, string email) : IValidation
|
||||
{
|
||||
private readonly PostDataModel? _post;
|
||||
public string Id { get; private set; } = id;
|
||||
public string FIO { get; private set; } = fio;
|
||||
public string PostId { get; private set; } = postId;
|
||||
@@ -21,6 +23,15 @@ public class WorkerDataModel(string id, string fio, string postId, DateTime birt
|
||||
public DateTime EmploymentDate { get; private set; } = employmentDate;
|
||||
public bool IsDeleted { get; private set; } = isDeleted;
|
||||
public string Email { get; private set; } = email;
|
||||
|
||||
public string PostName => _post?.PostName ?? string.Empty;
|
||||
public WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted, string email, PostDataModel post) : this(id, fio, postId, birthDate, employmentDate, isDeleted, email)
|
||||
{
|
||||
_post = post;
|
||||
}
|
||||
|
||||
public WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, string email) : this(id, fio, postId, birthDate, employmentDate, false, email) { }
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Exceptions;
|
||||
|
||||
public class ElementDeletedException : Exception
|
||||
{
|
||||
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure;
|
||||
|
||||
public interface IConfigurationDatabase
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure;
|
||||
|
||||
public interface IConfigurationSalary
|
||||
{
|
||||
double ExtraSaleSum { get; }
|
||||
int MaxParallelThreads { get; }
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure;
|
||||
|
||||
public class OperationResponse
|
||||
{
|
||||
protected HttpStatusCode StatusCode { get; set; }
|
||||
|
||||
protected object? Result { get; set; }
|
||||
|
||||
public IActionResult GetResponse(HttpRequest request, HttpResponse response)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ArgumentNullException.ThrowIfNull(response);
|
||||
|
||||
response.StatusCode = (int)StatusCode;
|
||||
|
||||
if (Result is null)
|
||||
{
|
||||
return new StatusCodeResult((int)StatusCode);
|
||||
}
|
||||
|
||||
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 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 };
|
||||
|
||||
protected static TResult NotFound<TResult>(string? errorMessage = null) where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.NotFound, Result = errorMessage };
|
||||
|
||||
protected static TResult InternalServerError<TResult>(string? errorMessage = null) where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.InternalServerError, Result = errorMessage };
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
|
||||
public class ManufacturerPostConfiguration : PostConfiguration
|
||||
{
|
||||
public override string Type => nameof(ManufacturerPostConfiguration);
|
||||
|
||||
public double SalePercent { get; set; }
|
||||
|
||||
public double BonusForExtraSales { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
|
||||
public class PackerPostConfiguration : PostConfiguration
|
||||
{
|
||||
public override string Type => nameof(PackerPostConfiguration);
|
||||
|
||||
public double PersonalCountPack { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
|
||||
public class PostConfiguration
|
||||
{
|
||||
public virtual string Type => nameof(PostConfiguration);
|
||||
|
||||
public double Rate { get; set; }
|
||||
}
|
||||
|
||||
@@ -6,4 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace SladkieBulkiContrakts.StoragesContarcts;
|
||||
|
||||
public interface IPostStorageContract
|
||||
{
|
||||
List<PostDataModel> GetList(bool onlyActual = true);
|
||||
List<PostDataModel> GetList();
|
||||
List<PostDataModel> GetPostWithHistory(string postId);
|
||||
PostDataModel? GetElementById(string id);
|
||||
PostDataModel? GetElementByName(string name);
|
||||
|
||||
@@ -15,4 +15,5 @@ public interface IWorkerStorageContract
|
||||
void AddElement(WorkerDataModel workerDataModel);
|
||||
void UpdElement(WorkerDataModel workerDataModel);
|
||||
void DelElement(string id);
|
||||
}
|
||||
int GetWorkerTrend(DateTime fromPeriod, DateTime toPeriod);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
public class IngredientViewModel
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
public string NameIngredients { get; set; } = string.Empty;
|
||||
|
||||
public string SizeInit { get; set; } = string.Empty;
|
||||
|
||||
public double InitPrice { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
public class PostViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
|
||||
public required string PostName { get; set; }
|
||||
|
||||
public required string PostType { get; set; }
|
||||
|
||||
public required string Configuration { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
public class ProductViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string Name { get; set; }
|
||||
public required string Description { get; set; }
|
||||
public required double UnitPrice { get; set; }
|
||||
public required string ProductType { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public List<IngredientViewModel> Ingredients { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
public class ProductionViewModel
|
||||
{
|
||||
public string Id { get; set; } = default!;
|
||||
public DateTime ProductionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public double Sum { get; set; }
|
||||
public string WorkerId { get; set; } = default!;
|
||||
public string ProductId { get; set; } = default!;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
public class SalaryViewModel
|
||||
{
|
||||
public required string WorkerId { get; set; }
|
||||
public required string WorkerFIO { get; set; }
|
||||
public DateTime SalaryDate { get; set; }
|
||||
public double WorkerSalary { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
public class WorkerViewModel
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string FIO { get; set; }
|
||||
public required string PostId { get; set; }
|
||||
public required string PostName { get; set; }
|
||||
public required string Email { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public DateTime BirthDate { get; set; }
|
||||
public DateTime EmploymentDate { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase;
|
||||
|
||||
class DefaultConfigurationDatabase : IConfigurationDatabase
|
||||
{
|
||||
public string ConnectionString => "";
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class IngredientStorageContract : IIngredientStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public IngredientStorageContract(SladkieBulkiDbContext sladkieBulkiDbContext)
|
||||
{
|
||||
_dbContext = sladkieBulkiDbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Ingredient, IngredientDataModel>();
|
||||
cfg.CreateMap<IngredientDataModel, Ingredient>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<IngredientDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Ingredients.Select(x => _mapper.Map<IngredientDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public IngredientDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<IngredientDataModel>(GetIngredientById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public IngredientDataModel? GetElementByName(string nameIngredients)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<IngredientDataModel>(_dbContext.Ingredients.FirstOrDefault(x => x.NameIngredients == nameIngredients));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<IngredientDataModel>? GetElementBySizeUnit(string sizeInit)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ingredients = _dbContext.Ingredients.Where(x => x.SizeInit == sizeInit).ToList();
|
||||
|
||||
return _mapper.Map<List<IngredientDataModel>>(ingredients);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(IngredientDataModel ingredientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Ingredients.Add(_mapper.Map<Ingredient>(ingredientDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", ingredientDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Ingredients_NameIngridients" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("NameIngredients", ingredientDataModel.NameIngredients);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UpdElement(IngredientDataModel ingredientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetIngredientById(ingredientDataModel.Id) ?? throw new ElementNotFoundException(ingredientDataModel.Id);
|
||||
_dbContext.Ingredients.Update(_mapper.Map(ingredientDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Ingredients_NameIngridients" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("NameIngredients", ingredientDataModel.NameIngredients);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Ingredient? GetIngredientById(string id) => _dbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class PostStorageContract : IPostStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public PostStorageContract(SladkieBulkiDbContext 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))
|
||||
.ForMember(x => x.Configuration, x => x.MapFrom(src => src.ConfigurationModel));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Posts.Select(x => _mapper.Map<PostDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetPostWithHistory(string postId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PostDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PostDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(PostDataModel postDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Posts.Add(_mapper.Map<Post>(postDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostId", postDataModel.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(PostDataModel postDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transaction = _dbContext.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id);
|
||||
if (!element.IsActual)
|
||||
{
|
||||
throw new ElementDeletedException(postDataModel.Id);
|
||||
}
|
||||
element.IsActual = false;
|
||||
_dbContext.SaveChanges();
|
||||
var newElement = _mapper.Map<Post>(postDataModel);
|
||||
_dbContext.Posts.Add(newElement);
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||
}
|
||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||
if (!element.IsActual)
|
||||
{
|
||||
throw new ElementDeletedException(id);
|
||||
}
|
||||
element.IsActual = false;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsActual = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class ProductStorageContract : IProductStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public ProductStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<ProductIngredient, ProductIngredientDataModel>();
|
||||
cfg.CreateMap<ProductIngredientDataModel, ProductIngredient>();
|
||||
cfg.CreateMap<Product, ProductDataModel>();
|
||||
cfg.CreateMap<ProductDataModel, Product>()
|
||||
.ForMember(x => x.IsDeleted, x => x.MapFrom(src => false))
|
||||
.ForMember(x => x.ProductIngredients, x => x.MapFrom(src => src.Ingredients));
|
||||
cfg.CreateMap<ProductHistory, ProductHistoryDataModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
|
||||
public List<ProductDataModel> GetList(bool onlyActive = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Products.Include(x => x.ProductIngredients).AsQueryable();
|
||||
if (onlyActive)
|
||||
{
|
||||
query = query.Where(x => !x.IsDeleted);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<ProductDataModel>(x))];
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ProductHistoryDataModel> GetHistoryByProductId(string productId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _dbContext.ProductHistories
|
||||
.Where(x => x.ProductId == productId)
|
||||
.OrderByDescending(x => x.ChangeDate)
|
||||
.Select(x => _mapper.Map<ProductHistoryDataModel>(x)).ToList();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ProductDataModel>(GetProductById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ProductDataModel>(
|
||||
_dbContext.Products.FirstOrDefault(x => x.Name == name && !x.IsDeleted));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(ProductDataModel productDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Products.Add(_mapper.Map<Product>(productDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", productDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException pgEx &&
|
||||
pgEx.ConstraintName == "IX_Products_Name_IsDeleted")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Name", productDataModel.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UpdElement(ProductDataModel productDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var transaction = _dbContext.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var element = GetProductById(productDataModel.Id)
|
||||
?? throw new ElementNotFoundException(productDataModel.Id);
|
||||
|
||||
if (element.IsDeleted)
|
||||
throw new ElementDeletedException(productDataModel.Id);
|
||||
|
||||
// Проверяем, если цена изменилась, то добавляем её в историю
|
||||
if (element.UnitPrice != productDataModel.UnitPrice)
|
||||
{
|
||||
_dbContext.ProductHistories.Add(new ProductHistory
|
||||
{
|
||||
ProductId = element.Id,
|
||||
OldPrice = element.UnitPrice
|
||||
});
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
// Обновляем флаг IsDeleted, если он изменился
|
||||
if (element.IsDeleted != productDataModel.IsDeleted)
|
||||
{
|
||||
element.IsDeleted = productDataModel.IsDeleted;
|
||||
}
|
||||
|
||||
// Маппим остальные данные
|
||||
_mapper.Map(productDataModel, element);
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException postgresEx && postgresEx.SqlState == "23505" && postgresEx.ConstraintName == "IX_Products_Name_IsDeleted")
|
||||
{
|
||||
// Обработка ошибки уникальности
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Name", productDataModel.Name);
|
||||
}
|
||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||
{
|
||||
// Перекидываем только специфичные ошибки
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// В случае других исключений — логируем ошибку
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetProductById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsDeleted = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Product? GetProductById(string id) =>
|
||||
_dbContext.Products.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class ProductionStorageContract : IProductionStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public ProductionStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Production, ProductionDataModel>();
|
||||
cfg.CreateMap<ProductionDataModel, Production>()
|
||||
.ForMember(dest => dest.Worker, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Product, opt => opt.Ignore());
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<ProductionDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null, string? productId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Productions.AsQueryable();
|
||||
|
||||
if (startDate is not null && endDate is not null)
|
||||
{
|
||||
query = query.Where(p => p.ProductionDate >= startDate && p.ProductionDate < endDate);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(workerId))
|
||||
{
|
||||
query = query.Where(p => p.WorkerId == workerId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(productId))
|
||||
{
|
||||
query = query.Where(p => p.ProductId == productId);
|
||||
}
|
||||
|
||||
return [.. query.Select(p => _mapper.Map<ProductionDataModel>(p))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductionDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ProductionDataModel>(GetProductionById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(ProductionDataModel productionDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Productions.Add(_mapper.Map<Production>(productionDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
var element = GetProductionById(id);
|
||||
if (element == null)
|
||||
{
|
||||
throw new ElementNotFoundException($"Element not found at value = {id}");
|
||||
}
|
||||
|
||||
_dbContext.Productions.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Production? GetProductionById(string id)
|
||||
{
|
||||
return _dbContext.Productions.FirstOrDefault(p => p.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiDatabase;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
internal class SalaryStorageContract : ISalaryStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SalaryStorageContract(SladkieBulkiDbContext 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.WorkerSalary));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? workerId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Salaries.Include(x => x.Worker)
|
||||
.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
||||
if (workerId is not null)
|
||||
{
|
||||
query = query.Where(x => x.WorkerId == workerId);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SalaryDataModel salaryDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class WorkerStorageContract : IWorkerStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public WorkerStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Post, PostDataModel>()
|
||||
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
|
||||
cfg.CreateMap<Worker, WorkerDataModel>();
|
||||
cfg.CreateMap<WorkerDataModel, Worker>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Workers.AsQueryable();
|
||||
if (onlyActive)
|
||||
{
|
||||
query = query.Where(x => !x.IsDeleted);
|
||||
}
|
||||
if (postId is not null)
|
||||
{
|
||||
query = query.Where(x => x.PostId == postId);
|
||||
}
|
||||
if (fromBirthDate is not null && toBirthDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.BirthDate >= fromBirthDate && x.BirthDate <= toBirthDate);
|
||||
}
|
||||
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
|
||||
}
|
||||
return [.. JoinPost(query).Select(x => _mapper.Map<WorkerDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public WorkerDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<WorkerDataModel>(GetWorkerById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public WorkerDataModel? GetElementByFIO(string fio)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(WorkerDataModel workerDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Workers.Add(_mapper.Map<Worker>(workerDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", workerDataModel.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(WorkerDataModel workerDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetWorkerById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id);
|
||||
_dbContext.Workers.Update(_mapper.Map(workerDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetWorkerById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsDeleted = true;
|
||||
element.DateOfDelete = DateTime.UtcNow;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private Worker? GetWorkerById(string id) => AddPost(_dbContext.Workers.FirstOrDefault(x => x.Id == id && !x.IsDeleted));
|
||||
|
||||
private IQueryable<Worker> JoinPost(IQueryable<Worker> query)
|
||||
=> query.GroupJoin(_dbContext.Posts.Where(x => x.IsActual), x => x.PostId, y => y.PostId, (x, y) => new { Worker = x, Post = y })
|
||||
.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));
|
||||
}
|
||||
363
SladkieBulki/SladkieBulkiDatabase/Migrations/20250520155308_FirstMigration.Designer.cs
generated
Normal file
363
SladkieBulki/SladkieBulkiDatabase/Migrations/20250520155308_FirstMigration.Designer.cs
generated
Normal file
@@ -0,0 +1,363 @@
|
||||
// <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 SladkieBulkiDatabase;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.Migrations
|
||||
{
|
||||
[DbContext(typeof(SladkieBulkiDbContext))]
|
||||
[Migration("20250520155308_FirstMigration")]
|
||||
partial class FirstMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("InitPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("NameIngredients")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("SizeInit")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NameIngredients")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Ingredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ChangeDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("IngredienId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("OldPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IngredienId");
|
||||
|
||||
b.ToTable("IngredientHistories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("ProductType")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("UnitPrice")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name", "IsDeleted")
|
||||
.IsUnique()
|
||||
.HasFilter("\"IsDeleted\" = FALSE");
|
||||
|
||||
b.ToTable("Products");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.Property<string>("ProductId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("IngredientId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("ProductId", "IngredientId");
|
||||
|
||||
b.HasIndex("IngredientId");
|
||||
|
||||
b.ToTable("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("ProductId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ProductionDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("WorkerId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ProductId");
|
||||
|
||||
b.HasIndex("WorkerId");
|
||||
|
||||
b.ToTable("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.Worker", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("BirthDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
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>("PostId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Workers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("IngredientHistory")
|
||||
.HasForeignKey("IngredienId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductHistories")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("IngredientId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Salary", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Salaries")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Navigation("IngredientHistory");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Navigation("ProductHistories");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
|
||||
b.Navigation("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Worker", b =>
|
||||
{
|
||||
b.Navigation("Productions");
|
||||
|
||||
b.Navigation("Salaries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class FirstMigration : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Ingredients",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "text", nullable: false),
|
||||
NameIngredients = table.Column<string>(type: "text", nullable: false),
|
||||
SizeInit = table.Column<string>(type: "text", nullable: false),
|
||||
InitPrice = table.Column<double>(type: "double precision", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Ingredients", x => x.Id);
|
||||
});
|
||||
|
||||
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),
|
||||
Name = table.Column<string>(type: "text", nullable: false),
|
||||
Description = table.Column<string>(type: "text", nullable: false),
|
||||
ProductType = table.Column<int>(type: "integer", nullable: false),
|
||||
UnitPrice = table.Column<int>(type: "integer", nullable: false),
|
||||
IsDeleted = table.Column<bool>(type: "boolean", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Products", 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),
|
||||
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),
|
||||
Email = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Workers", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "IngredientHistories",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "text", nullable: false),
|
||||
IngredienId = 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_IngredientHistories", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_IngredientHistories_Ingredients_IngredienId",
|
||||
column: x => x.IngredienId,
|
||||
principalTable: "Ingredients",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
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: "ProductIngredients",
|
||||
columns: table => new
|
||||
{
|
||||
ProductId = table.Column<string>(type: "text", nullable: false),
|
||||
IngredientId = table.Column<string>(type: "text", nullable: false),
|
||||
Count = table.Column<int>(type: "integer", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_ProductIngredients", x => new { x.ProductId, x.IngredientId });
|
||||
table.ForeignKey(
|
||||
name: "FK_ProductIngredients_Ingredients_IngredientId",
|
||||
column: x => x.IngredientId,
|
||||
principalTable: "Ingredients",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ProductIngredients_Products_ProductId",
|
||||
column: x => x.ProductId,
|
||||
principalTable: "Products",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Productions",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "text", nullable: false),
|
||||
ProductionDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
|
||||
Count = table.Column<int>(type: "integer", nullable: false),
|
||||
Sum = table.Column<double>(type: "double precision", nullable: false),
|
||||
WorkerId = table.Column<string>(type: "text", nullable: false),
|
||||
ProductId = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Productions", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Productions_Products_ProductId",
|
||||
column: x => x.ProductId,
|
||||
principalTable: "Products",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_Productions_Workers_WorkerId",
|
||||
column: x => x.WorkerId,
|
||||
principalTable: "Workers",
|
||||
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.CreateIndex(
|
||||
name: "IX_IngredientHistories_IngredienId",
|
||||
table: "IngredientHistories",
|
||||
column: "IngredienId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Ingredients_NameIngredients",
|
||||
table: "Ingredients",
|
||||
column: "NameIngredients",
|
||||
unique: true);
|
||||
|
||||
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_ProductIngredients_IngredientId",
|
||||
table: "ProductIngredients",
|
||||
column: "IngredientId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Productions_ProductId",
|
||||
table: "Productions",
|
||||
column: "ProductId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Productions_WorkerId",
|
||||
table: "Productions",
|
||||
column: "WorkerId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Products_Name_IsDeleted",
|
||||
table: "Products",
|
||||
columns: new[] { "Name", "IsDeleted" },
|
||||
unique: true,
|
||||
filter: "\"IsDeleted\" = FALSE");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Salaries_WorkerId",
|
||||
table: "Salaries",
|
||||
column: "WorkerId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "IngredientHistories");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Posts");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ProductHistories");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ProductIngredients");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Productions");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Salaries");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Ingredients");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Products");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Workers");
|
||||
}
|
||||
}
|
||||
}
|
||||
364
SladkieBulki/SladkieBulkiDatabase/Migrations/20250520203540_ChangeFieldsInPost.Designer.cs
generated
Normal file
364
SladkieBulki/SladkieBulkiDatabase/Migrations/20250520203540_ChangeFieldsInPost.Designer.cs
generated
Normal file
@@ -0,0 +1,364 @@
|
||||
// <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 SladkieBulkiDatabase;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.Migrations
|
||||
{
|
||||
[DbContext(typeof(SladkieBulkiDbContext))]
|
||||
[Migration("20250520203540_ChangeFieldsInPost")]
|
||||
partial class ChangeFieldsInPost
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("InitPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("NameIngredients")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("SizeInit")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NameIngredients")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Ingredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ChangeDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("IngredienId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("OldPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IngredienId");
|
||||
|
||||
b.ToTable("IngredientHistories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("ProductType")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("UnitPrice")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name", "IsDeleted")
|
||||
.IsUnique()
|
||||
.HasFilter("\"IsDeleted\" = FALSE");
|
||||
|
||||
b.ToTable("Products");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.Property<string>("ProductId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("IngredientId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("ProductId", "IngredientId");
|
||||
|
||||
b.HasIndex("IngredientId");
|
||||
|
||||
b.ToTable("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("ProductId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ProductionDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("WorkerId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ProductId");
|
||||
|
||||
b.HasIndex("WorkerId");
|
||||
|
||||
b.ToTable("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.Worker", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("BirthDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
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>("PostId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Workers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("IngredientHistory")
|
||||
.HasForeignKey("IngredienId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductHistories")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("IngredientId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Salary", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Salaries")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Navigation("IngredientHistory");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Navigation("ProductHistories");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
|
||||
b.Navigation("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Worker", b =>
|
||||
{
|
||||
b.Navigation("Productions");
|
||||
|
||||
b.Navigation("Salaries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
367
SladkieBulki/SladkieBulkiDatabase/Migrations/20250520222914_AddDateOfDeleteInWorker.Designer.cs
generated
Normal file
367
SladkieBulki/SladkieBulkiDatabase/Migrations/20250520222914_AddDateOfDeleteInWorker.Designer.cs
generated
Normal file
@@ -0,0 +1,367 @@
|
||||
// <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 SladkieBulkiDatabase;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.Migrations
|
||||
{
|
||||
[DbContext(typeof(SladkieBulkiDbContext))]
|
||||
[Migration("20250520222914_AddDateOfDeleteInWorker")]
|
||||
partial class AddDateOfDeleteInWorker
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("InitPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("NameIngredients")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("SizeInit")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NameIngredients")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Ingredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ChangeDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("IngredienId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("OldPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IngredienId");
|
||||
|
||||
b.ToTable("IngredientHistories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("ProductType")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("UnitPrice")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name", "IsDeleted")
|
||||
.IsUnique()
|
||||
.HasFilter("\"IsDeleted\" = FALSE");
|
||||
|
||||
b.ToTable("Products");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.Property<string>("ProductId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("IngredientId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("ProductId", "IngredientId");
|
||||
|
||||
b.HasIndex("IngredientId");
|
||||
|
||||
b.ToTable("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("ProductId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ProductionDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("WorkerId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ProductId");
|
||||
|
||||
b.HasIndex("WorkerId");
|
||||
|
||||
b.ToTable("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.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<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
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>("PostId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Workers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("IngredientHistory")
|
||||
.HasForeignKey("IngredienId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductHistories")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("IngredientId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Salary", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Salaries")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Navigation("IngredientHistory");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Navigation("ProductHistories");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
|
||||
b.Navigation("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Worker", b =>
|
||||
{
|
||||
b.Navigation("Productions");
|
||||
|
||||
b.Navigation("Salaries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using SladkieBulkiDatabase;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SladkieBulkiDatabase.Migrations
|
||||
{
|
||||
[DbContext(typeof(SladkieBulkiDbContext))]
|
||||
partial class SladkieBulkiDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("InitPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("NameIngredients")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("SizeInit")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NameIngredients")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Ingredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ChangeDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<string>("IngredienId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<double>("OldPrice")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IngredienId");
|
||||
|
||||
b.ToTable("IngredientHistories");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("ProductType")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<int>("UnitPrice")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name", "IsDeleted")
|
||||
.IsUnique()
|
||||
.HasFilter("\"IsDeleted\" = FALSE");
|
||||
|
||||
b.ToTable("Products");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.Property<string>("ProductId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("IngredientId")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.HasKey("ProductId", "IngredientId");
|
||||
|
||||
b.HasIndex("IngredientId");
|
||||
|
||||
b.ToTable("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("ProductId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<DateTime>("ProductionDate")
|
||||
.HasColumnType("timestamp without time zone");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<string>("WorkerId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ProductId");
|
||||
|
||||
b.HasIndex("WorkerId");
|
||||
|
||||
b.ToTable("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.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("SladkieBulkiDatabase.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<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
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>("PostId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Workers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.IngredientHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("IngredientHistory")
|
||||
.HasForeignKey("IngredienId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductHistory", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductHistories")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.ProductIngredient", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Ingredient", "Ingredient")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("IngredientId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("ProductIngredients")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Ingredient");
|
||||
|
||||
b.Navigation("Product");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Production", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Product", "Product")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("ProductId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Productions")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Product");
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Salary", b =>
|
||||
{
|
||||
b.HasOne("SladkieBulkiDatabase.Models.Worker", "Worker")
|
||||
.WithMany("Salaries")
|
||||
.HasForeignKey("WorkerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Worker");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Ingredient", b =>
|
||||
{
|
||||
b.Navigation("IngredientHistory");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Product", b =>
|
||||
{
|
||||
b.Navigation("ProductHistories");
|
||||
|
||||
b.Navigation("ProductIngredients");
|
||||
|
||||
b.Navigation("Productions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SladkieBulkiDatabase.Models.Worker", b =>
|
||||
{
|
||||
b.Navigation("Productions");
|
||||
|
||||
b.Navigation("Salaries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
22
SladkieBulki/SladkieBulkiDatabase/Models/Ingredient.cs
Normal file
22
SladkieBulki/SladkieBulkiDatabase/Models/Ingredient.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Ingredient
|
||||
{
|
||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string NameIngredients { get; set; }
|
||||
public required string SizeInit { get; set; }
|
||||
public double InitPrice { get; set; }
|
||||
|
||||
[ForeignKey("IngredientId")]
|
||||
public List<ProductIngredient>? ProductIngredients { get; set; }
|
||||
|
||||
[ForeignKey("IngredienId")]
|
||||
public List<IngredientHistory>? IngredientHistory { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class IngredientHistory
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public required string IngredienId { get; set; }
|
||||
public double OldPrice { get; set; }
|
||||
public DateTime ChangeDate { get; set; }
|
||||
public Ingredient Ingredient { get; set; }
|
||||
}
|
||||
20
SladkieBulki/SladkieBulkiDatabase/Models/Post.cs
Normal file
20
SladkieBulki/SladkieBulkiDatabase/Models/Post.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Post
|
||||
{
|
||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string PostId { get; set; }
|
||||
public required string PostName { get; set; }
|
||||
public PostType PostType { get; set; }
|
||||
public required PostConfiguration Configuration { get; set; }
|
||||
public bool IsActual { get; set; }
|
||||
public DateTime ChangeDate { get; set; }
|
||||
}
|
||||
30
SladkieBulki/SladkieBulkiDatabase/Models/Product.cs
Normal file
30
SladkieBulki/SladkieBulkiDatabase/Models/Product.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Product
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string Name { get; set; }
|
||||
public required string Description { get; set; }
|
||||
public ProductType ProductType { get; set; }
|
||||
public int UnitPrice { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
[ForeignKey("ProductId")]
|
||||
public List<ProductIngredient>? ProductIngredients { get; set; }
|
||||
|
||||
[ForeignKey("ProductId")]
|
||||
public List<Production>? Productions { get; set; }
|
||||
|
||||
[ForeignKey("ProductId")]
|
||||
public List<ProductHistory>? ProductHistories { get; set; }
|
||||
}
|
||||
16
SladkieBulki/SladkieBulkiDatabase/Models/ProductHistory.cs
Normal file
16
SladkieBulki/SladkieBulkiDatabase/Models/ProductHistory.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class ProductHistory
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string ProductId { get; set; }
|
||||
public double OldPrice { get; set; }
|
||||
public DateTime ChangeDate { get; set; } = DateTime.UtcNow;
|
||||
public Product? Product { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class ProductIngredient
|
||||
{
|
||||
public required string ProductId { get; set; }
|
||||
public required string IngredientId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Ingredient? Ingredient { get; set; }
|
||||
public Product? Product { get; set; }
|
||||
}
|
||||
22
SladkieBulki/SladkieBulkiDatabase/Models/Production.cs
Normal file
22
SladkieBulki/SladkieBulkiDatabase/Models/Production.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Production
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public DateTime ProductionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public double Sum { get; set; }
|
||||
public required string WorkerId { get; set; }
|
||||
public required string ProductId { get; set; }
|
||||
public Worker? Worker { get; set; }
|
||||
public Product? Product { get; set; }
|
||||
|
||||
|
||||
}
|
||||
18
SladkieBulki/SladkieBulkiDatabase/Models/Salary.cs
Normal file
18
SladkieBulki/SladkieBulkiDatabase/Models/Salary.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Salary
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string WorkerId { get; set; }
|
||||
public DateTime SalaryDate { get; set; }
|
||||
public double WorkerSalary { get; set; }
|
||||
public Worker? Worker { get; set; }
|
||||
}
|
||||
38
SladkieBulki/SladkieBulkiDatabase/Models/Worker.cs
Normal file
38
SladkieBulki/SladkieBulkiDatabase/Models/Worker.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(WorkerDataModel), ReverseMap = true)]
|
||||
internal class Worker
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string FIO { get; set; }
|
||||
public required string PostId { get; set; }
|
||||
public DateTime BirthDate { get; set; }
|
||||
public DateTime EmploymentDate { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public required string Email { get; set; }
|
||||
public DateTime? DateOfDelete { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
public Post? Post { get; set; }
|
||||
|
||||
[ForeignKey("WorkerId")]
|
||||
public List<Salary>? Salaries { get; set; }
|
||||
|
||||
[ForeignKey("WorkerId")]
|
||||
public List<Production>? Productions { get; set; }
|
||||
|
||||
public Worker AddPost(Post? post)
|
||||
{
|
||||
Post = post;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
16
SladkieBulki/SladkieBulkiDatabase/SampleContextFactory.cs
Normal file
16
SladkieBulki/SladkieBulkiDatabase/SampleContextFactory.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase;
|
||||
|
||||
internal class SampleContextFactory : IDesignTimeDbContextFactory<SladkieBulkiDbContext>
|
||||
{
|
||||
public SladkieBulkiDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
return new SladkieBulkiDbContext(new DefaultConfigurationDatabase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SladkieBulkiWedApi" />
|
||||
<InternalsVisibleTo Include="SladkieBulkiTests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
77
SladkieBulki/SladkieBulkiDatabase/SladkieBulkiDbContext.cs
Normal file
77
SladkieBulki/SladkieBulkiDatabase/SladkieBulkiDbContext.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
|
||||
|
||||
namespace SladkieBulkiDatabase;
|
||||
|
||||
internal class SladkieBulkiDbContext: DbContext
|
||||
{
|
||||
private readonly IConfigurationDatabase? _configurationDatabase;
|
||||
public SladkieBulkiDbContext(IConfigurationDatabase configurationDatabase)
|
||||
{
|
||||
_configurationDatabase = configurationDatabase;
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
|
||||
}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(12, 2));
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<Ingredient>().HasIndex(x => x.NameIngredients).IsUnique();
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostName, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostId, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<Product>()
|
||||
.HasIndex(x => new { x.Name, x.IsDeleted })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
|
||||
|
||||
modelBuilder.Entity<ProductIngredient>().HasKey(x => new { x.ProductId, x.IngredientId });
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.Property(x => x.Configuration)
|
||||
.HasColumnType("jsonb")
|
||||
.HasConversion(
|
||||
x => SerializePostConfiguration(x),
|
||||
x => DeserialzePostConfiguration(x)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public DbSet<Ingredient> Ingredients { get; set; }
|
||||
public DbSet<IngredientHistory> IngredientHistories { get; set; }
|
||||
public DbSet<Post> Posts { get; set; }
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<ProductHistory> ProductHistories { get; set; }
|
||||
public DbSet<Salary> Salaries { get; set; }
|
||||
public DbSet<Production> Productions { get; set; }
|
||||
public DbSet<ProductIngredient> ProductIngredients { 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(ManufacturerPostConfiguration) => JsonConvert.DeserializeObject<ManufacturerPostConfiguration>(jsonString)!,
|
||||
nameof(PackerPostConfiguration) => JsonConvert.DeserializeObject<PackerPostConfiguration>(jsonString)!,
|
||||
_ => JsonConvert.DeserializeObject<PostConfiguration>(jsonString)!,
|
||||
};
|
||||
}
|
||||
@@ -136,7 +136,7 @@ internal class IngredientBusinessLogicContractTests
|
||||
|
||||
// Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.NameIngridients, Is.EqualTo(name));
|
||||
Assert.That(element.NameIngredients, Is.EqualTo(name));
|
||||
_ingredientStorageContract.Verify(x =>
|
||||
x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
@@ -213,7 +213,7 @@ internal class IngredientBusinessLogicContractTests
|
||||
.Callback((IngredientDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id &&
|
||||
x.NameIngridients == record.NameIngridients &&
|
||||
x.NameIngredients == record.NameIngredients &&
|
||||
x.SizeInit == record.SizeInit &&
|
||||
x.InitPrice == record.InitPrice;
|
||||
});
|
||||
@@ -294,7 +294,7 @@ internal class IngredientBusinessLogicContractTests
|
||||
.Callback((IngredientDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id &&
|
||||
x.NameIngridients == record.NameIngridients &&
|
||||
x.NameIngredients == record.NameIngredients &&
|
||||
x.SizeInit == record.SizeInit &&
|
||||
x.InitPrice == record.InitPrice;
|
||||
});
|
||||
|
||||
@@ -1,456 +1,445 @@
|
||||
using Moq;
|
||||
using SladkieBulkiBusinessLogic.Implementations;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiBusinessLogic.Implementations;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
|
||||
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class PostBusinessLogicContractTests
|
||||
{
|
||||
private PostBusinessLogicContract _postBusinessLogicContract;
|
||||
private Mock<IPostStorageContract> _postStorageContract;
|
||||
private PostBusinessLogicContract _postBusinessLogicContract;
|
||||
private Mock<IPostStorageContract> _postStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_postStorageContract = new Mock<IPostStorageContract>();
|
||||
_postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_postStorageContract = new Mock<IPostStorageContract>();
|
||||
_postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_postStorageContract.Reset();
|
||||
}
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_postStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(),"name 1", PostType.Preform, 10, true, DateTime.UtcNow),
|
||||
new(Guid.NewGuid().ToString(), "name 2", PostType.Preform, 10, false, DateTime.UtcNow),
|
||||
new(Guid.NewGuid().ToString(), "name 3", PostType.Preform, 10, true, DateTime.UtcNow),
|
||||
};
|
||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
|
||||
var listAll = _postBusinessLogicContract.GetAllPosts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(listAll, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(listAll, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(),"name 1", PostType.Manufacturer, new PostConfiguration() { Rate = 10 }),
|
||||
new(Guid.NewGuid().ToString(), "name 2", PostType.Manufacturer, new PostConfiguration() { Rate = 10 }),
|
||||
new(Guid.NewGuid().ToString(), "name 3", PostType.Manufacturer, new PostConfiguration() { Rate = 10 }),
|
||||
};
|
||||
_postStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllPosts();
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_postStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
|
||||
var listAll = _postBusinessLogicContract.GetAllPosts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(listAll, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(listAll, Has.Count.EqualTo(0));
|
||||
});
|
||||
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllPosts();
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllPosts(), Throws.TypeOf<NullListException>());
|
||||
_postStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllPosts_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllPosts(), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(postId, "name 1", PostType.Preform, 10, true, DateTime.UtcNow),
|
||||
new(postId, "name 2", PostType.Preform, 10, false, DateTime.UtcNow)
|
||||
};
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(postId), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(postId, "name 1", PostType.Manufacturer, new PostConfiguration() { Rate = 10 }),
|
||||
new(postId, "name 2", PostType.Manufacturer, new PostConfiguration() { Rate = 10 })
|
||||
};
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(postId), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.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()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_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()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new PostDataModel(id, "name", PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _postBusinessLogicContract.GetPostByData(id);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new PostDataModel(id, "name", PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _postBusinessLogicContract.GetPostByData(id);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_GetByName_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postName = "name";
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
|
||||
//Act
|
||||
var element = _postBusinessLogicContract.GetPostByData(postName);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.PostName, Is.EqualTo(postName));
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_GetByName_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postName = "name";
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
|
||||
//Act
|
||||
var element = _postBusinessLogicContract.GetPostByData(postName);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.PostName, Is.EqualTo(postName));
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
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()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
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()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertPost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow.AddDays(-1));
|
||||
_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 &&
|
||||
x.ChangeDate == record.ChangeDate;
|
||||
[Test]
|
||||
public void InsertPost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, 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.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
|
||||
});
|
||||
//Act
|
||||
_postBusinessLogicContract.InsertPost(record);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
//Act
|
||||
_postBusinessLogicContract.InsertPost(record);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertPost_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertPost_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertPost_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertPost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow.AddDays(-1));
|
||||
_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 &&
|
||||
x.ChangeDate == record.ChangeDate;
|
||||
[Test]
|
||||
public void UpdatePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, 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.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
|
||||
});
|
||||
//Act
|
||||
_postBusinessLogicContract.UpdatePost(record);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
//Act
|
||||
_postBusinessLogicContract.UpdatePost(record);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Preform, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeletePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_postBusinessLogicContract.DeletePost(id);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_postBusinessLogicContract.DeletePost(id);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeletePost_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeletePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_postBusinessLogicContract.RestorePost(id);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_postBusinessLogicContract.RestorePost(id);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -180,27 +180,15 @@ internal class ProductBusinessLogicContractTests
|
||||
[Test]
|
||||
public void GetProductByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var expected = new ProductDataModel(productId, "Product Name", "Some description", ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 20, false);
|
||||
|
||||
_productStorageContract.Setup(x => x.GetElementById(productId)).Returns(expected);
|
||||
|
||||
// Act
|
||||
var actual = _productBusinessLogicContract.GetProductByData(productId);
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
Assert.That(actual.Ingredients, Is.EquivalentTo(expected.Ingredients));
|
||||
});
|
||||
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new ProductDataModel(id, "name", "desk", ProductType.Workpiece, [], 10, false);
|
||||
_productStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _productBusinessLogicContract.GetProductByData(id);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
@@ -280,7 +268,7 @@ internal class ProductBusinessLogicContractTests
|
||||
{
|
||||
// Arrange
|
||||
var flag = false;
|
||||
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false);
|
||||
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", "description", ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false);
|
||||
|
||||
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
|
||||
.Callback((ProductDataModel x) =>
|
||||
|
||||
@@ -4,7 +4,9 @@ using SladkieBulkiBusinessLogic.Implementations;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -20,6 +22,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
private Mock<IProductionStorageContract> _productionStorageContract;
|
||||
private Mock<IPostStorageContract> _postStorageContract;
|
||||
private Mock<IWorkerStorageContract> _workerStorageContract;
|
||||
private readonly ConfigurationSalaryTest _salaryConfigurationTest = new();
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
@@ -29,7 +32,8 @@ internal class SalaryBusinessLogicContractTests
|
||||
_postStorageContract = new Mock<IPostStorageContract>();
|
||||
_workerStorageContract = new Mock<IWorkerStorageContract>();
|
||||
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object,
|
||||
_productionStorageContract.Object, _postStorageContract.Object, _workerStorageContract.Object, new Mock<ILogger>().Object);
|
||||
_productionStorageContract.Object, _postStorageContract.Object, _workerStorageContract.Object, new Mock<ILogger>().Object, _salaryConfigurationTest);
|
||||
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
@@ -195,7 +199,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, saleSum, workerId, null)]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, postSalary, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new ManufacturerPostConfiguration { Rate = postSalary, SalePercent = 0.1 }));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru"),]);
|
||||
var sum = 0.0;
|
||||
@@ -203,7 +207,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
|
||||
.Callback((SalaryDataModel x) =>
|
||||
{
|
||||
sum = x.Salary;
|
||||
sum = x.WorkerSalary;
|
||||
});
|
||||
//Act
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||
@@ -230,7 +234,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker3Id, null),
|
||||
new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker3Id, null)]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = 2000 }));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns(list);
|
||||
//Act
|
||||
@@ -248,7 +252,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns([]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, postSalary, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = postSalary }));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
|
||||
var sum = 0.0;
|
||||
@@ -256,7 +260,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
|
||||
.Callback((SalaryDataModel x) =>
|
||||
{
|
||||
sum = x.Salary;
|
||||
sum = x.WorkerSalary;
|
||||
});
|
||||
//Act
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||
@@ -270,7 +274,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = 2000 }));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
|
||||
//Act&Assert
|
||||
@@ -298,7 +302,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 200.0, workerId, null)]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = 2000 }));
|
||||
//Act&Assert
|
||||
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
|
||||
}
|
||||
@@ -311,7 +315,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Throws(new StorageException(new InvalidOperationException()));
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = 2000 }));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
|
||||
//Act&Assert
|
||||
@@ -341,7 +345,7 @@ internal class SalaryBusinessLogicContractTests
|
||||
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 200.0, workerId, null)]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = 200 }));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
|
||||
@@ -71,12 +71,12 @@ internal class IngredientDataModelTests
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(ingredient.Id, Is.EqualTo(ingredientId));
|
||||
Assert.That(ingredient.NameIngridients, Is.EqualTo(ingredientName));
|
||||
Assert.That(ingredient.NameIngredients, Is.EqualTo(ingredientName));
|
||||
Assert.That(ingredient.SizeInit, Is.EqualTo(sizeInit));
|
||||
Assert.That(ingredient.InitPrice, Is.EqualTo(initPrice));
|
||||
});
|
||||
}
|
||||
|
||||
private static IngredientDataModel CreateDataModel(string? id, string? nameIngridients, string? sizeInit, double initPrice) =>
|
||||
new(id, nameIngridients, sizeInit, initPrice);
|
||||
private static IngredientDataModel CreateDataModel(string? id, string? NameIngredients, string? sizeInit, double initPrice) =>
|
||||
new(id, NameIngredients, sizeInit, initPrice);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -14,41 +15,49 @@ internal class PostDataModelTests
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var post = CreateDataModel(null, "name", PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
var post = CreateDataModel(null, "name", PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
post = CreateDataModel(string.Empty, "name", PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
post = CreateDataModel(string.Empty, "name", PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var post = CreateDataModel("id", "name", PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
var post = CreateDataModel("id", "name", PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PostNameIsEmptyTest()
|
||||
{
|
||||
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
|
||||
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Manufacturer, new PostConfiguration() { Rate = 10 });
|
||||
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PostTypeIsNoneTest()
|
||||
{
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow);
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, new PostConfiguration() { Rate = 10 });
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SalaryIsLessOrZeroTest()
|
||||
public void ConfigurationModelIsNullTest()
|
||||
{
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, 0, true, DateTime.UtcNow);
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, null);
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, -10, true, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void RateIsLessOrZeroTest()
|
||||
{
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = 0 });
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, new PostConfiguration() { Rate = -10 });
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
@@ -57,23 +66,20 @@ internal class PostDataModelTests
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var postName = "name";
|
||||
var postType = PostType.Preform;
|
||||
var salary = 10;
|
||||
var isActual = false;
|
||||
var changeDate = DateTime.UtcNow.AddDays(-1);
|
||||
var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate);
|
||||
var postType = PostType.Manufacturer;
|
||||
var configuration = new PostConfiguration() { Rate = 10 };
|
||||
var post = CreateDataModel(postId, postName, postType, configuration);
|
||||
Assert.That(() => post.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(post.Id, Is.EqualTo(postId));
|
||||
Assert.That(post.PostName, Is.EqualTo(postName));
|
||||
Assert.That(post.PostType, Is.EqualTo(postType));
|
||||
Assert.That(post.Salary, Is.EqualTo(salary));
|
||||
Assert.That(post.IsActual, Is.EqualTo(isActual));
|
||||
Assert.That(post.ChangeDate, Is.EqualTo(changeDate));
|
||||
Assert.That(post.ConfigurationModel, Is.EqualTo(configuration));
|
||||
Assert.That(post.ConfigurationModel.Rate, Is.EqualTo(configuration.Rate));
|
||||
});
|
||||
}
|
||||
|
||||
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
|
||||
new(id, postName, postType, salary, isActual, changeDate);
|
||||
}
|
||||
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, PostConfiguration configuration) =>
|
||||
new(id, postName, postType, configuration);
|
||||
}
|
||||
@@ -51,7 +51,7 @@ internal class SalaryDataModelTests
|
||||
{
|
||||
Assert.That(salary.WorkerId, Is.EqualTo(workerId));
|
||||
Assert.That(salary.SalaryDate, Is.EqualTo(salaryDate));
|
||||
Assert.That(salary.Salary, Is.EqualTo(workerSalary));
|
||||
Assert.That(salary.WorkerSalary, Is.EqualTo(workerSalary));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.Infrastructure;
|
||||
internal class ConfigurationDatabaseTest : IConfigurationDatabase
|
||||
{
|
||||
public string ConnectionString =>
|
||||
"Host=localhost;Port=5432;Database=rpp;Username=postgres;Password=postgres";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.Infrastructure;
|
||||
|
||||
class ConfigurationSalaryTest : IConfigurationSalary
|
||||
{
|
||||
public double ExtraSaleSum => 10;
|
||||
public int MaxParallelThreads { get; set; } = 4;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.Infrastructure;
|
||||
|
||||
internal class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>
|
||||
where TProgram : class
|
||||
{
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
var databaseConfig = services.SingleOrDefault(x => x.ServiceType == typeof(IConfigurationDatabase));
|
||||
if (databaseConfig is not null)
|
||||
services.Remove(databaseConfig);
|
||||
|
||||
var loggerFactory = services.SingleOrDefault(x => x.ServiceType == typeof(LoggerFactory));
|
||||
if (loggerFactory is not null)
|
||||
services.Remove(loggerFactory);
|
||||
|
||||
services.AddSingleton<IConfigurationDatabase, ConfigurationDatabaseTest>();
|
||||
});
|
||||
|
||||
builder.UseEnvironment("Development");
|
||||
|
||||
base.ConfigureWebHost(builder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using SladkieBulkiDatabase;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.Infrastructure;
|
||||
|
||||
internal static class SladkieBulkiDbContextExtensions
|
||||
{
|
||||
|
||||
public static Post InsertPostToDatabaseAndReturn(this SladkieBulkiDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Manufacturer, PostConfiguration? config = null, bool isActual = true, DateTime? changeDate = null)
|
||||
{
|
||||
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Configuration = config ?? new PostConfiguration() { Rate = 100 }, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
|
||||
dbContext.Posts.Add(post);
|
||||
dbContext.SaveChanges();
|
||||
return post;
|
||||
}
|
||||
|
||||
public static Product InsertProductWithIngredientsToDatabaseAndReturn(
|
||||
this SladkieBulkiDbContext dbContext,
|
||||
List<Ingredient> ingredients,
|
||||
string? id = null,
|
||||
string name = "test",
|
||||
string description = "desc",
|
||||
ProductType productType = ProductType.Production,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false)
|
||||
{
|
||||
var product = new Product
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
Name = name,
|
||||
Description = description,
|
||||
ProductType = productType,
|
||||
UnitPrice = unitPrice,
|
||||
IsDeleted = isDeleted,
|
||||
ProductIngredients = new List<ProductIngredient>()
|
||||
};
|
||||
|
||||
foreach (var ingredient in ingredients)
|
||||
{
|
||||
product.ProductIngredients.Add(new ProductIngredient
|
||||
{
|
||||
ProductId = product.Id,
|
||||
IngredientId = ingredient.Id,
|
||||
Count = 1 // или другое значение по-умолчанию
|
||||
});
|
||||
}
|
||||
|
||||
dbContext.Products.Add(product);
|
||||
dbContext.SaveChanges();
|
||||
return product;
|
||||
}
|
||||
public static bool ProductHasIngredients(
|
||||
this SladkieBulkiDbContext dbContext, string productId, List<string> ingredientIds)
|
||||
{
|
||||
var product = dbContext.Products
|
||||
.Include(p => p.ProductIngredients)
|
||||
.FirstOrDefault(p => p.Id == productId);
|
||||
|
||||
if (product == null)
|
||||
return false;
|
||||
|
||||
var productIngredientIds = product.ProductIngredients.Select(pi => pi.IngredientId).ToList();
|
||||
return ingredientIds.All(id => productIngredientIds.Contains(id));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static ProductHistory InsertProductHistoryToDatabaseAndReturn(this SladkieBulkiDbContext dbContext, string productId, double price = 10, DateTime? changeDate = null)
|
||||
{
|
||||
var productHistory = new ProductHistory() { Id = Guid.NewGuid().ToString(), ProductId = productId, OldPrice = price, ChangeDate = changeDate ?? DateTime.UtcNow };
|
||||
dbContext.ProductHistories.Add(productHistory);
|
||||
dbContext.SaveChanges();
|
||||
return productHistory;
|
||||
}
|
||||
|
||||
public static Salary InsertSalaryToDatabaseAndReturn(this SladkieBulkiDbContext dbContext, string workerId, double workerSalary = 1, DateTime? salaryDate = null)
|
||||
{
|
||||
var salary = new Salary() { WorkerId = workerId, WorkerSalary = workerSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
|
||||
dbContext.Salaries.Add(salary);
|
||||
dbContext.SaveChanges();
|
||||
return salary;
|
||||
}
|
||||
|
||||
public static Worker InsertWorkerToDatabaseAndReturn(this SladkieBulkiDbContext dbContext, string? id = null, string fio = "test", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false, DateTime? dateDelete = null, string? email = "1@mail.com")
|
||||
{
|
||||
var worker = new Worker() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted, DateOfDelete = dateDelete, Email = email};
|
||||
dbContext.Workers.Add(worker);
|
||||
dbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
|
||||
public static Ingredient InsertIngredientToDatabaseAndReturn(
|
||||
this SladkieBulkiDbContext dbContext,
|
||||
string? id = null,
|
||||
string nameIngredients = "test",
|
||||
string sizeInit = "kg",
|
||||
double initPrice = 10)
|
||||
{
|
||||
var ingredient = new Ingredient()
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
NameIngredients = nameIngredients,
|
||||
SizeInit = sizeInit,
|
||||
InitPrice = initPrice,
|
||||
};
|
||||
dbContext.Ingredients.Add(ingredient);
|
||||
dbContext.SaveChanges();
|
||||
return ingredient;
|
||||
}
|
||||
|
||||
public static Production InsertProductionToDatabaseAndReturn(
|
||||
this SladkieBulkiDbContext dbContext,
|
||||
string? id = null,
|
||||
DateTime? productionDate = null,
|
||||
int count = 1,
|
||||
double sum = 100,
|
||||
string? workerId = null,
|
||||
string? productId = null)
|
||||
{
|
||||
var production = new Production
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
ProductionDate = productionDate ?? DateTime.UtcNow,
|
||||
Count = count,
|
||||
Sum = sum,
|
||||
WorkerId = workerId ?? Guid.NewGuid().ToString(),
|
||||
ProductId = productId ?? Guid.NewGuid().ToString()
|
||||
};
|
||||
dbContext.Productions.Add(production);
|
||||
dbContext.SaveChanges();
|
||||
return production;
|
||||
}
|
||||
|
||||
public static Production? GetProductionFromDatabaseById(this SladkieBulkiDbContext dbContext, string id)
|
||||
=> dbContext.Productions.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static List<Production> GetProductionsFromDatabaseByPeriod(
|
||||
this SladkieBulkiDbContext dbContext,
|
||||
DateTime? startDate = null,
|
||||
DateTime? endDate = null,
|
||||
string? workerId = null,
|
||||
string? productId = null)
|
||||
{
|
||||
var query = dbContext.Productions.AsQueryable();
|
||||
|
||||
if (startDate is not null && endDate is not null)
|
||||
{
|
||||
query = query.Where(p => p.ProductionDate >= startDate && p.ProductionDate < endDate);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(workerId))
|
||||
{
|
||||
query = query.Where(p => p.WorkerId == workerId);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(productId))
|
||||
{
|
||||
query = query.Where(p => p.ProductId == productId);
|
||||
}
|
||||
return query.ToList();
|
||||
}
|
||||
|
||||
public static Ingredient? GetIngredientFromDatabaseById(this SladkieBulkiDbContext dbContext, string id)
|
||||
=> dbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static Ingredient? GetIngredientFromDatabaseByName(this SladkieBulkiDbContext dbContext, string name)
|
||||
=> dbContext.Ingredients.FirstOrDefault(x => x.NameIngredients == name);
|
||||
|
||||
|
||||
public static Post? GetPostFromDatabaseByPostId(this SladkieBulkiDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);
|
||||
|
||||
public static Post[] GetPostsFromDatabaseByPostId(this SladkieBulkiDbContext dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)];
|
||||
|
||||
public static Product? GetProductFromDatabaseById(this SladkieBulkiDbContext dbContext, string id) => dbContext.Products.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static Salary[] GetSalariesFromDatabaseByWorkerId(this SladkieBulkiDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.WorkerId == id)];
|
||||
|
||||
public static Worker? GetWorkerFromDatabaseById(this SladkieBulkiDbContext dbContext, string id) => dbContext.Workers.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
public static void RemoveIngredientsFromDatabase(this SladkieBulkiDbContext dbContext)
|
||||
=> dbContext.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
public static void RemovePostsFromDatabase(this SladkieBulkiDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
||||
|
||||
public static void RemoveProductionsFromDatabase(this SladkieBulkiDbContext dbContext)
|
||||
=> dbContext.ExecuteSqlRaw("TRUNCATE \"Productions\" CASCADE;");
|
||||
|
||||
public static void RemoveProductsFromDatabase(this SladkieBulkiDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE;");
|
||||
|
||||
public static void RemoveSalariesFromDatabase(this SladkieBulkiDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
||||
|
||||
public static void RemoveWorkersFromDatabase(this SladkieBulkiDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
|
||||
private static void ExecuteSqlRaw(this SladkieBulkiDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command);
|
||||
}
|
||||
@@ -1,16 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<PreserveCompilationContext>true</PreserveCompilationContext>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.16" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||
@@ -21,6 +32,8 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
|
||||
<ProjectReference Include="..\SladkieBulkiDatabase\SladkieBulkiDatabase.csproj" />
|
||||
<ProjectReference Include="..\SladkieBulkiWedApi\SladkieBulkiWedApi.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using SladkieBulkiDatabase;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
internal abstract class BaseStorageContractTest
|
||||
{
|
||||
protected SladkieBulkiDbContext SladkieBulkiDbContext { get; private set; }
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
SladkieBulkiDbContext = new SladkieBulkiDbContext(new ConfigurationDatabaseTest());
|
||||
|
||||
SladkieBulkiDbContext.Database.EnsureDeleted();
|
||||
SladkieBulkiDbContext.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.EnsureDeleted();
|
||||
SladkieBulkiDbContext.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class IngredientStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private IngredientStorageContract _ingredientStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_ingredientStorageContract = new IngredientStorageContract(SladkieBulkiDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"ProductIngredients\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var ingr = InsertToDbAndReturn(Guid.NewGuid().ToString(), "Мука", "кг", 25.5);
|
||||
InsertToDbAndReturn(Guid.NewGuid().ToString(), "Сахар", "кг", 30);
|
||||
InsertToDbAndReturn(Guid.NewGuid().ToString(), "Масло", "л", 100);
|
||||
|
||||
var list = _ingredientStorageContract.GetList();
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == ingr.Id), ingr);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _ingredientStorageContract.GetList();
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenExists_Test()
|
||||
{
|
||||
var ingr = InsertToDbAndReturn(Guid.NewGuid().ToString(), "Дрожжи", "г", 5);
|
||||
var element = _ingredientStorageContract.GetElementById(ingr.Id);
|
||||
AssertElement(element, ingr);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNotExists_Test()
|
||||
{
|
||||
var result = _ingredientStorageContract.GetElementById(Guid.NewGuid().ToString());
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var model = CreateModel(Guid.NewGuid().ToString(), "Соль", "г", 1.5);
|
||||
_ingredientStorageContract.AddElement(model);
|
||||
AssertElement(GetFromDb(model.Id), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenDuplicateId_Throws_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
InsertToDbAndReturn(id, "Молоко", "л", 50);
|
||||
var model = CreateModel(id, "Молоко", "л", 50);
|
||||
Assert.That(() => _ingredientStorageContract.AddElement(model), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
InsertToDbAndReturn(id, "Мёд", "л", 200);
|
||||
var model = CreateModel(id, "Мёд цветочный", "л", 250);
|
||||
_ingredientStorageContract.UpdElement(model);
|
||||
AssertElement(GetFromDb(id), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNotExists_Throws_Test()
|
||||
{
|
||||
var model = CreateModel(Guid.NewGuid().ToString(), "Имбирь", "г", 10);
|
||||
Assert.That(() => _ingredientStorageContract.UpdElement(model), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
|
||||
private static IngredientDataModel CreateModel(string id, string nameIngredients, string sizeInit, double initPrice)
|
||||
=> new(id, nameIngredients, sizeInit, initPrice);
|
||||
|
||||
private Ingredient InsertToDbAndReturn(string id, string nameIngredients, string sizeInit, double initPrice)
|
||||
{
|
||||
var ingr = new Ingredient
|
||||
{
|
||||
Id = id,
|
||||
NameIngredients = nameIngredients,
|
||||
SizeInit = sizeInit,
|
||||
InitPrice = initPrice
|
||||
};
|
||||
SladkieBulkiDbContext.Ingredients.Add(ingr);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return ingr;
|
||||
}
|
||||
|
||||
private Ingredient? GetFromDb(string id)
|
||||
=> SladkieBulkiDbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(IngredientDataModel? actual, Ingredient expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.NameIngredients, Is.EqualTo(expected.NameIngredients));
|
||||
Assert.That(actual.SizeInit, Is.EqualTo(expected.SizeInit));
|
||||
Assert.That(actual.InitPrice, Is.EqualTo(expected.InitPrice).Within(0.0001));
|
||||
});
|
||||
}
|
||||
|
||||
private static void AssertElement(Ingredient? actual, IngredientDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.NameIngredients, Is.EqualTo(expected.NameIngredients));
|
||||
Assert.That(actual.SizeInit, Is.EqualTo(expected.SizeInit));
|
||||
Assert.That(actual.InitPrice, Is.EqualTo(expected.InitPrice).Within(0.0001));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,329 @@
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class PostStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private PostStorageContract _postStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_postStorageContract = new PostStorageContract(SladkieBulkiDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemovePostsFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 2");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 3");
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenDifferentConfigTypes_Test()
|
||||
{
|
||||
var postSimple = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
||||
var postCashier = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new ManufacturerPostConfiguration() { SalePercent = 500 });
|
||||
var postSupervisor = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new PackerPostConfiguration() { PersonalCountPack = 20 });
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
AssertElement(list.First(x => x.Id == postSimple.PostId), postSimple);
|
||||
AssertElement(list.First(x => x.Id == postCashier.PostId), postCashier);
|
||||
AssertElement(list.First(x => x.Id == postSupervisor.PostId), postSupervisor);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetPostWithHistory_WhenHaveRecords_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
var list = _postStorageContract.GetPostWithHistory(postId);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetPostWithHistory_WhenNoRecords_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
var list = _postStorageContract.GetPostWithHistory(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
AssertElement(_postStorageContract.GetElementById(post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
Assert.That(() => _postStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.PostId), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenTrySearchById_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.Id), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
AssertElement(_postStorageContract.GetElementByName(post.PostName), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
Assert.That(() => _postStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.PostName), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
_postStorageContract.AddElement(post);
|
||||
AssertElement(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.Id), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSamePostId_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WithManufacturerPostConfiguration_Test()
|
||||
{
|
||||
var salePercent = 10;
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), config: new ManufacturerPostConfiguration() { SalePercent = salePercent });
|
||||
_postStorageContract.AddElement(post);
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.Id);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManufacturerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as ManufacturerPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WithPackerPostConfiguration_Test()
|
||||
{
|
||||
var trendPremium = 20;
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), config: new PackerPostConfiguration() { PersonalCountPack = trendPremium });
|
||||
_postStorageContract.AddElement(post);
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.Id);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(PackerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as PackerPostConfiguration)!.PersonalCountPack, Is.EqualTo(trendPremium));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
_postStorageContract.UpdElement(post);
|
||||
var posts = SladkieBulkiDbContext.GetPostsFromDatabaseByPostId(post.Id);
|
||||
Assert.That(posts, Is.Not.Null);
|
||||
Assert.That(posts, Has.Length.EqualTo(2));
|
||||
AssertElement(posts[0], CreateModel(post.Id));
|
||||
AssertElement(posts[^1], CreateModel(post.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "New Name");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(post.Id, postName: "name");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName);
|
||||
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: false);
|
||||
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WithManufacturerPostConfiguration_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
var salePercent = 10;
|
||||
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new ManufacturerPostConfiguration() { SalePercent = salePercent }));
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManufacturerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as ManufacturerPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WithPackerPostConfiguration_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
var trendPremium = 20;
|
||||
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new PackerPostConfiguration() { PersonalCountPack = trendPremium }));
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(PackerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as PackerPostConfiguration)!.PersonalCountPack, Is.EqualTo(trendPremium));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: true);
|
||||
_postStorageContract.DelElement(post.PostId);
|
||||
Assert.That(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.PostId), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
||||
Assert.That(() => _postStorageContract.DelElement(post.PostId), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
||||
_postStorageContract.ResElement(post.PostId);
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsActual);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.ResElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
|
||||
{
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: true);
|
||||
Assert.That(() => _postStorageContract.ResElement(post.PostId), Throws.Nothing);
|
||||
}
|
||||
|
||||
private static void AssertElement(PostDataModel? actual, Post expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.ConfigurationModel.Rate, Is.EqualTo(expected.Configuration.Rate));
|
||||
});
|
||||
}
|
||||
|
||||
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.Manufacturer, PostConfiguration? config = null)
|
||||
=> new(postId, postName, postType, config ?? new PostConfiguration() { Rate = 100 });
|
||||
|
||||
private static void AssertElement(Post? actual, PostDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.Configuration.Rate, Is.EqualTo(expected.ConfigurationModel.Rate));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class ProductStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private ProductStorageContract _productStorageContract;
|
||||
private Ingredient _ingredient;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_productStorageContract = new ProductStorageContract(SladkieBulkiDbContext);
|
||||
_ingredient = InsertIngredientToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"ProductIngredients\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||
var list = _productStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == product.Id), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _productStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyActual_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||
var list = _productStorageContract.GetList(onlyActive: true);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(!list.Any(x => x.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_IncludeNoActual_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||
var list = _productStorageContract.GetList(onlyActive: false);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.Count(x => x.IsDeleted), Is.EqualTo(2));
|
||||
Assert.That(list.Count(x => !x.IsDeleted), Is.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryByProductId_WhenHaveRecords_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _productStorageContract.GetHistoryByProductId(product.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryByProductId_WhenNoRecords_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _productStorageContract.GetHistoryByProductId(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_productStorageContract.GetElementById(product.Id), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _productStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.GetElementById(product.Id), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_productStorageContract.GetElementByName(product.Name), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _productStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.GetElementById(product.Name), Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
_productStorageContract.AddElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, true);
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.Nothing);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), CreateModel(product.Id, "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, isDeleted: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name unique");
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var ingredients = new List<(string ingredientId, int count)> { (_ingredient.Id, 1) };
|
||||
|
||||
var product = CreateModel(
|
||||
Guid.NewGuid().ToString(),
|
||||
"test",
|
||||
"description",
|
||||
ProductType.Workpiece,
|
||||
ingredients.Select(i => i.ingredientId).ToList(),
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
InsertProductToDatabaseAndReturn(
|
||||
Guid.NewGuid().ToString(),
|
||||
productName: product.Name,
|
||||
description: "description",
|
||||
productType: ProductType.Workpiece,
|
||||
unitPrice: 1,
|
||||
isDeleted: false,
|
||||
ingredients: ingredients
|
||||
);
|
||||
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.Name, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.Nothing);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||
_productStorageContract.UpdElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||
_productStorageContract.UpdElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), CreateModel(product.Id, isDeleted: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _productStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.Name);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.Name, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product), Throws.Nothing);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: false);
|
||||
_productStorageContract.DelElement(product.Id);
|
||||
var element = GetProductFromDatabaseById(product.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsDeleted);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _productStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.DelElement(product.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
|
||||
private Product InsertProductToDatabaseAndReturn(
|
||||
string id,
|
||||
string productName = "test",
|
||||
string description = "desc 1",
|
||||
ProductType productType = ProductType.Production,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false,
|
||||
List<(string ingredientId, int count)>? ingredients = null)
|
||||
{
|
||||
var product = new Product
|
||||
{
|
||||
Id = id,
|
||||
Name = productName,
|
||||
Description = description,
|
||||
ProductType = productType,
|
||||
UnitPrice = unitPrice,
|
||||
IsDeleted = isDeleted,
|
||||
ProductIngredients = new List<ProductIngredient>()
|
||||
};
|
||||
|
||||
if (ingredients is not null)
|
||||
{
|
||||
foreach (var (ingredientId, count) in ingredients)
|
||||
{
|
||||
product.ProductIngredients.Add(new ProductIngredient
|
||||
{
|
||||
ProductId = product.Id,
|
||||
IngredientId = ingredientId,
|
||||
Count = count
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SladkieBulkiDbContext.Products.Add(product);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
private ProductHistory InsertProductHistoryToDatabaseAndReturn(string productId, double price, DateTime changeDate)
|
||||
{
|
||||
var productHistory = new ProductHistory() { Id = Guid.NewGuid().ToString(), ProductId = productId, OldPrice = price, ChangeDate = changeDate };
|
||||
SladkieBulkiDbContext.ProductHistories.Add(productHistory);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return productHistory;
|
||||
}
|
||||
|
||||
private Ingredient InsertIngredientToDatabaseAndReturn( string nameIngredients = "sugar", string sizeInit = "kg", double initPrice = 0.1)
|
||||
{
|
||||
var ingredient = new Ingredient() { Id = Guid.NewGuid().ToString(), NameIngredients = nameIngredients, SizeInit = sizeInit, InitPrice = initPrice };
|
||||
SladkieBulkiDbContext.Ingredients.Add(ingredient);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return ingredient;
|
||||
}
|
||||
|
||||
private static void AssertElement(ProductDataModel? actual, Product expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.Description, Is.EqualTo(expected.Description));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static ProductDataModel CreateModel(
|
||||
string id,
|
||||
string name = "test",
|
||||
string description = "desk",
|
||||
ProductType productType = ProductType.Workpiece,
|
||||
List<string>? ingredientIds = null,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false)
|
||||
{
|
||||
// Если ingredientIds равно null, заменяем на пустой список
|
||||
ingredientIds = ingredientIds ?? new List<string>();
|
||||
|
||||
// Создаём список ингредиентов с использованием Select
|
||||
var ingredients = ingredientIds.Select(x => new ProductIngredientDataModel(id, x, 1)).ToList();
|
||||
|
||||
return new ProductDataModel(id, name, description, productType, ingredients, unitPrice, isDeleted);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Product? GetProductFromDatabaseById(string id) => SladkieBulkiDbContext.Products.Include(x => x.ProductIngredients).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Product? actual, ProductDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.Description, Is.EqualTo(expected.Description));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class ProductionStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private ProductionStorageContract _productionStorageContract;
|
||||
private Worker _worker;
|
||||
private Product _product;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_productionStorageContract = new ProductionStorageContract(SladkieBulkiDbContext);
|
||||
_worker = InsertWorkerToDatabaseAndReturn();
|
||||
_product = InsertProductToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Productions\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"ProductIngredients\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 5, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 2, 200);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 5, 500);
|
||||
var list = _productionStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == production.Id), production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _productionStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPeriod_Test()
|
||||
{
|
||||
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(-3), 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(1).AddMinutes(-3), 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(1).AddMinutes(3), 1, 100);
|
||||
|
||||
var list = _productionStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByWorkerId_Test()
|
||||
{
|
||||
var worker1 = InsertWorkerToDatabaseAndReturn("Main worker");
|
||||
var worker2 = InsertWorkerToDatabaseAndReturn("Other worker");
|
||||
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(worker2.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var list = _productionStorageContract.GetList(workerId: worker1.Id);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.WorkerId == worker1.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByProductId_Test()
|
||||
{
|
||||
var product1 = InsertProductToDatabaseAndReturn("Product 1");
|
||||
var product2 = InsertProductToDatabaseAndReturn("Product 2");
|
||||
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product1.Id, DateTime.UtcNow, 5, 500);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product1.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product2.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product2.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var list = _productionStorageContract.GetList(productId: product1.Id);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.ProductId == product1.Id));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var worker1 = InsertWorkerToDatabaseAndReturn("Main worker");
|
||||
var worker2 = InsertWorkerToDatabaseAndReturn("Other worker");
|
||||
var product1 = InsertProductToDatabaseAndReturn("Main product");
|
||||
var product2 = InsertProductToDatabaseAndReturn("Other product");
|
||||
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, product2.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100);
|
||||
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, product1.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(-3), 1, 100); // по дате
|
||||
InsertProductionToDatabaseAndReturn(worker2.Id, product2.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100); // по worker
|
||||
InsertProductionToDatabaseAndReturn(worker2.Id, product1.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100); // по worker и product
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, product2.Id, DateTime.UtcNow.AddDays(1).AddMinutes(3), 1, 100); // по дате
|
||||
|
||||
var list = _productionStorageContract.GetList(
|
||||
startDate: DateTime.UtcNow.AddDays(-1),
|
||||
endDate: DateTime.UtcNow.AddDays(1),
|
||||
workerId: worker1.Id,
|
||||
productId: product2.Id
|
||||
);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var result = _productionStorageContract.GetElementById(production.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
AssertElement(result, production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var result = _productionStorageContract.GetElementById(Guid.NewGuid().ToString());
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||
{
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var result = _productionStorageContract.GetElementById(production.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
AssertElement(result, production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var productionDate = DateTime.UtcNow;
|
||||
var production = CreateModel(Guid.NewGuid().ToString(), productionDate, 1, 100, _worker.Id, _product.Id);
|
||||
_productionStorageContract.AddElement(production);
|
||||
AssertElement(GetProductionFromDatabaseById(production.Id), production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
_productionStorageContract.DelElement(production.Id);
|
||||
|
||||
var element = GetProductionFromDatabaseById(production.Id);
|
||||
Assert.That(element, Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _productionStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
|
||||
private Worker InsertWorkerToDatabaseAndReturn(string fio = "test")
|
||||
{
|
||||
var worker = new Worker() { Id = Guid.NewGuid().ToString(), FIO = fio, PostId = Guid.NewGuid().ToString(), Email = "1@mail.ru" };
|
||||
SladkieBulkiDbContext.Workers.Add(worker);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
|
||||
|
||||
private Product InsertProductToDatabaseAndReturn(
|
||||
|
||||
string productName = "test",
|
||||
string description = "desc 1",
|
||||
ProductType productType = ProductType.Production,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false,
|
||||
List<(string ingredientId, int count)>? ingredients = null)
|
||||
{
|
||||
var product = new Product
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Name = productName,
|
||||
Description = description,
|
||||
ProductType = productType,
|
||||
UnitPrice = unitPrice,
|
||||
IsDeleted = isDeleted,
|
||||
ProductIngredients = new List<ProductIngredient>()
|
||||
};
|
||||
|
||||
if (ingredients is not null)
|
||||
{
|
||||
foreach (var (ingredientId, count) in ingredients)
|
||||
{
|
||||
product.ProductIngredients.Add(new ProductIngredient
|
||||
{
|
||||
ProductId = product.Id,
|
||||
IngredientId = ingredientId,
|
||||
Count = count
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SladkieBulkiDbContext.Products.Add(product);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Production InsertProductionToDatabaseAndReturn(string workerId, string productId, DateTime productionDate, int count, double sum)
|
||||
{
|
||||
var production = new Production
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
WorkerId = workerId,
|
||||
ProductId = productId,
|
||||
ProductionDate = productionDate,
|
||||
Count = count,
|
||||
Sum = sum
|
||||
};
|
||||
SladkieBulkiDbContext.Productions.Add(production);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return production;
|
||||
}
|
||||
|
||||
|
||||
private void AssertElement(ProductionDataModel result, Production expected)
|
||||
{
|
||||
Assert.That(result.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(result.ProductionDate, Is.EqualTo(expected.ProductionDate));
|
||||
Assert.That(result.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(result.Sum, Is.EqualTo(expected.Sum));
|
||||
Assert.That(result.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(result.ProductId, Is.EqualTo(expected.ProductId));
|
||||
}
|
||||
|
||||
private static ProductionDataModel CreateModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId)
|
||||
{
|
||||
return new ProductionDataModel(id, productionDate, count, sum, workerId, productId);
|
||||
}
|
||||
|
||||
private Production? GetProductionFromDatabaseById(string id) =>
|
||||
SladkieBulkiDbContext.Productions
|
||||
.Include(x => x.Product)
|
||||
.Include(x => x.Worker)
|
||||
.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Production? actual, ProductionDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.ProductId, Is.EqualTo(expected.ProductId));
|
||||
Assert.That(actual.ProductionDate, Is.EqualTo(expected.ProductionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.Sum, Is.EqualTo(expected.Sum));
|
||||
});
|
||||
|
||||
if (actual.Product != null)
|
||||
{
|
||||
Assert.That(actual.Product.Id, Is.EqualTo(expected.ProductId));
|
||||
}
|
||||
|
||||
if (actual.Worker != null)
|
||||
{
|
||||
Assert.That(actual.Worker.Id, Is.EqualTo(expected.WorkerId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class SalaryStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SalaryStorageContract _salaryStorageContract;
|
||||
private Worker _worker;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_salaryStorageContract = new SalaryStorageContract(SladkieBulkiDbContext);
|
||||
_worker = InsertWorkerToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
// Arrange
|
||||
var targetDate = DateTime.UtcNow;
|
||||
var salary = InsertSalaryToDatabaseAndReturn(_worker.Id, workerSalary: 100, salaryDate: targetDate);
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: targetDate.AddDays(1));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: targetDate.AddDays(2));
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
|
||||
var actual = list.FirstOrDefault(x =>
|
||||
x.WorkerId == _worker.Id &&
|
||||
Math.Abs((x.SalaryDate - targetDate).TotalSeconds) < 1 &&
|
||||
Math.Abs(x.WorkerSalary - 100) < 0.01
|
||||
);
|
||||
Assert.That(actual, Is.Not.Null, "Не найдена нужная запись по WorkerId и SalaryDate");
|
||||
AssertElement(actual, salary);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyInDatePeriod_Test()
|
||||
{
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByWorker_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn("name 2");
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||
InsertSalaryToDatabaseAndReturn(worker.Id);
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _worker.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByWorkerOnlyInDatePeriod_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn("name 2");
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _worker.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
|
||||
var salaryDate = DateTime.UtcNow;
|
||||
var salary = CreateModel(_worker.Id, 1.0, salaryDate);
|
||||
_salaryStorageContract.AddElement(salary);
|
||||
|
||||
var savedSalary = GetSalaryFromDatabaseByWorkerId(_worker.Id);
|
||||
Assert.NotNull(savedSalary);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(savedSalary.WorkerId, Is.EqualTo(salary.WorkerId));
|
||||
Assert.That(savedSalary.WorkerSalary, Is.EqualTo(salary.WorkerSalary));
|
||||
Assert.That(savedSalary.SalaryDate, Is.EqualTo(salaryDate).Within(TimeSpan.FromSeconds(1)));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private Worker InsertWorkerToDatabaseAndReturn(string workerFIO = "fio")
|
||||
{
|
||||
var worker = new Worker() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = workerFIO, IsDeleted = false , Email = "1@mail.ru"};
|
||||
SladkieBulkiDbContext.Workers.Add(worker);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
|
||||
private Salary InsertSalaryToDatabaseAndReturn(string workerId, double workerSalary = 1, DateTime? salaryDate = null)
|
||||
{
|
||||
var salary = new Salary() { WorkerId = workerId, WorkerSalary = workerSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
|
||||
SladkieBulkiDbContext.Salaries.Add(salary);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return salary;
|
||||
}
|
||||
|
||||
private static void AssertElement(SalaryDataModel? actual, Salary expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.WorkerSalary, Is.EqualTo(expected.WorkerSalary));
|
||||
});
|
||||
}
|
||||
|
||||
private static SalaryDataModel CreateModel(string workerId, double workerSalary, DateTime salaryDate)
|
||||
=> new(workerId, salaryDate, workerSalary);
|
||||
|
||||
|
||||
private Salary? GetSalaryFromDatabaseByWorkerId(string id) => SladkieBulkiDbContext.Salaries.FirstOrDefault(x => x.WorkerId == id);
|
||||
|
||||
private static void AssertElement(Salary? actual, SalaryDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.WorkerSalary, Is.EqualTo(expected.WorkerSalary));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
internal class WorkerStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private WorkerStorageContract _workerStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_workerStorageContract = new WorkerStorageContract(SladkieBulkiDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var worker1 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1");
|
||||
var worker2 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2");
|
||||
var worker3 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
||||
var list = _workerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == worker1.Id), worker1);
|
||||
AssertElement(list.First(x => x.Id == worker2.Id), worker2);
|
||||
AssertElement(list.First(x => x.Id == worker3.Id), worker3);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _workerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPostId_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
||||
var list = _workerStorageContract.GetList(postId: postId);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.PostId == postId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByBirthDate_Test()
|
||||
{
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", birthDate: DateTime.UtcNow.AddYears(-25));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", birthDate: DateTime.UtcNow.AddYears(-21));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", birthDate: DateTime.UtcNow.AddYears(-20));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-19));
|
||||
var list = _workerStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByEmploymentDate_Test()
|
||||
{
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", employmentDate: DateTime.UtcNow.AddDays(2));
|
||||
var list = _workerStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
var list = _workerStorageContract.GetList(postId: postId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1), fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_workerStorageContract.GetElementById(worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||
{
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_workerStorageContract.GetElementByFIO(worker.FIO), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
_workerStorageContract.AddElement(worker);
|
||||
AssertElement(SladkieBulkiDbContext.GetWorkerFromDatabaseById(worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||
Assert.That(() => _workerStorageContract.AddElement(worker), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString(), "New Fio");
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||
_workerStorageContract.UpdElement(worker);
|
||||
AssertElement(SladkieBulkiDbContext.GetWorkerFromDatabaseById(worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||
Assert.That(() => _workerStorageContract.UpdElement(worker), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_workerStorageContract.DelElement(worker.Id);
|
||||
var element = SladkieBulkiDbContext.GetWorkerFromDatabaseById(worker.Id);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.IsDeleted);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||
Assert.That(() => _workerStorageContract.DelElement(worker.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetWorkerTrend_WhenNoNewAndDeletedWorkers_Test()
|
||||
{
|
||||
var startDate = DateTime.UtcNow.AddDays(-5);
|
||||
var endDate = DateTime.UtcNow.AddDays(-3);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-9));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-1));
|
||||
var count = _workerStorageContract.GetWorkerTrend(startDate, endDate);
|
||||
Assert.That(count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetWorkerTrend_WhenHaveNewAndNoDeletedWorkers_Test()
|
||||
{
|
||||
var startDate = DateTime.UtcNow.AddDays(-5);
|
||||
var endDate = DateTime.UtcNow.AddDays(-3);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-4));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
var count = _workerStorageContract.GetWorkerTrend(startDate, endDate);
|
||||
Assert.That(count, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetWorkerTrend_WhenNoNewAndHaveDeletedWorkers_Test()
|
||||
{
|
||||
var startDate = DateTime.UtcNow.AddDays(-5);
|
||||
var endDate = DateTime.UtcNow.AddDays(-3);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-9));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-4));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-1));
|
||||
var count = _workerStorageContract.GetWorkerTrend(startDate, endDate);
|
||||
Assert.That(count, Is.EqualTo(-1));
|
||||
}
|
||||
|
||||
private static void AssertElement(WorkerDataModel? actual, Worker expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
private static WorkerDataModel CreateModel(string id, string fio = "fio", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false, string email = "test@test.com") =>
|
||||
new(id, fio, postId ?? Guid.NewGuid().ToString(), birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted, email);
|
||||
|
||||
private static void AssertElement(Worker? actual, WorkerDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
Assert.That(actual.DateOfDelete.HasValue, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SladkieBulkiDatabase;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
internal class BaseWebApiControllerTest
|
||||
{
|
||||
private WebApplicationFactory<Program> _webApplication;
|
||||
|
||||
protected HttpClient HttpClient { get; private set; }
|
||||
|
||||
protected SladkieBulkiDbContext SladkieBulkiDbContext { get; private set; }
|
||||
|
||||
protected static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_webApplication = new CustomWebApplicationFactory<Program>();
|
||||
HttpClient = _webApplication
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
using var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build())
|
||||
.CreateLogger());
|
||||
services.AddSingleton(loggerFactory);
|
||||
});
|
||||
})
|
||||
.CreateClient();
|
||||
|
||||
var request = HttpClient.GetAsync("/login/user").GetAwaiter().GetResult();
|
||||
var data = request.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {data}");
|
||||
|
||||
SladkieBulkiDbContext = _webApplication.Services.GetRequiredService<SladkieBulkiDbContext>();
|
||||
SladkieBulkiDbContext.Database.EnsureDeleted();
|
||||
SladkieBulkiDbContext.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
SladkieBulkiDbContext?.Database.EnsureDeleted();
|
||||
SladkieBulkiDbContext?.Dispose();
|
||||
HttpClient?.Dispose();
|
||||
_webApplication?.Dispose();
|
||||
}
|
||||
|
||||
protected static async Task<T?> GetModelFromResponseAsync<T>(HttpResponseMessage response) =>
|
||||
JsonSerializer.Deserialize<T>(await response.Content.ReadAsStringAsync(), JsonSerializerOptions);
|
||||
|
||||
|
||||
protected static StringContent MakeContent(object model) =>
|
||||
new(JsonSerializer.Serialize(model), Encoding.UTF8, "application/json");
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class IngredientControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemoveIngredientsFromDatabase();
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/ingredient/getrecords");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<IngredientViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var ingredient = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn(nameIngredients: "Дрожжи");
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/ingredient/getrecord/{ingredient.Id}");
|
||||
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<IngredientViewModel>(response), ingredient);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/ingredient/{Guid.NewGuid()}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var ingredient = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn(nameIngredients: "Соль");
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/ingredient/getrecord/{ingredient.NameIngredients}"); // <--- /getrecord/{name}
|
||||
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<IngredientViewModel>(response), ingredient);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var model = CreateModel(name: "Вода");
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync("/api/ingredient/register", MakeContent(model));
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(SladkieBulkiDbContext.GetIngredientFromDatabaseById(model.Id!), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test()
|
||||
{
|
||||
// Arrange
|
||||
var model = CreateModel(name: "Молоко");
|
||||
SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn(id: model.Id, nameIngredients: "Молоко");
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync("/api/ingredient/register", MakeContent(model));
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
// Arrange
|
||||
var modelWithEmptyName = CreateModel(name: "");
|
||||
var modelWithWrongId = CreateModel(id: "not-a-guid");
|
||||
// Act
|
||||
var responseWithEmptyName = await HttpClient.PostAsync("/api/ingredient/register", MakeContent(modelWithEmptyName));
|
||||
var responseWithWrongId = await HttpClient.PostAsync("/api/ingredient/register", MakeContent(modelWithWrongId));
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithEmptyName.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
||||
Assert.That(responseWithWrongId.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var ingr = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn(nameIngredients: "Кефир");
|
||||
var model = CreateModel(id: ingr.Id, name: "Кефир обновленный");
|
||||
// Act
|
||||
var response = await HttpClient.PutAsync("/api/ingredient/changeinfo", MakeContent(model));
|
||||
// Assert
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
|
||||
AssertElement(SladkieBulkiDbContext.GetIngredientFromDatabaseById(model.Id!), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
// Arrange
|
||||
var model = CreateModel(name: "Гречка");
|
||||
// Act
|
||||
var response = await HttpClient.PutAsync($"/api/ingredient/changeinfo", MakeContent(model)); // <-- экшен, не {id}
|
||||
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
private static IngredientBindingModel CreateModel(string? id = null, string name = "test", string size = "кг", double price = 5)
|
||||
=> new()
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
NameIngredients = name,
|
||||
SizeInit = size,
|
||||
InitPrice = price
|
||||
};
|
||||
|
||||
private static void AssertElement(IngredientViewModel? actual, Ingredient expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.NameIngredients, Is.EqualTo(expected.NameIngredients));
|
||||
Assert.That(actual.SizeInit, Is.EqualTo(expected.SizeInit));
|
||||
Assert.That(actual.InitPrice, Is.EqualTo(expected.InitPrice));
|
||||
});
|
||||
}
|
||||
private static void AssertElement(Ingredient? actual, IngredientBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.NameIngredients, Is.EqualTo(expected.NameIngredients));
|
||||
Assert.That(actual.SizeInit, Is.EqualTo(expected.SizeInit));
|
||||
Assert.That(actual.InitPrice, Is.EqualTo(expected.InitPrice));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,577 @@
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System.Net;
|
||||
using SladkieBulkiDatabase;
|
||||
using SladkieBulkiTests.WebApiControllersTests;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class PostControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemovePostsFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecords_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 2");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 3");
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/posts");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecords_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/posts");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecords_WhenDifferentConfigTypes_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postSimple = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
||||
var postCashier = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new ManufacturerPostConfiguration() { SalePercent = 500 });
|
||||
var postSupervisor = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new PackerPostConfiguration() { PersonalCountPack = 20 });
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/posts");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
AssertElement(data.First(x => x.Id == postSimple.PostId), postSimple);
|
||||
AssertElement(data.First(x => x.Id == postCashier.PostId), postCashier);
|
||||
AssertElement(data.First(x => x.Id == postSupervisor.PostId), postSupervisor);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetHistory_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posthistory/{postId}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetHistory_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posthistory/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetHistory_WhenWrongData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posthistory/id");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posts/{post.PostId}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<PostViewModel>(response), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posts/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenRecordWasDeleted_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posts/{post.PostId}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posts/{post.PostName}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<PostViewModel>(response), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posts/New%20Name");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByName_WhenRecordWasDeleted_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/posts/{post.PostName}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
var postModel = CreateModel();
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postModel.Id!), postModel);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModel = CreateModel();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postModel.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModel = CreateModel(postName: "unique name");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: postModel.PostName!);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manufacturer.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
|
||||
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manufacturer.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
|
||||
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
|
||||
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manufacturer.ToString(), ConfigurationJson = null };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
|
||||
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
|
||||
var responseWithPostTypeIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithPostTypeIncorrect));
|
||||
var responseWithSalaryIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithSalaryIncorrect));
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
||||
Assert.That(responseWithPostTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
||||
Assert.That(responseWithSalaryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Salary is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WithManufacturerPostConfiguration_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var salePercent = 10;
|
||||
var postModel = CreateModel(configuration: JsonSerializer.Serialize(new ManufacturerPostConfiguration() { SalePercent = salePercent, Rate = 10 }));
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManufacturerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as ManufacturerPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WithPackerPostConfiguration_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var trendPremium = 20;
|
||||
var postModel = CreateModel(configuration: JsonSerializer.Serialize(new PackerPostConfiguration() { PersonalCountPack = trendPremium, Rate = 10 }));
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(PackerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as PackerPostConfiguration)!.PersonalCountPack, Is.EqualTo(trendPremium));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModel = CreateModel();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postModel.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
AssertElement(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postModel.Id!), postModel);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModel = CreateModel();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenRecordWasDeleted_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModel = CreateModel();
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postModel.Id, isActual: false);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModel = CreateModel(postName: "unique name");
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postModel.Id);
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: postModel.PostName!);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manufacturer.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
|
||||
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manufacturer.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
|
||||
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
|
||||
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manufacturer.ToString(), ConfigurationJson = null };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
|
||||
var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
|
||||
var responseWithPostTypeIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithPostTypeIncorrect));
|
||||
var responseWithSalaryIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithSalaryIncorrect));
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
||||
Assert.That(responseWithPostTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
||||
Assert.That(responseWithSalaryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Salary is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WithManufacturerPostConfiguration_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var salePercent = 10;
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
var postModel = CreateModel(post.PostId, configuration: JsonSerializer.Serialize(new ManufacturerPostConfiguration() { SalePercent = salePercent, Rate = 10 }));
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManufacturerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as ManufacturerPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WithPackerPostConfiguration_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var trendPremium = 20;
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
var postModel = CreateModel(post.PostId, configuration: JsonSerializer.Serialize(new PackerPostConfiguration() { PersonalCountPack = trendPremium, Rate = 10 }));
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(PackerPostConfiguration).Name));
|
||||
Assert.That((element.Configuration as PackerPostConfiguration)!.PersonalCountPack, Is.EqualTo(trendPremium));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn().PostId;
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/posts/{postId}");
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postId), Is.Null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/posts/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false).PostId;
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/posts/{postId}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/posts/id");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Patch_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false).PostId;
|
||||
//Act
|
||||
var response = await HttpClient.PatchAsync($"/api/posts/{postId}", null);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postId), Is.Not.Null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Patch_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PatchAsync($"/api/posts/{Guid.NewGuid()}", null);
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Patch_WhenRecordNotWasDeleted_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn().PostId;
|
||||
//Act
|
||||
var response = await HttpClient.PatchAsync($"/api/posts/{postId}", null);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(postId), Is.Not.Null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Patch_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PatchAsync($"/api/posts/id", null);
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
private static void AssertElement(PostViewModel? actual, Post expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType.ToString()));
|
||||
Assert.That(JsonNode.Parse(actual.Configuration)!["Type"]!.GetValue<string>(), Is.EqualTo(expected.Configuration.Type));
|
||||
});
|
||||
}
|
||||
|
||||
private static PostBindingModel CreateModel(string? postId = null, string postName = "name", PostType postType = PostType.Manufacturer, string? configuration = null)
|
||||
=> new()
|
||||
{
|
||||
Id = postId ?? Guid.NewGuid().ToString(),
|
||||
PostName = postName,
|
||||
PostType = postType.ToString(),
|
||||
ConfigurationJson = configuration ?? JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 })
|
||||
};
|
||||
|
||||
private static void AssertElement(Post? actual, PostBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType.ToString(), Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.Configuration.Type, Is.EqualTo(JsonNode.Parse(expected.ConfigurationJson!)!["Type"]!.GetValue<string>()));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ProductControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemoveProductsFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveIngredientsFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var product1 = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Product 1");
|
||||
var product2 = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Product 2");
|
||||
var product3 = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Product 3");
|
||||
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/product/getrecords");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<ProductViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == product1.Id), product1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/product/getrecords");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<ProductViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Product by Id");
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/product/getrecord/{product.Id}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<ProductViewModel>(response), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/product/getrecord/{Guid.NewGuid()}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Unique Product Name");
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/product/getrecord/{product.Name}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<ProductViewModel>(response), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/product/getrecord/NonexistentProductName");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), id: productId, name: "Deletable Product");
|
||||
// Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/product/delete/{productId}");
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(SladkieBulkiDbContext.GetProductFromDatabaseById(productId)!.IsDeleted);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/product/delete/{Guid.NewGuid()}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenRecordWasDeleted_ShouldBadRequest_Test()
|
||||
{
|
||||
// Arrange
|
||||
var productId = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), isDeleted: true).Id;
|
||||
// Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/product/delete/{productId}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/product/delete/id");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
private static ProductBindingModel CreateModel(
|
||||
string? id = null,
|
||||
string name = "New Product",
|
||||
double unitPrice = 10,
|
||||
string productType = "None")
|
||||
{
|
||||
return new ProductBindingModel
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
Name = name,
|
||||
Description = "Описание продукта",
|
||||
UnitPrice = unitPrice,
|
||||
ProductType = productType,
|
||||
Ingredients = new List<IngredientBindingModel>
|
||||
{
|
||||
new IngredientBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
NameIngredients = "Сахар",
|
||||
SizeInit = "кг",
|
||||
InitPrice = 10
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private static void AssertElement(ProductViewModel? actual, Product expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.Description, Is.EqualTo(expected.Description));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType.ToString()));
|
||||
Assert.That(actual.Ingredients.Count, Is.EqualTo(expected.ProductIngredients.Count));
|
||||
var expectedNames = expected.ProductIngredients.Select(pi => pi.Ingredient!.NameIngredients).OrderBy(x => x).ToList();
|
||||
var actualNames = actual.Ingredients.Select(i => i.NameIngredients).OrderBy(x => x).ToList();
|
||||
Assert.That(actualNames, Is.EquivalentTo(expectedNames));
|
||||
});
|
||||
}
|
||||
|
||||
private static void AssertElement(Product? actual, ProductBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.Description, Is.EqualTo(expected.Description));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.ProductType.ToString(), Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.ProductIngredients.Count, Is.EqualTo(expected.Ingredients.Count));
|
||||
var expectedNames = expected.Ingredients.Select(i => i.NameIngredients).OrderBy(x => x).ToList();
|
||||
var actualNames = actual.ProductIngredients.Select(pi => pi.Ingredient!.NameIngredients).OrderBy(x => x).ToList();
|
||||
Assert.That(actualNames, Is.EquivalentTo(expectedNames));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class ProductionControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemoveProductionsFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecords_WhenNoRecords_ShouldReturnEmptyList_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/production/getrecords");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<ProductionViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecords_WhenHaveRecords_ShouldReturnAll_Test()
|
||||
{
|
||||
// Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
var product1 = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Product_" + Guid.NewGuid());
|
||||
var product2 = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "Product_" + Guid.NewGuid());
|
||||
var prod1 = SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
count: 10, sum: 500, workerId: worker.Id, productId: product1.Id);
|
||||
var prod2 = SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
count: 5, sum: 200, workerId: worker.Id, productId: product2.Id);
|
||||
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync("/api/production/getrecords");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<ProductionViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
AssertElement(data.First(x => x.Id == prod1.Id), prod1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecord_ById_ShouldReturnProduction_Test()
|
||||
{
|
||||
// Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>());
|
||||
var prod = SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
count: 3, sum: 99.5, workerId: worker.Id, productId: product.Id);
|
||||
var response = await HttpClient.GetAsync($"/api/production/getrecord/{prod.Id}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<ProductionViewModel>(response), prod);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetRecord_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
// Act
|
||||
var response = await HttpClient.GetAsync($"/api/production/getrecord/{Guid.NewGuid()}");
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Register_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
// Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "TestProduct_" + Guid.NewGuid());
|
||||
|
||||
var model = CreateModel(count: 7, sum: 300, workerId: worker.Id, productId: product.Id);
|
||||
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync("/api/production/register", MakeContent(model));
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(SladkieBulkiDbContext.GetProductionFromDatabaseById(model.Id!), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Register_WhenWorkerIdIsNotGuid_ShouldBadRequest_Test()
|
||||
{
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "TestProduct_" + Guid.NewGuid());
|
||||
|
||||
var model = CreateModel(workerId: "not-a-guid", productId: product.Id);
|
||||
|
||||
var response = await HttpClient.PostAsync("/api/production/register", MakeContent(model));
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Register_WhenWorkerIdNotExists_ShouldInternalServerError_Test()
|
||||
{
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "TestProduct_" + Guid.NewGuid());
|
||||
|
||||
var model = CreateModel(workerId: Guid.NewGuid().ToString(), productId: product.Id);
|
||||
|
||||
var response = await HttpClient.PostAsync("/api/production/register", MakeContent(model));
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
||||
// Или BadRequest, если Adapter ловит такую ошибку отдельно
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Register_WhenProductIdIsNotGuid_ShouldBadRequest_Test()
|
||||
{
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
var model = CreateModel(workerId: worker.Id, productId: "not-a-guid");
|
||||
|
||||
var response = await HttpClient.PostAsync("/api/production/register", MakeContent(model));
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Register_WhenCountIsZeroOrNegative_ShouldBadRequest_Test()
|
||||
{
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(new List<Ingredient>(), name: "TestProduct_" + Guid.NewGuid());
|
||||
|
||||
var model = CreateModel(count: 0, sum: 100, workerId: worker.Id, productId: product.Id);
|
||||
|
||||
var response = await HttpClient.PostAsync("/api/production/register", MakeContent(model));
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Register_WhenInvalidData_ShouldBadRequest_Test()
|
||||
{
|
||||
// Arrange
|
||||
var model = CreateModel(count: 0, sum: 10); // count <= 0 некорректно
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync("/api/production/register", MakeContent(model));
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static ProductionBindingModel CreateModel(
|
||||
string? id = null, DateTime? date = null, int count = 1, double sum = 50, string? workerId = null, string? productId = null)
|
||||
=> new()
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
ProductionDate = date ?? DateTime.UtcNow,
|
||||
Count = count,
|
||||
Sum = sum,
|
||||
WorkerId = workerId ?? Guid.NewGuid().ToString(),
|
||||
ProductId = productId ?? Guid.NewGuid().ToString()
|
||||
};
|
||||
|
||||
|
||||
private static void AssertElement(ProductionViewModel? actual, Production expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.ProductionDate, Is.EqualTo(expected.ProductionDate).Within(TimeSpan.FromSeconds(1))); // секунды могут отличаться
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.Sum, Is.EqualTo(expected.Sum));
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.ProductId, Is.EqualTo(expected.ProductId));
|
||||
});
|
||||
}
|
||||
private static void AssertElement(Production? actual, ProductionBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.ProductionDate, Is.EqualTo(expected.ProductionDate).Within(TimeSpan.FromSeconds(1)));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.Sum, Is.EqualTo(expected.Sum));
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.ProductId, Is.EqualTo(expected.ProductId));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SalaryControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemovePostsFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveProductionsFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveProductsFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveIngredientsFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveSalariesFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveWorkersFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name");
|
||||
var salary = SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, workerSalary: 100);
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id);
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SalaryViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.WorkerSalary == salary.WorkerSalary), salary);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SalaryViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_OnlyInDatePeriod_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name");
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SalaryViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenDateIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getworkerrecords?fromDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByWorker_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker1 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 1");
|
||||
var worker2 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 2");
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker1.Id);
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker1.Id);
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker2.Id);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getworkerrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&id={worker1.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SalaryViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
Assert.That(data.All(x => x.WorkerId == worker1.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByWorker_OnlyInDatePeriod_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker1 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 1");
|
||||
var worker2 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 2");
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker1.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker1.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker2.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker1.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker2.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
SladkieBulkiDbContext.InsertSalaryToDatabaseAndReturn(worker1.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getworkerrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&id={worker1.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<SalaryViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
Assert.That(data.All(x => x.WorkerId == worker1.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByWorker_WhenDateIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 1");
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getworkerrecords?fromDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&id={worker.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByWorker_WhenIdIsNotGuid_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/salaries/getworkerrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&id=id");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Calculate_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var ingredient = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn(
|
||||
nameIngredients: "Сахар",
|
||||
sizeInit: "кг",
|
||||
initPrice: 100
|
||||
);
|
||||
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(
|
||||
config: new ManufacturerPostConfiguration()
|
||||
{
|
||||
Rate = 1000,
|
||||
SalePercent = 0.1, // 10% от среднего за день
|
||||
BonusForExtraSales = 0 // по умолчанию
|
||||
}
|
||||
);
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name", postId: post.PostId);
|
||||
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(
|
||||
new List<Ingredient> { ingredient },
|
||||
name: "Булка",
|
||||
description: "Вкусная булка",
|
||||
productType: ProductType.Packaging,
|
||||
unitPrice: 100
|
||||
);
|
||||
|
||||
var production = SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
productId: product.Id,
|
||||
workerId: worker.Id,
|
||||
productionDate: DateTime.UtcNow.Date, // Сегодня
|
||||
sum: 2000
|
||||
);
|
||||
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync($"/api/salaries/calculate?date={DateTime.UtcNow:O}", MakeContent(new { }));
|
||||
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent).Or.EqualTo(HttpStatusCode.OK));
|
||||
|
||||
var salaries = SladkieBulkiDbContext.GetSalariesFromDatabaseByWorkerId(worker.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(salaries, Is.Not.Null);
|
||||
Assert.That(salaries.Length, Is.EqualTo(1));
|
||||
Assert.That(salaries.First().WorkerSalary, Is.EqualTo(1000 + 2000 * 0.1).Within(0.01));
|
||||
Assert.That(salaries.First().SalaryDate.Month, Is.EqualTo(DateTime.UtcNow.Month));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Calculate_WithSeveralWorkers_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(
|
||||
config: new ManufacturerPostConfiguration() { Rate = 1000, SalePercent = 0.1 }); // 10%
|
||||
var worker1 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 1", postId: post.PostId);
|
||||
var worker2 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 2", postId: post.PostId);
|
||||
var worker3 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 3", postId: post.PostId);
|
||||
|
||||
var ingredient = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn();
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(
|
||||
new List<Ingredient> { ingredient }, name: "Product", unitPrice: 100);
|
||||
|
||||
// ВАЖНО! Разные дни для worker1, чтобы процент считался дважды
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
workerId: worker1.Id, productId: product.Id, sum: 2000, productionDate: DateTime.UtcNow.Date);
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
workerId: worker1.Id, productId: product.Id, sum: 2000, productionDate: DateTime.UtcNow.Date.AddDays(-1));
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
workerId: worker2.Id, productId: product.Id, sum: 2000, productionDate: DateTime.UtcNow.Date);
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
workerId: worker3.Id, productId: product.Id, sum: 2000, productionDate: DateTime.UtcNow.Date);
|
||||
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync($"/api/salaries/calculate?date={DateTime.UtcNow:O}", null);
|
||||
|
||||
// Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
var salaries = SladkieBulkiDbContext.Salaries.ToArray();
|
||||
Assert.That(salaries, Has.Length.EqualTo(3), "Суммарно должно быть по зарплате на каждого работника");
|
||||
|
||||
var s1 = salaries.FirstOrDefault(x => x.WorkerId == worker1.Id);
|
||||
var s2 = salaries.FirstOrDefault(x => x.WorkerId == worker2.Id);
|
||||
var s3 = salaries.FirstOrDefault(x => x.WorkerId == worker3.Id);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(s1, Is.Not.Null);
|
||||
Assert.That(s2, Is.Not.Null);
|
||||
Assert.That(s3, Is.Not.Null);
|
||||
Assert.That(s1.WorkerSalary, Is.EqualTo(1000 + 0.1 * 2000 + 0.1 * 2000).Within(0.01)); // 1400
|
||||
Assert.That(s2.WorkerSalary, Is.EqualTo(1000 + 0.1 * 2000).Within(0.01)); // 1200
|
||||
Assert.That(s3.WorkerSalary, Is.EqualTo(1000 + 0.1 * 2000).Within(0.01)); // 1200
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Calculate_WithoutSalesByWorker_ShouldSuccess_Test()
|
||||
{
|
||||
// Arrange:
|
||||
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(config: new ManufacturerPostConfiguration() { Rate = 1000, SalePercent = 0.1 });
|
||||
var worker1 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 1", postId: post.PostId);
|
||||
var worker2 = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name 2", postId: post.PostId);
|
||||
|
||||
var ingredient = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn();
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(
|
||||
new List<Ingredient> { ingredient }, name: "Test Product", unitPrice: 100);
|
||||
|
||||
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
workerId: worker1.Id, productId: product.Id, sum: 2000, productionDate: DateTime.UtcNow.AddMonths(-1));
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(
|
||||
workerId: worker2.Id, productId: product.Id, sum: 2000, productionDate: DateTime.UtcNow);
|
||||
|
||||
// Act: рассчитываем зарплаты за текущий месяц
|
||||
var response = await HttpClient.PostAsync($"/api/salaries/calculate?date={DateTime.UtcNow:O}", null);
|
||||
|
||||
// Assert:
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
var salary1 = SladkieBulkiDbContext.GetSalariesFromDatabaseByWorkerId(worker1.Id).FirstOrDefault();
|
||||
var salary2 = SladkieBulkiDbContext.GetSalariesFromDatabaseByWorkerId(worker2.Id).FirstOrDefault();
|
||||
|
||||
Assert.That(salary1, Is.Not.Null);
|
||||
Assert.That(salary2, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(salary1.WorkerSalary, Is.EqualTo(1000).Within(0.01), "worker1 зарплата только оклад");
|
||||
Assert.That(salary2.WorkerSalary, Is.EqualTo(1000 + 0.1 * 2000).Within(0.01), "worker2 зарплата оклад + бонус за продажу");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Calculate_PostNotFound_ShouldNotFound_Test()
|
||||
{
|
||||
// Arrange: создаём одну валидную должность, но работнику задаём другой случайный postId
|
||||
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(); // есть пост в БД, но не тот, который у работника
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "name", postId: Guid.NewGuid().ToString());
|
||||
|
||||
var ingredient = SladkieBulkiDbContext.InsertIngredientToDatabaseAndReturn();
|
||||
var product = SladkieBulkiDbContext.InsertProductWithIngredientsToDatabaseAndReturn(
|
||||
new List<Ingredient> { ingredient }, name: "Product 1", unitPrice: 100);
|
||||
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(workerId: worker.Id, productId: product.Id, sum: 2000);
|
||||
SladkieBulkiDbContext.InsertProductionToDatabaseAndReturn(workerId: worker.Id, productId: product.Id, sum: 2000);
|
||||
|
||||
// Act
|
||||
var response = await HttpClient.PostAsync($"/api/salaries/calculate?date={DateTime.UtcNow:O}", null);
|
||||
|
||||
// Assert: ожидаем NotFound (так как должность не найдена)
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
|
||||
private static void AssertElement(SalaryViewModel? actual, Salary expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.WorkerFIO, Is.EqualTo(expected.Worker!.FIO));
|
||||
Assert.That(actual.WorkerSalary, Is.EqualTo(expected.WorkerSalary));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,575 @@
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System.Net;
|
||||
|
||||
namespace SladkieBulkiTests.WebApiControllersTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class WorkerControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
private Post _post;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.RemovePostsFromDatabase();
|
||||
SladkieBulkiDbContext.RemoveWorkersFromDatabase();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenHaveRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId)
|
||||
.AddPost(_post);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/workers/getrecords");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
});
|
||||
AssertElement(data.First(x => x.Id == worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/workers/getrecords");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_OnlyActual_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId, isDeleted: true);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId, isDeleted: false);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId, isDeleted: false);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/workers/getrecords?includeDeleted=false");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
Assert.That(data.All(x => !x.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_IncludeNoActual_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId, isDeleted: true);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId, isDeleted: true);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId, isDeleted: false);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync("/api/workers/getrecords?includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
Assert.That(data.Any(x => x.IsDeleted));
|
||||
Assert.That(data.Any(x => !x.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByPostId_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId, isDeleted: true);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 4");
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getpostrecords?id={_post.PostId}&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(3));
|
||||
Assert.That(data.All(x => x.PostId == _post.PostId));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByPostId_WhenNoRecords_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 4");
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getpostrecords?id={Guid.NewGuid()}&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.That(data, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByPostId_WhenIdIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getpostrecords?id=id&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByBirthDate_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", birthDate: DateTime.UtcNow.AddYears(-25));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 2", birthDate: DateTime.UtcNow.AddYears(-21));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 3", birthDate: DateTime.UtcNow.AddYears(-20), isDeleted: true);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 4", birthDate: DateTime.UtcNow.AddYears(-19));
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getbirthdaterecords?fromDate={DateTime.UtcNow.AddYears(-21).AddMinutes(-1):O}&toDate={DateTime.UtcNow.AddYears(-20).AddMinutes(1):O}&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByBirthDate_WhenDateIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getbirthdaterecords?fromDate={DateTime.UtcNow.AddMinutes(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddMinutes(-1):MM/dd/yyyy HH:mm:ss}&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByEmploymentDate_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 3", employmentDate: DateTime.UtcNow.AddDays(1), isDeleted: true);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(fio: "fio 4", employmentDate: DateTime.UtcNow.AddDays(2));
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getemploymentrecords?fromDate={DateTime.UtcNow.AddDays(-1).AddMinutes(-1):O}&toDate={DateTime.UtcNow.AddDays(1).AddMinutes(1):O}&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
var data = await GetModelFromResponseAsync<List<WorkerViewModel>>(response);
|
||||
Assert.That(data, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(data, Has.Count.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetList_ByEmploymentDate_WhenDateIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getemploymentrecords?fromDate={DateTime.UtcNow.AddMinutes(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddMinutes(-1):MM/dd/yyyy HH:mm:ss}&includeDeleted=true");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(postId: _post.PostId).AddPost(_post);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getrecord/{worker.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<WorkerViewModel>(response), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getrecord/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ById_WhenRecordWasDeleted_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(postId: _post.PostId, isDeleted: true);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getrecord/{worker.Id}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByFIO_WhenHaveRecord_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(postId: _post.PostId).AddPost(_post);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getrecord/{worker.FIO}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
AssertElement(await GetModelFromResponseAsync<WorkerViewModel>(response), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByFIO_WhenNoRecord_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getrecord/New%20Fio");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetElement_ByFIO_WhenRecordWasDeleted_ShouldNotFound_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(postId: _post.PostId, isDeleted: true);
|
||||
//Act
|
||||
var response = await HttpClient.GetAsync($"/api/workers/getrecord/{worker.FIO}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
var workerModel = CreateModel(_post.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/workers/register", MakeContent(workerModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
AssertElement(SladkieBulkiDbContext.GetWorkerFromDatabaseById(workerModel.Id!), workerModel);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerModel = CreateModel(_post.Id);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(workerModel.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/workers/register", MakeContent(workerModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerModelWithIdIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = "Id",
|
||||
FIO = "fio",
|
||||
BirthDate = DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = _post.Id,
|
||||
Email = "test1@email.com"
|
||||
};
|
||||
var workerModelWithFioIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = string.Empty,
|
||||
BirthDate = DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = _post.Id,
|
||||
Email = "test2@email.com"
|
||||
};
|
||||
var workerModelWithPostIdIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = "fio",
|
||||
BirthDate = DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = "Id",
|
||||
Email = "test3@email.com"
|
||||
};
|
||||
var workerModelWithBirthDateIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = "fio",
|
||||
BirthDate = DateTime.UtcNow.AddYears(-15),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = _post.Id,
|
||||
Email = "test4@email.com"
|
||||
};
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/workers/register", MakeContent(workerModelWithIdIncorrect));
|
||||
var responseWithFioIncorrect = await HttpClient.PostAsync($"/api/workers/register", MakeContent(workerModelWithFioIncorrect));
|
||||
var responseWithPostIdIncorrect = await HttpClient.PostAsync($"/api/workers/register", MakeContent(workerModelWithPostIdIncorrect));
|
||||
var responseWithBirthDateIncorrect = await HttpClient.PostAsync($"/api/workers/register", MakeContent(workerModelWithBirthDateIncorrect));
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithFioIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Fio is incorrect");
|
||||
Assert.That(responseWithPostIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "PostId is incorrect");
|
||||
Assert.That(responseWithBirthDateIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "BirthDate is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/workers/register", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PostAsync($"/api/workers/register", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerModel = CreateModel(_post.Id);
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(workerModel.Id);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
AssertElement(SladkieBulkiDbContext.GetWorkerFromDatabaseById(workerModel.Id!), workerModel);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerModel = CreateModel(_post.Id, fio: "new fio");
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenRecordWasDeleted_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerModel = CreateModel(_post.Id, fio: "new fio");
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(workerModel.Id, isDeleted: true);
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModel));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerModelWithIdIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = "Id",
|
||||
FIO = "fio",
|
||||
BirthDate = DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = _post.Id,
|
||||
Email = "put1@email.com"
|
||||
};
|
||||
var workerModelWithFioIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = string.Empty,
|
||||
BirthDate = DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = _post.Id,
|
||||
Email = "put2@email.com"
|
||||
};
|
||||
var workerModelWithPostIdIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = "fio",
|
||||
BirthDate = DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = "Id",
|
||||
Email = "put3@email.com"
|
||||
};
|
||||
var workerModelWithBirthDateIncorrect = new WorkerBindingModel
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = "fio",
|
||||
BirthDate = DateTime.UtcNow.AddYears(-15),
|
||||
EmploymentDate = DateTime.UtcNow.AddDays(-5),
|
||||
PostId = _post.Id,
|
||||
Email = "put4@email.com"
|
||||
};
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModelWithIdIncorrect));
|
||||
var responseWithFioIncorrect = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModelWithFioIncorrect));
|
||||
var responseWithPostIdIncorrect = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModelWithPostIdIncorrect));
|
||||
var responseWithBirthDateIncorrect = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(workerModelWithBirthDateIncorrect));
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithFioIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Fio is incorrect");
|
||||
Assert.That(responseWithPostIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "PostId is incorrect");
|
||||
Assert.That(responseWithBirthDateIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "BirthDate is incorrect");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(string.Empty));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.PutAsync($"/api/workers/changeinfo", MakeContent(new { Data = "test", Position = 10 }));
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldSuccess_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(workerId);
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/workers/delete/{workerId}");
|
||||
SladkieBulkiDbContext.ChangeTracker.Clear();
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
||||
Assert.That(SladkieBulkiDbContext.GetWorkerFromDatabaseById(workerId)!.IsDeleted);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn();
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/workers/delete/{Guid.NewGuid()}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
||||
{
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/workers/delete/id");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenRecordWasDeleted_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(isDeleted: true).Id;
|
||||
//Act
|
||||
var response = await HttpClient.DeleteAsync($"/api/workers/delete/{workerId}");
|
||||
//Assert
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
||||
}
|
||||
|
||||
private static void AssertElement(WorkerViewModel? actual, Worker expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.Post!.PostName));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.BirthDate.ToString(), Is.EqualTo(expected.BirthDate.ToString()));
|
||||
Assert.That(actual.EmploymentDate.ToString(), Is.EqualTo(expected.EmploymentDate.ToString()));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
private static WorkerBindingModel CreateModel(string postId, string? id = null, string fio = "fio", DateTime? birthDate = null, DateTime? employmentDate = null, string email = "test@email.com")
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
FIO = fio,
|
||||
BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-22),
|
||||
EmploymentDate = employmentDate ?? DateTime.UtcNow.AddDays(-5),
|
||||
PostId = postId,
|
||||
Email = email
|
||||
};
|
||||
}
|
||||
|
||||
private static void AssertElement(Worker? actual, WorkerBindingModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.BirthDate.ToString(), Is.EqualTo(expected.BirthDate.ToString()));
|
||||
Assert.That(actual.EmploymentDate.ToString(), Is.EqualTo(expected.EmploymentDate.ToString()));
|
||||
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
||||
Assert.That(!actual.IsDeleted);
|
||||
});
|
||||
}
|
||||
}
|
||||
24
SladkieBulki/SladkieBulkiTests/appsettings.json
Normal file
24
SladkieBulki/SladkieBulkiTests/appsettings.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information"
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "../logs/sladkiebulki-.log",
|
||||
"rollingInterval": "Day",
|
||||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3} {Username} {Message:lj}{Exception}{NewLine}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
177
SladkieBulki/SladkieBulkiWedApi/Adapters/IngredientAdapter.cs
Normal file
177
SladkieBulki/SladkieBulkiWedApi/Adapters/IngredientAdapter.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.BusinessLogicsContracts;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiContrakts.AdapterContracts;
|
||||
|
||||
namespace SladkieBulkiWedApi.Adapters;
|
||||
|
||||
public class IngredientAdapter : IIngredientAdapter
|
||||
{
|
||||
private readonly IIngredientBusinessLogicContract _ingredientBusinessLogicContract;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public IngredientAdapter(IIngredientBusinessLogicContract ingredientBusinessLogicContract, ILogger<IngredientAdapter> logger)
|
||||
{
|
||||
_ingredientBusinessLogicContract = ingredientBusinessLogicContract;
|
||||
_logger = logger;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<IngredientBindingModel, IngredientDataModel>();
|
||||
cfg.CreateMap<IngredientDataModel, IngredientViewModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public IngredientOperationResponse GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return IngredientOperationResponse.OK(
|
||||
_ingredientBusinessLogicContract.GetAllIngredients()
|
||||
.Select(x => _mapper.Map<IngredientViewModel>(x))
|
||||
.ToList()
|
||||
);
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
_logger.LogError("NullListException");
|
||||
return IngredientOperationResponse.NotFound("The list is not initialized");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return IngredientOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IngredientOperationResponse GetElement(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
IngredientDataModel model;
|
||||
if (Guid.TryParse(data, out _))
|
||||
model = _ingredientBusinessLogicContract.GetIngredientById(data);
|
||||
else
|
||||
model = _ingredientBusinessLogicContract.GetIngredientByName(data);
|
||||
|
||||
return IngredientOperationResponse.OK(
|
||||
_mapper.Map<IngredientViewModel>(model)
|
||||
);
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return IngredientOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return IngredientOperationResponse.NotFound($"Not found element by data {data}");
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return IngredientOperationResponse.BadRequest($"Element by data: {data} was deleted");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return IngredientOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public IngredientOperationResponse RegisterIngredient(IngredientBindingModel ingredientModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_ingredientBusinessLogicContract.InsertIngredient(_mapper.Map<IngredientDataModel>(ingredientModel));
|
||||
return IngredientOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return IngredientOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return IngredientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException");
|
||||
return IngredientOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return IngredientOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public IngredientOperationResponse ChangeIngredientInfo(IngredientBindingModel ingredientModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_ingredientBusinessLogicContract.UpdateIngredient(_mapper.Map<IngredientDataModel>(ingredientModel));
|
||||
return IngredientOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return IngredientOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return IngredientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return IngredientOperationResponse.BadRequest($"Not found element by Id {ingredientModel.Id}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException");
|
||||
return IngredientOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return IngredientOperationResponse.BadRequest($"Element by id: {ingredientModel.Id} was deleted");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return IngredientOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
270
SladkieBulki/SladkieBulkiWedApi/Adapters/PostAdapter.cs
Normal file
270
SladkieBulki/SladkieBulkiWedApi/Adapters/PostAdapter.cs
Normal file
@@ -0,0 +1,270 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.BusinessLogicsContracts;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiContrakts.AdapterContracts;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SladkieBulkiWedApi.Adapters;
|
||||
|
||||
public class PostAdapter : IPostAdapter
|
||||
{
|
||||
private readonly IPostBusinessLogicContract _postBusinessLogicContract;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
private readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
public PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, ILogger<PostAdapter> logger)
|
||||
{
|
||||
_postBusinessLogicContract = postBusinessLogicContract;
|
||||
_logger = logger;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<PostBindingModel, PostDataModel>();
|
||||
cfg.CreateMap<PostDataModel, PostViewModel>()
|
||||
.ForMember(x => x.Configuration, x => x.MapFrom(src => JsonSerializer.Serialize(src.ConfigurationModel, JsonSerializerOptions)));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public PostOperationResponse GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllPosts().Select(x => _mapper.Map<PostViewModel>(x))]);
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
_logger.LogError("NullListException");
|
||||
return PostOperationResponse.NotFound("The list is not initialized");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public PostOperationResponse GetHistory(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllDataOfPost(id).Select(x => _mapper.Map<PostViewModel>(x))]);
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return PostOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public PostOperationResponse GetElement(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
return PostOperationResponse.OK(_mapper.Map<PostViewModel>(_postBusinessLogicContract.GetPostByData(data)));
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return PostOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return PostOperationResponse.NotFound($"Not found element by data {data}");
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return PostOperationResponse.BadRequest($"Element by data: {data} was deleted");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public PostOperationResponse RegisterPost(PostBindingModel postModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_postBusinessLogicContract.InsertPost(_mapper.Map<PostDataModel>(postModel));
|
||||
return PostOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return PostOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException");
|
||||
return PostOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public PostOperationResponse ChangePostInfo(PostBindingModel postModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_postBusinessLogicContract.UpdatePost(_mapper.Map<PostDataModel>(postModel));
|
||||
return PostOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return PostOperationResponse.BadRequest("Data is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return PostOperationResponse.BadRequest($"Not found element by Id {postModel.Id}");
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementExistsException");
|
||||
return PostOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return PostOperationResponse.BadRequest($"Element by id: {postModel.Id} was deleted");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public PostOperationResponse RemovePost(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_postBusinessLogicContract.DeletePost(id);
|
||||
return PostOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return PostOperationResponse.BadRequest("Id is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return PostOperationResponse.BadRequest($"Not found element by id: {id}");
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return PostOperationResponse.BadRequest($"Element by id: {id} was deleted");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public PostOperationResponse RestorePost(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_postBusinessLogicContract.RestorePost(id);
|
||||
return PostOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ArgumentNullException");
|
||||
return PostOperationResponse.BadRequest("Id is empty");
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return PostOperationResponse.BadRequest($"Not found element by id: {id}");
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return PostOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return PostOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
150
SladkieBulki/SladkieBulkiWedApi/Adapters/ProductAdapter.cs
Normal file
150
SladkieBulki/SladkieBulkiWedApi/Adapters/ProductAdapter.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.AdapterContracts;
|
||||
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
|
||||
using SladkieBulkiContrakts.BindingModels;
|
||||
using SladkieBulkiContrakts.BusinessLogicsContracts;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.ViewModels;
|
||||
|
||||
namespace SladkieBulkiWedApi.Adapters;
|
||||
|
||||
public class ProductAdapter : IProductAdapter
|
||||
{
|
||||
private readonly IProductBusinessLogicContract _logic;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public ProductAdapter(IProductBusinessLogicContract logic, ILogger<ProductAdapter> logger)
|
||||
{
|
||||
_logic = logic;
|
||||
_logger = logger;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<ProductBindingModel, ProductDataModel>();
|
||||
cfg.CreateMap<ProductDataModel, ProductViewModel>();
|
||||
});
|
||||
_mapper = config.CreateMapper();
|
||||
}
|
||||
|
||||
public ProductOperationResponse GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = _logic.GetAllProducts();
|
||||
return ProductOperationResponse.OK(list.Select(x => _mapper.Map<ProductViewModel>(x)).ToList());
|
||||
}
|
||||
catch (NullListException)
|
||||
{
|
||||
return ProductOperationResponse.NotFound("No records found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return ProductOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductOperationResponse GetElement(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProductDataModel model;
|
||||
if (Guid.TryParse(data, out _))
|
||||
model = _logic.GetProductByData(data);
|
||||
else
|
||||
model = _logic.GetProductByData(data);
|
||||
|
||||
return ProductOperationResponse.OK(_mapper.Map<ProductViewModel>(model));
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
return ProductOperationResponse.NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return ProductOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductOperationResponse RegisterProduct(ProductBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dataModel = _mapper.Map<ProductDataModel>(model);
|
||||
_logic.InsertProduct(dataModel);
|
||||
return ProductOperationResponse.NoContent();
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return ProductOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductOperationResponse ChangeProductInfo(ProductBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dataModel = _mapper.Map<ProductDataModel>(model);
|
||||
_logic.UpdateProduct(dataModel);
|
||||
return ProductOperationResponse.NoContent();
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return ProductOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductOperationResponse DeleteProduct(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logic.DeleteProduct(id);
|
||||
return ProductOperationResponse.NoContent();
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
return ProductOperationResponse.BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Exception");
|
||||
return ProductOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user