18 Commits

Author SHA1 Message Date
50a0807cdf семафор 2025-05-21 12:43:59 +04:00
4abc5ebcfb забыл тесты раскомитить 2025-05-21 12:26:01 +04:00
681ae1d484 финальная миграция 2025-05-21 02:29:39 +04:00
1b9cbc274d правки в зп
даты.... и <= <
2025-05-21 02:28:26 +04:00
55c269a934 2 миграция 2025-05-21 00:39:54 +04:00
7e8e7a5ee8 правка моделей и тестов 2025-05-21 00:13:39 +04:00
b4c6d0110a миграция 2025-05-20 19:56:48 +04:00
f9cfe3dc00 Лаба готова к сдаче 2025-05-20 19:05:02 +04:00
5c1b458ffd почти все 2025-05-20 18:24:23 +04:00
2b92a2b9bd продкт 2025-05-19 23:18:00 +04:00
c960ed7320 + 2025-05-19 20:56:33 +04:00
4393d5097e часть производства 2025-05-19 20:48:39 +04:00
f1c2d1d3d8 ингредиент 2025-05-19 19:52:10 +04:00
cea9386132 Работник 2025-05-19 01:38:28 +04:00
620832b1a8 Изменения в должности в соответствии с примером. Зеленая зона соответственно 2025-05-19 00:35:03 +04:00
f886b16532 подготовка к добавлению контроллеров, адатеров и тестов 2025-05-18 21:22:47 +04:00
db9da65e76 небольшое начало 2025-05-18 18:41:36 +04:00
629e20e498 начало. Добавлен проект 2025-05-14 16:53:32 +04:00
103 changed files with 7491 additions and 726 deletions

View File

@@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiTests", "Sladki
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiBusinessLogic", "SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj", "{004CA71C-5308-44C7-82DA-4098BA6F5F1C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SladkieBulkiDatabase", "SladkieBulkiDatabase\SladkieBulkiDatabase.csproj", "{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}"
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
@@ -33,6 +35,10 @@ Global
{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

View File

@@ -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)

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="SladkieBulkiWedApi" />
<InternalsVisibleTo Include="SladkieBulkiTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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();
}

View File

@@ -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!;
}

View File

@@ -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; }
}

View File

@@ -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);

View File

@@ -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 postId, 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; } = 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 postId, string postName, PostType postType, do
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");
}
}

View File

@@ -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> productIngredients, 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; } = productIngredients;
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");
}
}

View File

@@ -10,9 +10,15 @@ 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 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())

View File

@@ -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())

View File

@@ -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; }
}

View File

@@ -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 };
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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>

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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; } = [];
}

View File

@@ -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!;
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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 => "";
}

View File

@@ -29,21 +29,17 @@ internal class PostStorageContract : IPostStorageContract
.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.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(bool onlyActual = true)
public List<PostDataModel> GetList()
{
try
{
var query = _dbContext.Posts.AsQueryable();
if (onlyActual)
{
query = query.Where(x => x.IsActual);
}
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
return [.. _dbContext.Posts.Select(x => _mapper.Map<PostDataModel>(x))];
}
catch (Exception ex)
{

View File

@@ -4,6 +4,7 @@ using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.StoragesContarcts;
using SladkieBulkiDatabase.Models;
using SladkieBulkiDatabase;
using Microsoft.EntityFrameworkCore;
internal class SalaryStorageContract : ISalaryStorageContract
{
@@ -15,7 +16,10 @@ internal class SalaryStorageContract : ISalaryStorageContract
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(SladkieBulkiDbContext).Assembly);
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);
}
@@ -24,7 +28,8 @@ internal class SalaryStorageContract : ISalaryStorageContract
{
try
{
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
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);

View File

@@ -18,7 +18,10 @@ internal class WorkerStorageContract : IWorkerStorageContract
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(Worker).Assembly);
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);
}
@@ -44,7 +47,7 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
}
return [.. query.Select(x => _mapper.Map<WorkerDataModel>(x))];
return [.. JoinPost(query).Select(x => _mapper.Map<WorkerDataModel>(x))];
}
catch (Exception ex)
{
@@ -70,7 +73,7 @@ internal class WorkerStorageContract : IWorkerStorageContract
{
try
{
return _mapper.Map<WorkerDataModel>(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio));
return _mapper.Map<WorkerDataModel>(AddPost(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio && !x.IsDeleted)));
}
catch (Exception ex)
{
@@ -91,11 +94,6 @@ internal class WorkerStorageContract : IWorkerStorageContract
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Id", workerDataModel.Id);
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Workers_Email" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Email", workerDataModel.Email);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
@@ -103,14 +101,12 @@ internal class WorkerStorageContract : IWorkerStorageContract
}
}
public void UpdElement(WorkerDataModel workerDataModel)
{
try
{
var element = GetWorkerById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id);
_mapper.Map(workerDataModel, element);
_dbContext.Workers.Update(_mapper.Map(workerDataModel, element));
_dbContext.SaveChanges();
}
catch (ElementNotFoundException)
@@ -118,11 +114,6 @@ internal class WorkerStorageContract : IWorkerStorageContract
_dbContext.ChangeTracker.Clear();
throw;
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Workers_Email" })
{
_dbContext.ChangeTracker.Clear();
throw new ElementExistsException("Email", workerDataModel.Email);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
@@ -130,13 +121,13 @@ internal class WorkerStorageContract : IWorkerStorageContract
}
}
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)
@@ -151,6 +142,27 @@ internal class WorkerStorageContract : IWorkerStorageContract
}
}
private Worker? GetWorkerById(string id) => _dbContext.Workers.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
}
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));
}

View 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
}
}
}

View File

@@ -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");
}
}
}

View 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
}
}
}

View File

@@ -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);
}
}
}

View 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
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -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
}
}
}

View File

@@ -1,4 +1,5 @@
using SladkieBulkiContrakts.Enums;
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -13,7 +14,7 @@ internal class Post
public required string PostId { get; set; }
public required string PostName { get; set; }
public PostType PostType { get; set; }
public double Salary { get; set; }
public required PostConfiguration Configuration { get; set; }
public bool IsActual { get; set; }
public DateTime ChangeDate { get; set; }
}

View File

@@ -8,7 +8,6 @@ using System.Threading.Tasks;
namespace SladkieBulkiDatabase.Models;
[AutoMap(typeof(SalaryDataModel), ReverseMap = true)]
internal class Salary
{
public string Id { get; set; } = Guid.NewGuid().ToString();

View File

@@ -14,15 +14,25 @@ internal class Worker
{
public required string Id { get; set; }
public required string FIO { get; set; }
public string PostId { 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;
}
}

View 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());
}
}

View File

@@ -9,6 +9,11 @@
<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>
@@ -17,6 +22,7 @@
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="SladkieBulkiWedApi" />
<InternalsVisibleTo Include="SladkieBulkiTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

View File

@@ -1,14 +1,23 @@
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(IConfigurationDatabase configurationDatabase): DbContext
internal class SladkieBulkiDbContext: DbContext
{
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
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));
@@ -37,6 +46,14 @@ internal class SladkieBulkiDbContext(IConfigurationDatabase configurationDatabas
.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)
);
}
@@ -49,5 +66,12 @@ internal class SladkieBulkiDbContext(IConfigurationDatabase configurationDatabas
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)!,
};
}

View File

@@ -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);
}
}

View File

@@ -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) =>

View File

@@ -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;
@@ -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;
@@ -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

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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" />
@@ -22,6 +33,7 @@
<ProjectReference Include="..\SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj" />
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
<ProjectReference Include="..\SladkieBulkiDatabase\SladkieBulkiDatabase.csproj" />
<ProjectReference Include="..\SladkieBulkiWedApi\SladkieBulkiWedApi.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -3,8 +3,10 @@ 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;
@@ -22,15 +24,15 @@ internal class PostStorageContractTests : BaseStorageContractTest
[TearDown]
public void TearDown()
{
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
SladkieBulkiDbContext.RemovePostsFromDatabase();
}
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
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));
@@ -46,43 +48,25 @@ internal class PostStorageContractTests : BaseStorageContractTest
}
[Test]
public void Try_GetList_OnlyActual_Test()
public void Try_GetList_WhenDifferentConfigTypes_Test()
{
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isActual: true);
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isActual: false);
var list = _postStorageContract.GetList(onlyActual: true);
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);
Assert.Multiple(() =>
{
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(!list.Any(x => !x.IsActual));
});
}
[Test]
public void Try_GetList_IncludeNoActual_Test()
{
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isActual: true);
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isActual: false);
var list = _postStorageContract.GetList(onlyActual: false);
Assert.That(list, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(list, Has.Count.EqualTo(3));
Assert.That(list.Count(x => x.IsActual), Is.EqualTo(2));
Assert.That(list.Count(x => !x.IsActual), Is.EqualTo(1));
});
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();
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
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));
@@ -92,9 +76,9 @@ internal class PostStorageContractTests : BaseStorageContractTest
public void Try_GetPostWithHistory_WhenNoRecords_Test()
{
var postId = Guid.NewGuid().ToString();
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
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));
@@ -103,103 +87,117 @@ internal class PostStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetElementById_WhenHaveRecord_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
AssertElement(_postStorageContract.GetElementById(post.PostId), post);
}
[Test]
public void Try_GetElementById_WhenNoRecord_Test()
{
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
Assert.That(() => _postStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
}
[Test]
public void Try_GetElementById_WhenRecordHasDeleted_Test()
public void Try_GetElementById_WhenRecordWasDeleted_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
Assert.That(() => _postStorageContract.GetElementById(post.PostId), Is.Null);
}
[Test]
public void Try_GetElementById_WhenTrySearchById_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
Assert.That(() => _postStorageContract.GetElementById(post.Id), Is.Null);
}
[Test]
public void Try_GetElementByName_WhenHaveRecord_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
AssertElement(_postStorageContract.GetElementByName(post.PostName), post);
}
[Test]
public void Try_GetElementByName_WhenNoRecord_Test()
{
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn();
Assert.That(() => _postStorageContract.GetElementByName("name"), Is.Null);
}
[Test]
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
public void Try_GetElementByName_WhenRecordWasDeleted_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
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(), isActual: true);
var post = CreateModel(Guid.NewGuid().ToString());
_postStorageContract.AddElement(post);
AssertElement(GetPostFromDatabaseByPostId(post.Id), post);
}
[Test]
public void Try_AddElement_WhenActualIsFalse_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), isActual: false);
Assert.That(() => _postStorageContract.AddElement(post), Throws.Nothing);
AssertElement(GetPostFromDatabaseByPostId(post.Id), CreateModel(post.Id, isActual: true));
AssertElement(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.Id), post);
}
[Test]
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), "name unique", isActual: true);
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName: post.PostName, isActual: true);
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_WhenHaveRecordWithSamePostIdAndActualIsTrue_Test()
public void Try_AddElement_WhenHaveRecordWithSamePostId_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), isActual: true);
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
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());
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
_postStorageContract.UpdElement(post);
var posts = SladkieBulkiDbContext.Posts.Where(x => x.PostId == post.Id).OrderByDescending(x => x.ChangeDate);
Assert.That(posts.Count(), Is.EqualTo(2));
AssertElement(posts.First(), CreateModel(post.Id, isActual: true));
AssertElement(posts.Last(), CreateModel(post.Id, isActual: false));
}
[Test]
public void Try_UpdElement_WhenActualIsFalse_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), isActual: false);
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
_postStorageContract.UpdElement(post);
AssertElement(GetPostFromDatabaseByPostId(post.Id), CreateModel(post.Id, isActual: true));
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]
@@ -212,8 +210,8 @@ internal class PostStorageContractTests : BaseStorageContractTest
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), "New Name");
InsertPostToDatabaseAndReturn(post.Id, postName: "name");
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName: post.PostName);
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(post.Id, postName: "name");
SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName);
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementExistsException>());
}
@@ -221,21 +219,46 @@ internal class PostStorageContractTests : BaseStorageContractTest
public void Try_UpdElement_WhenRecordWasDeleted_Test()
{
var post = CreateModel(Guid.NewGuid().ToString());
InsertPostToDatabaseAndReturn(post.Id, isActual: false);
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 = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: true);
_postStorageContract.DelElement(post.PostId);
var element = GetPostFromDatabaseByPostId(post.PostId);
Assert.Multiple(() =>
{
Assert.That(element, Is.Not.Null);
Assert.That(!element!.IsActual);
});
Assert.That(SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.PostId), Is.Null);
}
[Test]
@@ -247,16 +270,16 @@ internal class PostStorageContractTests : BaseStorageContractTest
[Test]
public void Try_DelElement_WhenRecordWasDeleted_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
Assert.That(() => _postStorageContract.DelElement(post.PostId), Throws.TypeOf<ElementDeletedException>());
}
[Test]
public void Try_ResElement_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: false);
_postStorageContract.ResElement(post.PostId);
var element = GetPostFromDatabaseByPostId(post.PostId);
var element = SladkieBulkiDbContext.GetPostFromDatabaseByPostId(post.PostId);
Assert.Multiple(() =>
{
Assert.That(element, Is.Not.Null);
@@ -273,18 +296,10 @@ internal class PostStorageContractTests : BaseStorageContractTest
[Test]
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
{
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
var post = SladkieBulkiDbContext.InsertPostToDatabaseAndReturn(isActual: true);
Assert.That(() => _postStorageContract.ResElement(post.PostId), Throws.Nothing);
}
private Post InsertPostToDatabaseAndReturn(string id, string postName = "test", PostType postType = PostType.Preform, double salary = 10, bool isActual = true, DateTime? changeDate = null)
{
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id, PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
SladkieBulkiDbContext.Posts.Add(post);
SladkieBulkiDbContext.SaveChanges();
return post;
}
private static void AssertElement(PostDataModel? actual, Post expected)
{
Assert.That(actual, Is.Not.Null);
@@ -293,15 +308,12 @@ internal class PostStorageContractTests : BaseStorageContractTest
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.Salary, Is.EqualTo(expected.Salary));
Assert.That(actual.IsActual, Is.EqualTo(expected.IsActual));
Assert.That(actual.ConfigurationModel.Rate, Is.EqualTo(expected.Configuration.Rate));
});
}
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.Preform, double salary = 10, bool isActual = false, DateTime? changeDate = null)
=> new(postId, postName, postType, salary, isActual, changeDate ?? DateTime.UtcNow);
private Post? GetPostFromDatabaseByPostId(string id) => SladkieBulkiDbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
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)
{
@@ -311,8 +323,7 @@ internal class PostStorageContractTests : BaseStorageContractTest
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.Salary, Is.EqualTo(expected.Salary));
Assert.That(actual.IsActual, Is.EqualTo(expected.IsActual));
Assert.That(actual.Configuration.Rate, Is.EqualTo(expected.ConfigurationModel.Rate));
});
}
}

View File

@@ -34,15 +34,26 @@ internal class SalaryStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var salary = InsertSalaryToDatabaseAndReturn(_worker.Id, workerSalary: 100);
InsertSalaryToDatabaseAndReturn(_worker.Id);
InsertSalaryToDatabaseAndReturn(_worker.Id);
// 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));
AssertElement(list.First(), salary);
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()
{

View File

@@ -3,6 +3,7 @@ using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiDatabase.Implementations;
using SladkieBulkiDatabase.Models;
using SladkieBulkiTests.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -30,15 +31,18 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1");
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2");
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
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(), worker);
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()
{
@@ -51,9 +55,9 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
public void Try_GetList_ByPostId_Test()
{
var postId = Guid.NewGuid().ToString();
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId);
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId);
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
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));
@@ -63,10 +67,10 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetList_ByBirthDate_Test()
{
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", birthDate: DateTime.UtcNow.AddYears(-25));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", birthDate: DateTime.UtcNow.AddYears(-21));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", birthDate: DateTime.UtcNow.AddYears(-20));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-19));
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));
@@ -75,10 +79,10 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetList_ByEmploymentDate_Test()
{
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", employmentDate: DateTime.UtcNow.AddDays(1));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", employmentDate: DateTime.UtcNow.AddDays(2));
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));
@@ -88,10 +92,10 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
public void Try_GetList_ByAllParameters_Test()
{
var postId = Guid.NewGuid().ToString();
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
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));
@@ -100,7 +104,7 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetElementById_WhenHaveRecord_Test()
{
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_workerStorageContract.GetElementById(worker.Id), worker);
}
@@ -113,7 +117,7 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetElementByFIO_WhenHaveRecord_Test()
{
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_workerStorageContract.GetElementByFIO(worker.FIO), worker);
}
@@ -128,14 +132,14 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
{
var worker = CreateModel(Guid.NewGuid().ToString());
_workerStorageContract.AddElement(worker);
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
AssertElement(SladkieBulkiDbContext.GetWorkerFromDatabaseById(worker.Id), worker);
}
[Test]
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
{
var worker = CreateModel(Guid.NewGuid().ToString());
InsertWorkerToDatabaseAndReturn(worker.Id);
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id);
Assert.That(() => _workerStorageContract.AddElement(worker), Throws.TypeOf<ElementExistsException>());
}
@@ -143,9 +147,9 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
public void Try_UpdElement_Test()
{
var worker = CreateModel(Guid.NewGuid().ToString(), "New Fio");
InsertWorkerToDatabaseAndReturn(worker.Id);
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id);
_workerStorageContract.UpdElement(worker);
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
AssertElement(SladkieBulkiDbContext.GetWorkerFromDatabaseById(worker.Id), worker);
}
[Test]
@@ -158,16 +162,16 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
{
var worker = CreateModel(Guid.NewGuid().ToString());
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
Assert.That(() => _workerStorageContract.UpdElement(worker), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_DelElement_Test()
{
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
var worker = SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
_workerStorageContract.DelElement(worker.Id);
var element = GetWorkerFromDatabase(worker.Id);
var element = SladkieBulkiDbContext.GetWorkerFromDatabaseById(worker.Id);
Assert.That(element, Is.Not.Null);
Assert.That(element.IsDeleted);
}
@@ -182,32 +186,44 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
{
var worker = CreateModel(Guid.NewGuid().ToString());
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
SladkieBulkiDbContext.InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
Assert.That(() => _workerStorageContract.DelElement(worker.Id), Throws.TypeOf<ElementNotFoundException>());
}
private Worker InsertWorkerToDatabaseAndReturn(
string id,
string fio = "test",
string? postId = null,
DateTime? birthDate = null,
DateTime? employmentDate = null,
bool isDeleted = false,
string email = "test@example.com")
[Test]
public void Try_GetWorkerTrend_WhenNoNewAndDeletedWorkers_Test()
{
var worker = new Worker()
{
Id = id,
FIO = fio,
Email = email,
PostId = postId ?? Guid.NewGuid().ToString(),
BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20),
EmploymentDate = employmentDate ?? DateTime.UtcNow,
IsDeleted = isDeleted
};
SladkieBulkiDbContext.Workers.Add(worker);
SladkieBulkiDbContext.SaveChanges();
return worker;
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)
@@ -224,18 +240,9 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
});
}
private static WorkerDataModel CreateModel(
string id,
string fio = "fio",
string? postId = null,
DateTime? birthDate = null,
DateTime? employmentDate = null,
bool isDeleted = false,
string email = "fio@example.com") =>
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 Worker? GetWorkerFromDatabase(string id) => SladkieBulkiDbContext.Workers.FirstOrDefault(x => x.Id == id);
private static void AssertElement(Worker? actual, WorkerDataModel expected)
{
Assert.That(actual, Is.Not.Null);
@@ -247,6 +254,7 @@ internal class WorkerStorageContractTests : BaseStorageContractTest
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));
});
}
}

View File

@@ -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");
}

View File

@@ -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));
});
}
}

View File

@@ -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>()));
});
}
}

View File

@@ -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));
});
}
}

View File

@@ -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));
});
}
}

View File

@@ -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));
});
}
}

View File

@@ -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);
});
}
}

View 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}"
}
}
]
}
}

View 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);
}
}
}

View 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);
}
}
}

View 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);
}
}
}

View File

@@ -0,0 +1,101 @@
using AutoMapper;
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
using SladkieBulkiContrakts.BindingModels;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.ViewModels;
namespace SladkieBulkiWedApi.Adapters;
public class ProductionAdapter : IProductionAdapter
{
private readonly IProductionBusinessLogicContract _productionLogic;
private readonly ILogger _logger;
private readonly IMapper _mapper;
public ProductionAdapter(IProductionBusinessLogicContract productionLogic, ILogger<ProductionAdapter> logger)
{
_productionLogic = productionLogic;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ProductionBindingModel, ProductionDataModel>();
cfg.CreateMap<ProductionDataModel, ProductionViewModel>();
});
_mapper = config.CreateMapper();
}
public ProductionOperationResponse GetList(DateTime? from = null, DateTime? to = null, string? workerId = null, string? productId = null)
{
try
{
var data = _productionLogic.GetAllSalesByPeriod(from ?? DateTime.MinValue, to ?? DateTime.MaxValue);
return ProductionOperationResponse.OK(data.Select(x => _mapper.Map<ProductionViewModel>(x)).ToList());
}
catch (NullListException)
{
return ProductionOperationResponse.NotFound("No records found");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ProductionOperationResponse.InternalServerError(ex.Message);
}
}
public ProductionOperationResponse GetElement(string id)
{
try
{
var element = _productionLogic.GetProductionByData(id);
return ProductionOperationResponse.OK(_mapper.Map<ProductionViewModel>(element));
}
catch (ElementNotFoundException ex)
{
return ProductionOperationResponse.NotFound(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ProductionOperationResponse.InternalServerError(ex.Message);
}
}
public ProductionOperationResponse RegisterProduction(ProductionBindingModel model)
{
try
{
var dataModel = _mapper.Map<ProductionDataModel>(model);
_productionLogic.InsertProduction(dataModel);
return ProductionOperationResponse.NoContent();
}
catch (ValidationException ex)
{
return ProductionOperationResponse.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ProductionOperationResponse.InternalServerError(ex.Message);
}
}
public ProductionOperationResponse CancelProduction(string id)
{
try
{
_productionLogic.CancelProduction(id);
return ProductionOperationResponse.NoContent();
}
catch (ElementNotFoundException ex)
{
return ProductionOperationResponse.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ProductionOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,126 @@
using AutoMapper;
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.ViewModels;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
namespace SladkieBulkiWedApi.Adapters;
public class SalaryAdapter : ISalaryAdapter
{
private readonly ISalaryBusinessLogicContract _salaryBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public SalaryAdapter(ISalaryBusinessLogicContract salaryBusinessLogicContract, ILogger<SalaryAdapter> logger)
{
_salaryBusinessLogicContract = salaryBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SalaryDataModel, SalaryViewModel>();
});
_mapper = new Mapper(config);
}
public SalaryOperationResponse GetListByPeriod(DateTime fromDate, DateTime toDate)
{
try
{
return SalaryOperationResponse.OK([.. _salaryBusinessLogicContract.GetAllSalariesByPeriod(fromDate, toDate).Select(x => _mapper.Map<SalaryViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SalaryOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return SalaryOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return SalaryOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SalaryOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SalaryOperationResponse.InternalServerError(ex.Message);
}
}
public SalaryOperationResponse GetListByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
{
try
{
return SalaryOperationResponse.OK([.. _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(fromDate, toDate, workerId).Select(x => _mapper.Map<SalaryViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SalaryOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return SalaryOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return SalaryOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SalaryOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SalaryOperationResponse.InternalServerError(ex.Message);
}
}
public SalaryOperationResponse CalculateSalary(DateTime date)
{
try
{
_salaryBusinessLogicContract.CalculateSalaryByMounth(date);
return SalaryOperationResponse.NoContent();
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return SalaryOperationResponse.NoContent(); // <-- вот это 404
}
catch (NullListException ex)
{
_logger.LogError(ex, "NullListException");
return SalaryOperationResponse.NotFound(ex.Message);// <-- это тоже 404 если надо
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SalaryOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SalaryOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,278 @@
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 WorkerAdapter : IWorkerAdapter
{
private readonly IWorkerBusinessLogicContract _buyerBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public WorkerAdapter(IWorkerBusinessLogicContract workerBusinessLogicContract, ILogger<WorkerAdapter> logger)
{
_buyerBusinessLogicContract = workerBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<WorkerBindingModel, WorkerDataModel>();
cfg.CreateMap<WorkerDataModel, WorkerViewModel>();
});
_mapper = new Mapper(config);
}
public WorkerOperationResponse GetList(bool includeDeleted)
{
try
{
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkers(!includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return WorkerOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse GetPostList(string id, bool includeDeleted)
{
try
{
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkersByPost(id, !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return WorkerOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse GetListByBirthDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
{
try
{
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkersByBirthDate(fromDate, toDate, !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return WorkerOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return WorkerOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse GetListByEmploymentDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
{
try
{
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkersByEmploymentDate(fromDate, toDate, !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return WorkerOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return WorkerOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse GetElement(string data)
{
try
{
return WorkerOperationResponse.OK(_mapper.Map<WorkerViewModel>(_buyerBusinessLogicContract.GetWorkerByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return WorkerOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return WorkerOperationResponse.NotFound($"Not found element by data {data}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse RegisterWorker(WorkerBindingModel workerModel)
{
try
{
_buyerBusinessLogicContract.InsertWorker(_mapper.Map<WorkerDataModel>(workerModel));
return WorkerOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return WorkerOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return WorkerOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse ChangeWorkerInfo(WorkerBindingModel workerModel)
{
try
{
_buyerBusinessLogicContract.UpdateWorker(_mapper.Map<WorkerDataModel>(workerModel));
return WorkerOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return WorkerOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return WorkerOperationResponse.BadRequest($"Not found element by Id {workerModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return WorkerOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
public WorkerOperationResponse RemoveWorker(string id)
{
try
{
_buyerBusinessLogicContract.DeleteWorker(id);
return WorkerOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return WorkerOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return WorkerOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return WorkerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return WorkerOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,13 @@
using Microsoft.IdentityModel.Tokens;
using System.Text;
namespace SladkieBulkiWedApi;
public class AuthOptions
{
public const string ISSUER = "CatHasPaws_AuthServer"; // издатель токена
public const string AUDIENCE = "CatHasPaws_AuthClient"; // потребитель токена
const string KEY = "catsecret_secretsecretsecretkey!123"; // ключ для шифрации
public static SymmetricSecurityKey GetSymmetricSecurityKey() => new(Encoding.UTF8.GetBytes(KEY));
}

View File

@@ -0,0 +1,55 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BindingModels;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.StoragesContarcts;
namespace SladkieBulkiWedApi.Controllers;
[Authorize]
[Route("api/[controller]/[action]")]
[ApiController]
[Produces("application/json")]
public class IngredientController(IIngredientAdapter adapter) : ControllerBase
{
private readonly IIngredientAdapter _adapter = adapter;
/// <summary>
/// Получить список всех ингредиентов.
/// </summary>
[HttpGet]
public IActionResult GetRecords()
{
return _adapter.GetList().GetResponse(Request, Response);
}
/// <summary>
/// Получить ингредиент по идентификатору (id или name).
/// </summary>
[HttpGet("{data}")]
public IActionResult GetRecord(string data)
{
return _adapter.GetElement(data).GetResponse(Request, Response);
}
/// <summary>
/// Зарегистрировать новый ингредиент.
/// </summary>
[HttpPost]
public IActionResult Register([FromBody] IngredientBindingModel model)
{
return _adapter.RegisterIngredient(model).GetResponse(Request, Response);
}
/// <summary>
/// Изменить информацию об ингредиенте.
/// </summary>
[HttpPut]
public IActionResult ChangeInfo([FromBody] IngredientBindingModel model)
{
return _adapter.ChangeIngredientInfo(model).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,20 @@
using SladkieBulkiContrakts.AdapterContracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace SladkieBulkiWedApi.Controllers;
[Authorize]
[Route("api/[controller]")]
[ApiController]
[Produces("application/json")]
public class PostHistoryController(IPostAdapter adapter) : ControllerBase
{
private readonly IPostAdapter _adapter = adapter;
[HttpGet("{id}")]
public IActionResult GetHistory(string id)
{
return _adapter.GetHistory(id).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,51 @@
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BindingModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace SladkieBulkiWebApi.Controllers;
[Authorize]
[Route("api/[controller]")]
[ApiController]
[Produces("application/json")]
public class PostsController(IPostAdapter adapter) : ControllerBase
{
private readonly IPostAdapter _adapter = adapter;
[HttpGet]
public IActionResult GetRecords()
{
return _adapter.GetList().GetResponse(Request, Response);
}
[HttpGet("{data}")]
public IActionResult GetRecord(string data)
{
return _adapter.GetElement(data).GetResponse(Request, Response);
}
[HttpPost]
public IActionResult Register([FromBody] PostBindingModel model)
{
return _adapter.RegisterPost(model).GetResponse(Request, Response);
}
[HttpPut]
public IActionResult ChangeInfo([FromBody] PostBindingModel model)
{
return _adapter.ChangePostInfo(model).GetResponse(Request, Response);
}
[HttpDelete("{id}")]
public IActionResult Delete(string id)
{
return _adapter.RemovePost(id).GetResponse(Request, Response);
}
[HttpPatch("{id}")]
public IActionResult Restore(string id)
{
return _adapter.RestorePost(id).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BindingModels;
namespace SladkieBulkiWedApi.Controllers;
[Authorize]
[Route("api/[controller]/[action]")]
[ApiController]
[Produces("application/json")]
public class ProductController(IProductAdapter adapter) : ControllerBase
{
private readonly IProductAdapter _adapter = adapter;
[HttpGet]
public IActionResult GetRecords()
{
return _adapter.GetList().GetResponse(Request, Response);
}
[HttpGet("{data}")]
public IActionResult GetRecord(string data)
{
// data — это либо Id, либо имя продукта
return _adapter.GetElement(data).GetResponse(Request, Response);
}
[HttpPost]
public IActionResult Register([FromBody] ProductBindingModel model)
{
return _adapter.RegisterProduct(model).GetResponse(Request, Response);
}
[HttpPut]
public IActionResult ChangeInfo([FromBody] ProductBindingModel model)
{
return _adapter.ChangeProductInfo(model).GetResponse(Request, Response);
}
[HttpDelete("{id}")]
public IActionResult Delete(string id)
{
return _adapter.DeleteProduct(id).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BindingModels;
namespace SladkieBulkiWedApi.Controllers;
[Authorize]
[Route("api/[controller]/[action]")]
[ApiController]
[Produces("application/json")]
public class ProductionController(IProductionAdapter adapter) : ControllerBase
{
private readonly IProductionAdapter _adapter = adapter;
/// <summary>
/// Получить список всех производств за период (или все, если не переданы даты).
/// </summary>
[HttpGet]
public IActionResult GetRecords(DateTime? fromDate = null, DateTime? toDate = null)
{
// Можно расширить: добавить фильтрацию по продукту и работнику, если нужно
return _adapter.GetList(fromDate, toDate).GetResponse(Request, Response);
}
/// <summary>
/// Получить производство по идентификатору.
/// </summary>
[HttpGet("{id}")]
public IActionResult GetRecord(string id)
{
return _adapter.GetElement(id).GetResponse(Request, Response);
}
/// <summary>
/// Зарегистрировать новое производство.
/// </summary>
[HttpPost]
public IActionResult Register([FromBody] ProductionBindingModel model)
{
return _adapter.RegisterProduction(model).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using SladkieBulkiContrakts.AdapterContracts.OperationResponses;
using SladkieBulkiContrakts.AdapterContracts;
using Microsoft.AspNetCore.Authorization;
namespace SladkieBulkiWedApi.Controllers;
[Authorize]
[Route("api/[controller]/[action]")]
[ApiController]
[Produces("application/json")]
public class SalariesController(ISalaryAdapter adapter) : ControllerBase
{
private readonly ISalaryAdapter _adapter = adapter;
[HttpGet]
public IActionResult GetRecords(DateTime fromDate, DateTime toDate)
{
return _adapter.GetListByPeriod(fromDate, toDate).GetResponse(Request, Response);
}
[HttpGet]
public IActionResult GetWorkerRecords(string id, DateTime fromDate, DateTime toDate)
{
return _adapter.GetListByPeriodByWorker(fromDate, toDate, id).GetResponse(Request, Response);
}
[HttpPost]
public IActionResult Calculate(DateTime date)
{
return _adapter.CalculateSalary(date).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace SladkieBulkiWedApi.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@@ -0,0 +1,63 @@
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BindingModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace SladkieBulkiWedApi.Controllers;
[Authorize]
[Route("api/[controller]/[action]")]
[ApiController]
[Produces("application/json")]
public class WorkersController(IWorkerAdapter adapter) : ControllerBase
{
private readonly IWorkerAdapter _adapter = adapter;
[HttpGet]
public IActionResult GetRecords(bool includeDeleted = false)
{
return _adapter.GetList(includeDeleted).GetResponse(Request, Response);
}
[HttpGet]
public IActionResult GetPostRecords(string id, bool includeDeleted = false)
{
return _adapter.GetPostList(id, includeDeleted).GetResponse(Request, Response);
}
[HttpGet]
public IActionResult GetBirthDateRecords(DateTime fromDate, DateTime toDate, bool includeDeleted = false)
{
return _adapter.GetListByBirthDate(fromDate, toDate, includeDeleted).GetResponse(Request, Response);
}
[HttpGet]
public IActionResult GetEmploymentRecords(DateTime fromDate, DateTime toDate, bool includeDeleted = false)
{
return _adapter.GetListByEmploymentDate(fromDate, toDate, includeDeleted).GetResponse(Request, Response);
}
[HttpGet("{data}")]
public IActionResult GetRecord(string data)
{
return _adapter.GetElement(data).GetResponse(Request, Response);
}
[HttpPost]
public IActionResult Register([FromBody] WorkerBindingModel model)
{
return _adapter.RegisterWorker(model).GetResponse(Request, Response);
}
[HttpPut]
public IActionResult ChangeInfo([FromBody] WorkerBindingModel model)
{
return _adapter.ChangeWorkerInfo(model).GetResponse(Request, Response);
}
[HttpDelete("{id}")]
public IActionResult Delete(string id)
{
return _adapter.RemoveWorker(id).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,13 @@
using SladkieBulkiContrakts.Infrastructure;
namespace SladkieBulkiWedApi.Infrastructure;
public class ConfigurationDatabase(IConfiguration configuration) : IConfigurationDatabase
{
private readonly Lazy<DataBaseSettings> _dataBaseSettings = new(() =>
{
return configuration.GetValue<DataBaseSettings>("DataBaseSettings") ?? throw new InvalidDataException(nameof(DataBaseSettings));
});
public string ConnectionString => _dataBaseSettings.Value.ConnectionString;
}

View File

@@ -0,0 +1,15 @@
using SladkieBulkiContrakts.Infrastructure;
namespace SladkieBulkiWedApi.Infrastructure;
public class ConfigurationSalary(IConfiguration configuration) : IConfigurationSalary
{
private readonly Lazy<SalarySettings> _salarySettings = new(() =>
{
return configuration.GetSection("SalarySettings").Get<SalarySettings>() ?? throw new InvalidDataException(nameof(SalarySettings));
});
public double ExtraSaleSum => _salarySettings.Value.ExtraSaleSum;
public int MaxParallelThreads { get; set; } = 4;
}

View File

@@ -0,0 +1,6 @@
namespace SladkieBulkiWedApi.Infrastructure;
public class DataBaseSettings
{
public required string ConnectionString { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace SladkieBulkiWedApi.Infrastructure;
public class SalarySettings
{
public double ExtraSaleSum { get; set; }
}

View File

@@ -0,0 +1,120 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.AdapterContracts;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.Infrastructure;
using SladkieBulkiContrakts.StoragesContarcts;
using SladkieBulkiDatabase;
using SladkieBulkiDatabase.Implementations;
using SladkieBulkiWedApi;
using SladkieBulkiWedApi.Adapters;
using SladkieBulkiWedApi.Infrastructure;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
using var loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger());
builder.Services.AddSingleton(loggerFactory.CreateLogger("Any"));
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateIssuer = true,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidIssuer = AuthOptions.ISSUER,
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateAudience = true,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidAudience = AuthOptions.AUDIENCE,
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateLifetime = true,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateIssuerSigningKey = true,
};
});
builder.Services.AddSingleton<IConfigurationDatabase, ConfigurationDatabase>();
builder.Services.AddSingleton<IConfigurationSalary, ConfigurationSalary>();
builder.Services.AddTransient<IIngredientBusinessLogicContract, IngredientBusinessLogicContract>();
builder.Services.AddTransient<IProductionBusinessLogicContract, ProductionBusinessLogicContract>();
builder.Services.AddTransient<IPostBusinessLogicContract, PostBusinessLogicContract>();
builder.Services.AddTransient<IProductBusinessLogicContract, ProductBusinessLogicContract>();
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
builder.Services.AddTransient<IWorkerBusinessLogicContract, WorkerBusinessLogicContract>();
builder.Services.AddTransient<SladkieBulkiDbContext>();
builder.Services.AddTransient<IIngredientStorageContract, IngredientStorageContract>();
builder.Services.AddTransient<IProductionStorageContract, ProductionStorageContract>();
builder.Services.AddTransient<IPostStorageContract, PostStorageContract>();
builder.Services.AddTransient<IProductStorageContract, ProductStorageContract>();
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
builder.Services.AddTransient<IWorkerStorageContract, WorkerStorageContract>();
builder.Services.AddTransient<IIngredientAdapter, IngredientAdapter>();
builder.Services.AddTransient<IProductionAdapter, ProductionAdapter>();
builder.Services.AddTransient<IPostAdapter, PostAdapter>();
builder.Services.AddTransient<IProductAdapter, ProductAdapter>();
builder.Services.AddTransient<IWorkerAdapter, WorkerAdapter>();
builder.Services.AddTransient<ISalaryAdapter, SalaryAdapter>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
if (app.Environment.IsProduction())
{
var dbContext = app.Services.GetRequiredService<SladkieBulkiDbContext>();
if (dbContext.Database.CanConnect())
{
dbContext.Database.EnsureCreated();
dbContext.Database.Migrate();
}
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.Map("/login/{username}", (string username) =>
{
return new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(
issuer: AuthOptions.ISSUER,
audience: AuthOptions.AUDIENCE,
claims: [new(ClaimTypes.Name, username)],
expires: DateTime.UtcNow.Add(TimeSpan.FromMinutes(2)),
signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)));
});
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59976",
"sslPort": 44311
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5115",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7204;http://localhost:5115",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj" />
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
<ProjectReference Include="..\SladkieBulkiDatabase\SladkieBulkiDatabase.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="SladkieBulkiTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@SladkieBulkiWedApi_HostAddress = http://localhost:5115
GET {{SladkieBulkiWedApi_HostAddress}}/weatherforecast/
Accept: application/json
###

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