4 Commits

Author SHA1 Message Date
user
7b86908eaf task 6 ready 2025-04-23 02:36:48 +04:00
user
0fad6bdb13 Чуть чуть начал 2025-04-21 01:28:05 +04:00
user
0eda3b21b0 Вроде всё 2025-04-19 00:52:05 +04:00
user
bb89dd1952 Почти готово 2025-04-09 01:58:17 +04:00
65 changed files with 4207 additions and 210 deletions

View File

@@ -0,0 +1,146 @@
using SquirrelBusinessLogic.OfficePackage;
using SquirrelContract.BusinessLogicContracts;
using SquirrelContract.DataModels;
using SquirrelContract.Exceptions;
using SquirrelContract.Extensions;
using SquirrelContract.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace SquirrelBusinessLogic.Implementations;
public class ReportContract(ICocktailStorageContract cocktailStorageContract, ISalaryStorageContract salaryStorageContract, ISaleStorageContract saleStorageContract,
BaseWordBuilder baseWordBuilder, BaseExcelBuilder baseExcelBuilder, BasePdfBuilder basePdfBuilder, ILogger logger) : IReportContract
{
private readonly ICocktailStorageContract _cocktailStorageContract = cocktailStorageContract;
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly BaseWordBuilder _baseWordBuilder = baseWordBuilder;
private readonly BaseExcelBuilder _baseExcelBuilder = baseExcelBuilder;
private readonly BasePdfBuilder _basePdfBuilder = basePdfBuilder;
private readonly ILogger _logger = logger;
internal static readonly string[] documentHeader = ["Название блюда", "Старая цена", "Дата"];
internal static readonly string[] tableHeader = ["Дата", "Сумма", "Скидка", "Товар", "Кол-во"];
public Task<List<CocktailAndCocktailHistoryDataModel>> GetDataCocktailsHistoryAsync(CancellationToken ct)
{
_logger.LogInformation("Get data PostHistory");
return GetCocktailsHistoriesAsync(ct);
}
public async Task<Stream> CreateDocumentCocktailsHistoryAsync(CancellationToken ct)
{
_logger.LogInformation("Create report CocktailHistory");
var data = await GetCocktailsHistoriesAsync(ct) ?? throw new InvalidOperationException("No found data");
return _baseWordBuilder
.AddHeader("История коктейлей")
.AddParagraph($"Сформировано на дату {DateTime.Now}")
.AddTable(
[3000, 3000, 3000],
[.. new List<string[]>() { documentHeader }
.Union(data.SelectMany(x =>
(new List<string[]>() { new[] { x.CocktailName, "", "" } })
.Union(x.Histories.Zip(x.Data, (price, date) => new[] { "", price, date }))
).ToList())
])
.Build();
}
private async Task<List<CocktailAndCocktailHistoryDataModel>> GetCocktailsHistoriesAsync(CancellationToken ct) =>
[.. (await _cocktailStorageContract.GetHistoriesListAsync(ct)).GroupBy(x => x.CocktailName).Select(x => new CocktailAndCocktailHistoryDataModel {
CocktailName = x.Key, Histories = [.. x.Select(y => y.OldPrice.ToString())], Data = [.. x.Select(y => y.ChangeDate.ToString())] })];
public async Task<Stream> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
string[] tableHeader1 = ["Сотрудник", "Дата", "Сумма", "Скидка", "Товар", "Кол-во"];
_logger.LogInformation("Create report SalesByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
var data = await GetDataBySalesAsync(dateStart, dateFinish, ct) ?? throw new InvalidOperationException("No found data");
var tableRows = new List<string[]>
{
tableHeader1
};
foreach (var sale in data)
{
tableRows.Add(new string[]
{
sale.EmployeeFIO ?? "",
sale.SaleDate.ToShortDateString(),
sale.Sum.ToString("N2"),
sale.Discount.ToString("N2"),
"", ""
});
foreach (var cocktail in sale.Cocktails ?? Enumerable.Empty<SaleCocktailDataModel>())
{
tableRows.Add(new string[]
{
"", "", "", "", cocktail.CocktailName, cocktail.Count.ToString("N2")
});
}
}
tableRows.Add(new string[]
{
"Всего", "", data.Sum(x => x.Sum).ToString("N2"), data.Sum(x => x.Discount).ToString("N2"), "", ""
});
return _baseExcelBuilder
.AddHeader("Продажи за период", 0, 6)
.AddParagraph($"с {dateStart.ToShortDateString()} по {dateFinish.ToShortDateString()}", 2)
.AddTable([15, 15, 10, 10, 25, 10], tableRows)
.Build();
}
public async Task<List<SaleDataModel>> GetDataBySalesAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
if (dateStart.IsDateNotOlder(dateFinish))
{
throw new IncorrectDatesException(dateStart, dateFinish);
}
return [.. (await _saleStorageContract.GetListAsync(dateStart,
dateFinish, ct)).OrderBy(x => x.SaleDate)];
}
public Task<List<SaleDataModel>> GetDataSaleByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Get data SalesByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
return GetDataBySalesAsync(dateStart, dateFinish, ct);
}
public Task<List<EmployeeSalaryByPeriodDataModel>> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Get data SalaryByPeriod from {dateStart} to { dateFinish}", dateStart, dateFinish);
return GetDataBySalaryAsync(dateStart, dateFinish, ct);
}
private async Task<List<EmployeeSalaryByPeriodDataModel>> GetDataBySalaryAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
if (dateStart.IsDateNotOlder(dateFinish))
{
throw new IncorrectDatesException(dateStart, dateFinish);
}
return [.. (await _salaryStorageContract.GetListAsync(dateStart, dateFinish, ct))
.GroupBy(x => x.EmployeeId)
.Select(x => new EmployeeSalaryByPeriodDataModel {
EmployeeFIO = x.First().EmployeeFIO,
TotalSalary = x.Sum(y => y.Salary),
FromPeriod = x.Min(y => y.SalaryDate),
ToPeriod = x.Max(y => y.SalaryDate)
})
.OrderBy(x => x.EmployeeFIO)];
}
public async Task<Stream> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
_logger.LogInformation("Create report SalaryByPeriod from {dateStart} to {dateFinish}", dateStart, dateFinish);
var data = await GetDataBySalaryAsync(dateStart, dateFinish, ct) ?? throw new InvalidOperationException("No found data");
return _basePdfBuilder
.AddHeader("Зарплатная ведомость")
.AddParagraph($"за период с {dateStart.ToShortDateString()} по {dateFinish.ToShortDateString()}")
.AddPieChart("Начисления", [.. data.Select(x => (x.EmployeeFIO, x.TotalSalary))])
.Build();
}
}

View File

@@ -1,20 +1,26 @@
using BarBelochkaContract.DataModels;
using Microsoft.Extensions.Logging;
using SquirrelContract.BusinessLogicContracts;
using SquirrelContract.DataModels;
using SquirrelContract.Exceptions;
using SquirrelContract.Extensions;
using SquirrelContract.Infastructure;
using SquirrelContract.Infastructure.PostConfigurations;
using SquirrelContract.StoragesContracts;
namespace SquirrelBusinessLogic.Implementations;
public class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,
ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract, ILogger logger) : ISalaryBusinessLogicContract
ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract, ILogger logger, IConfigurationSalary сonfiguration) : ISalaryBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract;
private readonly IConfigurationSalary _salaryConfiguration = сonfiguration;
private readonly Lock _lockObject = new();
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
@@ -51,13 +57,67 @@ public class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageCon
var employees = _employeeStorageContract.GetList() ?? throw new NullListException();
foreach (var employee in employees)
{
var sales = _saleStorageContract.GetList(startDate, finishDate, employeeId: employee.Id)?.Sum(x => x.Sum) ??
throw new NullListException();
var post = _postStorageContract.GetElementById(employee.PostId) ??
throw new NullListException();
var salary = post.Salary + sales * 0.1;
var sales = _saleStorageContract.GetList(startDate, finishDate, employeeId: employee.Id) ?? throw new NullListException();
var post = _postStorageContract.GetElementById(employee.PostId) ?? throw new NullListException();
var salary = post.ConfigurationModel switch
{
null => 0,
BartenderPostConfiguration cpc => CalculateSalaryForBartender(sales, startDate, finishDate, cpc),
ManagerPostConfiguration spc => CalculateSalaryForManager(startDate, finishDate, spc),
PostConfiguration pc => pc.Rate,
};
_logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, DateTime.SpecifyKind(finishDate, DateTimeKind.Utc), salary));
_salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, finishDate, salary));
}
}
private double CalculateSalaryForBartender(List<SaleDataModel> sales, DateTime startDate, DateTime finishDate, BartenderPostConfiguration config)
{
var tasks = new List<Task>();
var calcPercent = 0.0;
for (var date = startDate; date < finishDate; date = date.AddDays(1))
{
tasks.Add(Task.Factory.StartNew((object? obj) =>
{
var dateInTask = (DateTime)obj!;
var salesInDay = sales.Where(x => x.SaleDate >= dateInTask && x.SaleDate <= dateInTask.AddDays(1)).ToArray();
if (salesInDay.Length > 0)
{
lock (_lockObject)
{
calcPercent += (salesInDay.Sum(x => x.Sum) / salesInDay.Length) * config.SalePercent;
}
}
}, date));
}
var calcBonusTask = Task.Run(() =>
{
return sales.Where(x => x.Sum > _salaryConfiguration.ExtraSaleSum).Sum(x => x.Sum) * config.BonusForExtraSales;
});
try
{
Task.WaitAll([Task.WhenAll(tasks), calcBonusTask]);
}
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 CalculateSalaryForManager(DateTime startDate, DateTime finishDate, ManagerPostConfiguration config)
{
try
{
return config.Rate + config.PersonalCountTrendPremium * _employeeStorageContract.GetEmployeeTrend(startDate, finishDate);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in the supervisor payroll process");
return 0;
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -12,7 +12,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,13 @@
using SquirrelContract.AdapterContracts.OperationResponses;
namespace SquirrelContract.AdapterContracts;
public interface IReportAdapter
{
Task<ReportOperationResponse> GetDataCocktailsHistoryAsync(CancellationToken ct);
Task<ReportOperationResponse> GetDataBySalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<ReportOperationResponse> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<ReportOperationResponse> CreateDocumentCocktailsHistoryAsync(CancellationToken ct);
Task<ReportOperationResponse> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<ReportOperationResponse> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
}

View File

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

View File

@@ -10,5 +10,5 @@ public class PostBindingModel
public string? PostType { get; set; }
public double Salary { get; set; }
public string? ConfigurationJson { get; set; }
}

View File

@@ -0,0 +1,13 @@
using SquirrelContract.DataModels;
namespace SquirrelContract.BusinessLogicContracts;
public interface IReportContract
{
Task<List<CocktailAndCocktailHistoryDataModel>> GetDataCocktailsHistoryAsync(CancellationToken ct);
Task<Stream> CreateDocumentCocktailsHistoryAsync(CancellationToken ct);
Task<List<SaleDataModel>> GetDataBySalesAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<Stream> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<List<EmployeeSalaryByPeriodDataModel>> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
Task<Stream> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct);
}

View File

@@ -0,0 +1,8 @@
namespace SquirrelContract.DataModels;
public class CocktailAndCocktailHistoryDataModel
{
public required string CocktailName { get; set; }
public required List<string> Histories { get; set; }
public required List<string> Data { get; set; }
}

View File

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

View File

@@ -1,11 +1,14 @@
using SquirrelContract.Enums;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using SquirrelContract.Enums;
using SquirrelContract.Exceptions;
using SquirrelContract.Extensions;
using SquirrelContract.Infastructure;
using SquirrelContract.Infastructure.PostConfigurations;
namespace SquirrelContract.DataModels;
public class PostDataModel(string postId, string postName, PostType postType, double salary) : IValidation
public class PostDataModel(string postId, string postName, PostType postType, PostConfiguration configuration) : IValidation
{
public string Id { get; private set; } = postId;
@@ -13,7 +16,21 @@ public class PostDataModel(string postId, string postName, PostType postType, do
public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
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(BartenderPostConfiguration) => JsonConvert.DeserializeObject<BartenderPostConfiguration>(configurationJson)!,
nameof(ManagerPostConfiguration) => JsonConvert.DeserializeObject<ManagerPostConfiguration>(configurationJson)!,
_ => JsonConvert.DeserializeObject<PostConfiguration>(configurationJson)!,
};
}
}
public void Validate()
{
@@ -29,7 +46,9 @@ 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

@@ -0,0 +1,7 @@
namespace SquirrelContract.Infastructure;
public interface IConfigurationSalary
{
double ExtraSaleSum { get; }
int MaxConcurrentThreads { get; }
}

View File

@@ -10,28 +10,40 @@ public class OperationResponse
protected object? Result { get; set; }
public IActionResult GetResponse(HttpRequest request, HttpResponse response)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(response);
protected string? FileName { get; set; }
response.StatusCode = (int)StatusCode;
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);
}
if (Result is Stream stream)
{
return new FileStreamResult(stream, "application/octetstream")
{
FileDownloadName = FileName
};
}
return new ObjectResult(Result);
}
protected static TResult OK<TResult, TData>(TData data) where TResult :
OperationResponse, new() => new() { StatusCode = HttpStatusCode.OK, Result = data };
if (Result is null)
{
return new StatusCodeResult((int)StatusCode);
}
protected static TResult OK<TResult, TData>(TData data, string fileName) where TResult : OperationResponse, new()
=> new() { StatusCode = HttpStatusCode.OK, Result = data, FileName = fileName };
return new ObjectResult(Result);
}
protected static TResult NoContent<TResult>() where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.NoContent };
protected static TResult OK<TResult, TData>(TData data) where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.OK, Result = data };
protected static TResult BadRequest<TResult>(string? errorMessage = null)
where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.BadRequest, Result = errorMessage };
protected static TResult NoContent<TResult>() where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.NoContent };
protected static TResult NotFound<TResult>(string? errorMessage = null)
where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.NotFound, Result = errorMessage };
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 };
}
protected static TResult InternalServerError<TResult>(string? errorMessage = null)
where TResult : OperationResponse, new() => new() { StatusCode = HttpStatusCode.InternalServerError, Result = errorMessage };
}

View File

@@ -0,0 +1,17 @@
using Microsoft.Extensions.Configuration;
namespace SquirrelContract.Infastructure.PostConfigurations;
public class BartenderPostConfiguration : PostConfiguration
{
public override string Type => nameof(BartenderPostConfiguration);
public double SalePercent { get; set; }
public double BonusForExtraSales { get; set; }
public BartenderPostConfiguration(IConfiguration? config = null)
{
if (config != null)
{
config.GetSection("BartenderSettings");
}
}
}

View File

@@ -0,0 +1,7 @@
namespace SquirrelContract.Infastructure.PostConfigurations;
public class ManagerPostConfiguration : PostConfiguration
{
public override string Type => nameof(ManagerPostConfiguration);
public double PersonalCountTrendPremium { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace SquirrelContract.Infastructure.PostConfigurations;
public class PostConfiguration
{
public virtual string Type => nameof(PostConfiguration);
public double Rate { get; set; }
}

View File

@@ -6,6 +6,7 @@ public interface ICocktailStorageContract
{
List<CocktailDataModel> GetList();
List<CocktailHistoryDataModel> GetHistoryByCocktailId(string cocktailId);
Task<List<CocktailHistoryDataModel>> GetHistoriesListAsync(CancellationToken ct);
CocktailDataModel? GetElementById(string id);
CocktailDataModel? GetElementByName(string name);
void AddElement(CocktailDataModel cocktailDataModel);

View File

@@ -17,4 +17,6 @@ public interface IEmployeeStorageContract
void UpdElement(EmployeeDataModel employeeDataModel);
void DelElement(string id);
int GetEmployeeTrend(DateTime fromPeriod, DateTime toPeriod);
}

View File

@@ -5,6 +5,6 @@ namespace SquirrelContract.StoragesContracts;
public interface ISalaryStorageContract
{
List<SalaryDataModel> GetList(DateTime? startDate, DateTime? endDate, string? employeeId = null);
Task<List<SalaryDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct);
void AddElement(SalaryDataModel salaryDataModel);
}

View File

@@ -6,6 +6,8 @@ public interface ISaleStorageContract
{
List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeeId = null, string? clientId = null, string? cocktailId = null);
Task<List<SaleDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct);
SaleDataModel? GetElementById(string id);
void AddElement(SaleDataModel saleDataModel);

View File

@@ -0,0 +1,8 @@
namespace SquirrelContract.ViewModels;
public class CocktailAndCocktailHistoryViewModel
{
public required string CocktailName { get; set; }
public required List<string> Histories { get; set; }
public required List<string> Data { get; set; }
}

View File

@@ -0,0 +1,9 @@
namespace SquirrelContract.ViewModels;
public class EmployeeSalaryByPeriodViewModel
{
public required string EmployeeFIO { get; set; }
public double TotalSalary { get; set; }
public DateTime FromPeriod { get; set; }
public DateTime ToPeriod { get; set; }
}

View File

@@ -8,5 +8,5 @@ public class PostViewModel
public required string PostType { get; set; }
public double Salary { get; set; }
public required string Configuration { get; set; }
}

View File

@@ -0,0 +1,8 @@
using SquirrelContract.Infastructure;
namespace SquirrelDatabase;
class DefaultConfigurationDatabase : IConfigurationDatabase
{
public string ConnectionString => "";
}

View File

@@ -20,7 +20,19 @@ public class CocktailStorageContract : ICocktailStorageContract
{
cfg.CreateMap<Cocktail, CocktailDataModel>();
cfg.CreateMap<CocktailDataModel, Cocktail>();
cfg.CreateMap<CocktailHistory, CocktailHistoryDataModel>();
cfg.CreateMap<CocktailHistory, CocktailHistoryDataModel>()
.ConstructUsing(src => new CocktailHistoryDataModel(
src.CocktailId.ToString(),
src.OldPrice,
src.ChangeDate,
new CocktailDataModel(
src.Cocktail.Id.ToString(),
src.Cocktail.CocktailName,
src.Cocktail.Price,
src.Cocktail.BaseAlcohol
)
));
});
_mapper = new Mapper(config);
}
@@ -51,6 +63,19 @@ public class CocktailStorageContract : ICocktailStorageContract
}
}
public async Task<List<CocktailHistoryDataModel>> GetHistoriesListAsync(CancellationToken ct)
{
try
{
return [.. await _dbContext.CocktailHistories.Include(x => x.Cocktail).Select(x => _mapper.Map<CocktailHistoryDataModel>(x)).ToListAsync(ct)];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public CocktailDataModel? GetElementById(string id)
{
try

View File

@@ -152,6 +152,21 @@ public class EmployeeStorageContract : IEmployeeStorageContract
}
}
public int GetEmployeeTrend(DateTime fromPeriod, DateTime toPeriod)
{
try
{
var countWorkersOnBegining = _dbContext.Employees.Count(x => x.EmploymentDate < fromPeriod && (!x.IsDeleted || x.DateOfDelete > fromPeriod));
var countWorkersOnEnding = _dbContext.Employees.Count(x => x.EmploymentDate < toPeriod && (!x.IsDeleted || x.DateOfDelete > toPeriod));
return countWorkersOnEnding - countWorkersOnBegining;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
private Employee? GetEmployeeById(string id) => AddPost(_dbContext.Employees.FirstOrDefault(x => x.Id == id && !x.IsDeleted));
private IQueryable<Employee> JoinPost(IQueryable<Employee> query)

View File

@@ -24,7 +24,9 @@ public 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);
}

View File

@@ -46,6 +46,22 @@ public class SalaryStorageContract : ISalaryStorageContract
}
}
public async Task<List<SalaryDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct)
{
try
{
return [.. await _dbContext.Salaries.Include(x =>
x.Employee).Where(x => x.SalaryDate >= DateTime.SpecifyKind(startDate, DateTimeKind.Utc) &&
x.SalaryDate <= DateTime.SpecifyKind(endDate, DateTimeKind.Utc))
.Select(x => _mapper.Map<SalaryDataModel>(x)).ToListAsync(ct)];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public void AddElement(SalaryDataModel salaryDataModel)
{
try

View File

@@ -65,6 +65,27 @@ public class SaleStorageContract : ISaleStorageContract
}
}
public async Task<List<SaleDataModel>> GetListAsync(DateTime startDate, DateTime endDate, CancellationToken ct)
{
try
{
return [.. await _dbContext.Sales
.Include(x => x.Employee)
.Include(x => x.Client)
.Include(x => x.SaleCocktails)!
.ThenInclude(sc => sc.Cocktail)
.Where(x => x.SaleDate >= DateTime.SpecifyKind(startDate, DateTimeKind.Utc)
&& x.SaleDate < DateTime.SpecifyKind(endDate, DateTimeKind.Utc))
.Select(x => _mapper.Map<SaleDataModel>(x))
.ToListAsync(ct)];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public SaleDataModel? GetElementById(string id)
{
try

View File

@@ -0,0 +1,330 @@
// <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 SquirrelDatabase;
#nullable disable
namespace SquirrelDatabase.Migrations
{
[DbContext(typeof(SquirrelDbContext))]
[Migration("20250407181533_FirstMigration")]
partial class FirstMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<double>("DiscountSize")
.HasColumnType("double precision");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Clients");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("BaseAlcohol")
.HasColumnType("integer");
b.Property<string>("CocktailName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailName")
.IsUnique();
b.ToTable("Cocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("CocktailId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailId");
b.ToTable("CocktailHistories");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", 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("Employees");
});
modelBuilder.Entity("SquirrelDatabase.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("SquirrelDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("EmployeeSalary")
.HasColumnType("double precision");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.HasKey("Id");
b.HasIndex("EmployeeId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("ClientId")
.HasColumnType("text");
b.Property<double>("Discount")
.HasColumnType("double precision");
b.Property<int>("DiscountType")
.HasColumnType("integer");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("EmployeeId");
b.ToTable("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("CocktailId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "CocktailId");
b.HasIndex("CocktailId");
b.ToTable("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("CocktailHistories")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
});
modelBuilder.Entity("SquirrelDatabase.Models.Salary", b =>
{
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Salaries")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.HasOne("SquirrelDatabase.Models.Client", "Client")
.WithMany("Sales")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Sales")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("SaleCocktails")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SquirrelDatabase.Models.Sale", "Sale")
.WithMany("SaleCocktails")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
b.Navigation("Sale");
});
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Navigation("CocktailHistories");
b.Navigation("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Navigation("SaleCocktails");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,252 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SquirrelDatabase.Migrations
{
/// <inheritdoc />
public partial class FirstMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Clients",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
FIO = table.Column<string>(type: "text", nullable: false),
PhoneNumber = table.Column<string>(type: "text", nullable: false),
DiscountSize = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Clients", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Cocktails",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
CocktailName = table.Column<string>(type: "text", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false),
BaseAlcohol = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Cocktails", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Employees",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
FIO = table.Column<string>(type: "text", nullable: false),
Email = 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)
},
constraints: table =>
{
table.PrimaryKey("PK_Employees", 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: "CocktailHistories",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
CocktailId = 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_CocktailHistories", x => x.Id);
table.ForeignKey(
name: "FK_CocktailHistories_Cocktails_CocktailId",
column: x => x.CocktailId,
principalTable: "Cocktails",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Salaries",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
EmployeeId = table.Column<string>(type: "text", nullable: false),
SalaryDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
EmployeeSalary = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Salaries", x => x.Id);
table.ForeignKey(
name: "FK_Salaries_Employees_EmployeeId",
column: x => x.EmployeeId,
principalTable: "Employees",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Sales",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
EmployeeId = table.Column<string>(type: "text", nullable: false),
ClientId = table.Column<string>(type: "text", nullable: true),
SaleDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
Sum = table.Column<double>(type: "double precision", nullable: false),
DiscountType = table.Column<int>(type: "integer", nullable: false),
Discount = table.Column<double>(type: "double precision", nullable: false),
IsCancel = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Sales", x => x.Id);
table.ForeignKey(
name: "FK_Sales_Clients_ClientId",
column: x => x.ClientId,
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "FK_Sales_Employees_EmployeeId",
column: x => x.EmployeeId,
principalTable: "Employees",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SaleCocktails",
columns: table => new
{
SaleId = table.Column<string>(type: "text", nullable: false),
CocktailId = table.Column<string>(type: "text", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SaleCocktails", x => new { x.SaleId, x.CocktailId });
table.ForeignKey(
name: "FK_SaleCocktails_Cocktails_CocktailId",
column: x => x.CocktailId,
principalTable: "Cocktails",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_SaleCocktails_Sales_SaleId",
column: x => x.SaleId,
principalTable: "Sales",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Clients_PhoneNumber",
table: "Clients",
column: "PhoneNumber",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_CocktailHistories_CocktailId",
table: "CocktailHistories",
column: "CocktailId");
migrationBuilder.CreateIndex(
name: "IX_Cocktails_CocktailName",
table: "Cocktails",
column: "CocktailName",
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_Salaries_EmployeeId",
table: "Salaries",
column: "EmployeeId");
migrationBuilder.CreateIndex(
name: "IX_SaleCocktails_CocktailId",
table: "SaleCocktails",
column: "CocktailId");
migrationBuilder.CreateIndex(
name: "IX_Sales_ClientId",
table: "Sales",
column: "ClientId");
migrationBuilder.CreateIndex(
name: "IX_Sales_EmployeeId",
table: "Sales",
column: "EmployeeId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CocktailHistories");
migrationBuilder.DropTable(
name: "Posts");
migrationBuilder.DropTable(
name: "Salaries");
migrationBuilder.DropTable(
name: "SaleCocktails");
migrationBuilder.DropTable(
name: "Cocktails");
migrationBuilder.DropTable(
name: "Sales");
migrationBuilder.DropTable(
name: "Clients");
migrationBuilder.DropTable(
name: "Employees");
}
}
}

View File

@@ -0,0 +1,331 @@
// <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 SquirrelDatabase;
#nullable disable
namespace SquirrelDatabase.Migrations
{
[DbContext(typeof(SquirrelDbContext))]
[Migration("20250407203820_ChangeFieldsInPost")]
partial class ChangeFieldsInPost
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<double>("DiscountSize")
.HasColumnType("double precision");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Clients");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("BaseAlcohol")
.HasColumnType("integer");
b.Property<string>("CocktailName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailName")
.IsUnique();
b.ToTable("Cocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("CocktailId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailId");
b.ToTable("CocktailHistories");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", 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("Employees");
});
modelBuilder.Entity("SquirrelDatabase.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("SquirrelDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("EmployeeSalary")
.HasColumnType("double precision");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.HasKey("Id");
b.HasIndex("EmployeeId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("ClientId")
.HasColumnType("text");
b.Property<double>("Discount")
.HasColumnType("double precision");
b.Property<int>("DiscountType")
.HasColumnType("integer");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("EmployeeId");
b.ToTable("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("CocktailId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "CocktailId");
b.HasIndex("CocktailId");
b.ToTable("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("CocktailHistories")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
});
modelBuilder.Entity("SquirrelDatabase.Models.Salary", b =>
{
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Salaries")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.HasOne("SquirrelDatabase.Models.Client", "Client")
.WithMany("Sales")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Sales")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("SaleCocktails")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SquirrelDatabase.Models.Sale", "Sale")
.WithMany("SaleCocktails")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
b.Navigation("Sale");
});
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Navigation("CocktailHistories");
b.Navigation("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Navigation("SaleCocktails");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SquirrelDatabase.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,334 @@
// <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 SquirrelDatabase;
#nullable disable
namespace SquirrelDatabase.Migrations
{
[DbContext(typeof(SquirrelDbContext))]
[Migration("20250408214441_ChangeEmployee")]
partial class ChangeEmployee
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<double>("DiscountSize")
.HasColumnType("double precision");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Clients");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("BaseAlcohol")
.HasColumnType("integer");
b.Property<string>("CocktailName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailName")
.IsUnique();
b.ToTable("Cocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("CocktailId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailId");
b.ToTable("CocktailHistories");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", 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("Employees");
});
modelBuilder.Entity("SquirrelDatabase.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("SquirrelDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("EmployeeSalary")
.HasColumnType("double precision");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.HasKey("Id");
b.HasIndex("EmployeeId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("ClientId")
.HasColumnType("text");
b.Property<double>("Discount")
.HasColumnType("double precision");
b.Property<int>("DiscountType")
.HasColumnType("integer");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("EmployeeId");
b.ToTable("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("CocktailId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "CocktailId");
b.HasIndex("CocktailId");
b.ToTable("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("CocktailHistories")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
});
modelBuilder.Entity("SquirrelDatabase.Models.Salary", b =>
{
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Salaries")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.HasOne("SquirrelDatabase.Models.Client", "Client")
.WithMany("Sales")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Sales")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("SaleCocktails")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SquirrelDatabase.Models.Sale", "Sale")
.WithMany("SaleCocktails")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
b.Navigation("Sale");
});
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Navigation("CocktailHistories");
b.Navigation("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Navigation("SaleCocktails");
});
#pragma warning restore 612, 618
}
}
}

View File

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

View File

@@ -0,0 +1,331 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SquirrelDatabase;
#nullable disable
namespace SquirrelDatabase.Migrations
{
[DbContext(typeof(SquirrelDbContext))]
partial class SquirrelDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<double>("DiscountSize")
.HasColumnType("double precision");
b.Property<string>("FIO")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("PhoneNumber")
.IsUnique();
b.ToTable("Clients");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("BaseAlcohol")
.HasColumnType("integer");
b.Property<string>("CocktailName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailName")
.IsUnique();
b.ToTable("Cocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<DateTime>("ChangeDate")
.HasColumnType("timestamp without time zone");
b.Property<string>("CocktailId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("OldPrice")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CocktailId");
b.ToTable("CocktailHistories");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", 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("Employees");
});
modelBuilder.Entity("SquirrelDatabase.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("SquirrelDatabase.Models.Salary", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<double>("EmployeeSalary")
.HasColumnType("double precision");
b.Property<DateTime>("SalaryDate")
.HasColumnType("timestamp without time zone");
b.HasKey("Id");
b.HasIndex("EmployeeId");
b.ToTable("Salaries");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<string>("ClientId")
.HasColumnType("text");
b.Property<double>("Discount")
.HasColumnType("double precision");
b.Property<int>("DiscountType")
.HasColumnType("integer");
b.Property<string>("EmployeeId")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("IsCancel")
.HasColumnType("boolean");
b.Property<DateTime>("SaleDate")
.HasColumnType("timestamp without time zone");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("EmployeeId");
b.ToTable("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.Property<string>("SaleId")
.HasColumnType("text");
b.Property<string>("CocktailId")
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("SaleId", "CocktailId");
b.HasIndex("CocktailId");
b.ToTable("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.CocktailHistory", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("CocktailHistories")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
});
modelBuilder.Entity("SquirrelDatabase.Models.Salary", b =>
{
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Salaries")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.HasOne("SquirrelDatabase.Models.Client", "Client")
.WithMany("Sales")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("SquirrelDatabase.Models.Employee", "Employee")
.WithMany("Sales")
.HasForeignKey("EmployeeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Employee");
});
modelBuilder.Entity("SquirrelDatabase.Models.SaleCocktail", b =>
{
b.HasOne("SquirrelDatabase.Models.Cocktail", "Cocktail")
.WithMany("SaleCocktails")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SquirrelDatabase.Models.Sale", "Sale")
.WithMany("SaleCocktails")
.HasForeignKey("SaleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
b.Navigation("Sale");
});
modelBuilder.Entity("SquirrelDatabase.Models.Client", b =>
{
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Cocktail", b =>
{
b.Navigation("CocktailHistories");
b.Navigation("SaleCocktails");
});
modelBuilder.Entity("SquirrelDatabase.Models.Employee", b =>
{
b.Navigation("Salaries");
b.Navigation("Sales");
});
modelBuilder.Entity("SquirrelDatabase.Models.Sale", b =>
{
b.Navigation("SaleCocktails");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -21,6 +21,8 @@ public class Employee
public bool IsDeleted { get; set; }
public DateTime? DateOfDelete { get; set; }
[NotMapped]
public Post? Post { get; set; }

View File

@@ -1,6 +1,7 @@
using AutoMapper;
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.Infastructure.PostConfigurations;
namespace SquirrelDatabase.Models;
@@ -14,7 +15,7 @@ public class Post
public PostType PostType { get; set; }
public double Salary { get; set; }
public required PostConfiguration Configuration { get; set; }
public bool IsActual { get; set; }

View File

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

View File

@@ -9,7 +9,12 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
</ItemGroup>
@@ -17,4 +22,10 @@
<ProjectReference Include="..\SquirrelContract\SquirrelContract.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="SquirrelTests" />
<InternalVisibleTo Include="SquirrelWebApi" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
</Project>

View File

@@ -1,16 +1,27 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SquirrelContract.Infastructure;
using SquirrelContract.Infastructure.PostConfigurations;
using SquirrelDatabase.Models;
namespace SquirrelDatabase;
public class SquirrelDbContext(IConfigurationDatabase configurationDatabase) : DbContext
public class SquirrelDbContext: DbContext
{
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
private readonly IConfigurationDatabase? _configurationDatabase;
public SquirrelDbContext(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));
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o
=> o.SetPostgresVersion(12, 2));
base.OnConfiguring(optionsBuilder);
}
@@ -38,6 +49,13 @@ public class SquirrelDbContext(IConfigurationDatabase configurationDatabase) : D
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
modelBuilder.Entity<SaleCocktail>().HasKey(x => new { x.SaleId, x.CocktailId });
modelBuilder.Entity<Post>()
.Property(x => x.Configuration)
.HasColumnType("jsonb")
.HasConversion(
x => SerializePostConfiguration(x),
x => DeserialzePostConfiguration(x));
}
public DbSet<Client> Clients { get; set; }
@@ -55,4 +73,12 @@ public class SquirrelDbContext(IConfigurationDatabase configurationDatabase) : D
public DbSet<SaleCocktail> SaleCocktails { get; set; }
public DbSet<Employee> Employees { 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(BartenderPostConfiguration) => JsonConvert.DeserializeObject<BartenderPostConfiguration>(jsonString)!,
nameof(ManagerPostConfiguration) => JsonConvert.DeserializeObject<ManagerPostConfiguration>(jsonString)!,
_ => JsonConvert.DeserializeObject<PostConfiguration>(jsonString)!,
};
}

View File

@@ -4,6 +4,7 @@ using SquirrelBusinessLogic.Implementations;
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.Exceptions;
using SquirrelContract.Infastructure.PostConfigurations;
using SquirrelContract.StoragesContracts;
namespace SquirrelTests.BusinessLogicContractsTests;
@@ -33,9 +34,9 @@ internal class PostBusinessLogicContractTests
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.Manager, 10),
new(Guid.NewGuid().ToString(), "name 2", PostType.Manager, 10),
new(Guid.NewGuid().ToString(), "name 3", PostType.Manager, 10),
new(Guid.NewGuid().ToString(),"name 1", PostType.Bartender, new PostConfiguration() { Rate = 10 }),
new(Guid.NewGuid().ToString(), "name 2", PostType.Bartender, new PostConfiguration() { Rate = 10 }),
new(Guid.NewGuid().ToString(), "name 3", PostType.Bartender, new PostConfiguration() { Rate = 10 }),
};
_postStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act
@@ -89,8 +90,8 @@ internal class PostBusinessLogicContractTests
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>()
{
new(postId, "name 1", PostType.Manager, 10),
new(postId, "name 2", PostType.Manager, 10)
new(postId, "name 1", PostType.Bartender, new PostConfiguration() { Rate = 10 }),
new(postId, "name 2", PostType.Bartender, new PostConfiguration() { Rate = 10 })
};
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act
@@ -154,7 +155,7 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.Manager, 10);
var record = new PostDataModel(id, "name", PostType.Bartender, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(id);
@@ -169,7 +170,7 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Manager, 10);
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Bartender, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(postName);
@@ -225,11 +226,11 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 10);
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary;
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
});
//Act
_postBusinessLogicContract.InsertPost(record);
@@ -244,7 +245,7 @@ internal class PostBusinessLogicContractTests
//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.Manager, 10)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -260,7 +261,7 @@ internal class PostBusinessLogicContractTests
public void InsertPost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Manager, 10)), Throws.TypeOf<ValidationException>());
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
@@ -270,7 +271,7 @@ internal class PostBusinessLogicContractTests
//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.Manager, 10)), Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -279,11 +280,11 @@ internal class PostBusinessLogicContractTests
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 10);
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary;
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
});
//Act
_postBusinessLogicContract.UpdatePost(record);
@@ -298,7 +299,7 @@ internal class PostBusinessLogicContractTests
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10)), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -308,7 +309,7 @@ internal class PostBusinessLogicContractTests
//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.Manager, 10)), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
@@ -324,7 +325,7 @@ internal class PostBusinessLogicContractTests
public void UpdatePost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Manager, 10)), Throws.TypeOf<ValidationException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
@@ -334,7 +335,7 @@ internal class PostBusinessLogicContractTests
//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.Manager, 10)), Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}

View File

@@ -0,0 +1,477 @@
using SquirrelBusinessLogic.Implementations;
using SquirrelBusinessLogic.OfficePackage;
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.Exceptions;
using SquirrelContract.StoragesContracts;
using Microsoft.Extensions.Logging;
using Moq;
using BarBelochkaContract.DataModels;
using static NUnit.Framework.Internal.OSPlatform;
namespace SquirrelTests.BusinessLogicContractsTests;
[TestFixture]
internal class ReportContractTests
{
private ReportContract _reportContract;
private Mock<ISaleStorageContract> _saleStorageContract;
private Mock<ICocktailStorageContract> _cocktailStorageContract;
private Mock<ISalaryStorageContract> _salaryStorageContract;
private Mock<BaseWordBuilder> _baseWordBuilder;
private Mock<BaseExcelBuilder> _baseExcelBuilder;
private Mock<BasePdfBuilder> _basePdfBuilder;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_saleStorageContract = new Mock<ISaleStorageContract>();
_cocktailStorageContract = new Mock<ICocktailStorageContract>();
_salaryStorageContract = new Mock<ISalaryStorageContract>();
_baseWordBuilder = new Mock<BaseWordBuilder>();
_baseExcelBuilder = new Mock<BaseExcelBuilder>();
_basePdfBuilder = new Mock<BasePdfBuilder>();
_reportContract = new ReportContract(_cocktailStorageContract.Object, _salaryStorageContract.Object, _saleStorageContract.Object,
_baseWordBuilder.Object, _baseExcelBuilder.Object, _basePdfBuilder.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_saleStorageContract.Reset();
_salaryStorageContract.Reset();
_cocktailStorageContract.Reset();
}
[Test]
public async Task GetDataHistoriesByCocktail_ShouldSuccess_Test()
{
//Arrange
var cocktailId1 = Guid.NewGuid().ToString();
var cocktailId2 = Guid.NewGuid().ToString();
var cocktail1 = new CocktailDataModel(cocktailId1, "name1", 10.5, AlcoholType.Vodka);
var cocktail2 = new CocktailDataModel(cocktailId1, "name2", 10.5, AlcoholType.Vodka);
_cocktailStorageContract.Setup(x => x.GetHistoriesListAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(new List<CocktailHistoryDataModel>()
{
new(cocktailId1, 22, DateTime.UtcNow, cocktail1),
new(cocktailId2, 21, DateTime.UtcNow, cocktail2),
new(cocktailId1, 33, DateTime.UtcNow, cocktail1),
new(cocktailId1, 32, DateTime.UtcNow, cocktail1),
new(cocktailId2, 65, DateTime.UtcNow, cocktail2)
}));
//Act
var data = await _reportContract.GetDataCocktailsHistoryAsync(CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
Assert.Multiple(() =>
{
Assert.That(data.First(x => x.CocktailName == cocktail1.CocktailName).Histories, Has.Count.EqualTo(3));
Assert.That(data.First(x => x.CocktailName == cocktail2.CocktailName).Histories, Has.Count.EqualTo(2));
});
_cocktailStorageContract.Verify(x => x.GetHistoriesListAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataHistoriesByCocktail_WhenNoRecords_ShouldSuccess_Test()
{
//Arrange
_cocktailStorageContract.Setup(x =>
x.GetHistoriesListAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<CocktailHistoryDataModel>());
//Act
var data = await _reportContract.GetDataCocktailsHistoryAsync(CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(0));
_cocktailStorageContract.Verify(x =>
x.GetHistoriesListAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public void GetDataHistoriesByCocktail_WhenStorageThrowError_ShouldFail_Test()
{
//Arrange
_cocktailStorageContract.Setup(x =>
x.GetHistoriesListAsync(It.IsAny<CancellationToken>()))
.ThrowsAsync(new StorageException(new InvalidOperationException()));
//Act & Assert
Assert.That(async () => await
_reportContract.GetDataCocktailsHistoryAsync(CancellationToken.None),
Throws.TypeOf<StorageException>());
_cocktailStorageContract.Verify(x =>
x.GetHistoriesListAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task CreateDocumentHistoriesByCocktail_ShouldSuccess_Test()
{
// Arrange
var cocktailId1 = Guid.NewGuid().ToString();
var cocktailId2 = Guid.NewGuid().ToString();
var cocktail1 = new CocktailDataModel(cocktailId1, "name1", 10.5, AlcoholType.Vodka);
var cocktail2 = new CocktailDataModel(cocktailId1, "name2", 10.5, AlcoholType.Vodka);
var histories = new List<CocktailHistoryDataModel>()
{
new(cocktailId1, 22, DateTime.UtcNow, cocktail1),
new(cocktailId1, 33, DateTime.UtcNow, cocktail1),
new(cocktailId1, 32, DateTime.UtcNow, cocktail1),
new(cocktailId2, 21, DateTime.UtcNow, cocktail2),
new(cocktailId2, 65, DateTime.UtcNow, cocktail2)
};
_cocktailStorageContract.Setup(x => x.GetHistoriesListAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(histories));
_baseWordBuilder.Setup(x => x.AddHeader(It.IsAny<string>())).Returns(_baseWordBuilder.Object);
_baseWordBuilder.Setup(x => x.AddParagraph(It.IsAny<string>())).Returns(_baseWordBuilder.Object);
var countRows = 0;
string[] firstRow = [];
string[] secondRow = [];
_baseWordBuilder.Setup(x => x.AddTable(It.IsAny<int[]>(),
It.IsAny<List<string[]>>()))
.Callback((int[] widths, List<string[]> data) =>
{
countRows = data.Count;
if (data.Count > 0) firstRow = data[0];
if (data.Count > 1) secondRow = data[1];
})
.Returns(_baseWordBuilder.Object);
// Act
var data = await _reportContract.CreateDocumentCocktailsHistoryAsync(CancellationToken.None);
// Assert
_cocktailStorageContract.Verify(x => x.GetHistoriesListAsync(It.IsAny<CancellationToken>()), Times.Once);
_baseWordBuilder.Verify(x => x.AddHeader(It.IsAny<string>()), Times.Once);
_baseWordBuilder.Verify(x => x.AddParagraph(It.IsAny<string>()), Times.Once);
_baseWordBuilder.Verify(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()), Times.Once);
_baseWordBuilder.Verify(x => x.Build(), Times.Once);
Assert.Multiple(() =>
{
// 1 заголовок + 2 блюда (name1 и name2) + 5 записей истории
Assert.That(countRows, Is.EqualTo(8));
Assert.That(firstRow, Has.Length.EqualTo(3));
Assert.That(secondRow, Has.Length.EqualTo(3));
Assert.That(firstRow[0], Is.EqualTo("Название блюда"));
Assert.That(firstRow[1], Is.EqualTo("Старая цена"));
Assert.That(secondRow[0], Is.EqualTo("name1"));
Assert.That(secondRow[1], Is.EqualTo(""));
});
}
[Test]
public async Task GetDataBySalesByPeriod_ShouldSuccess_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new
List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel("",
Guid.NewGuid().ToString(), 10, 10)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DiscountType.None, false, [new SaleCocktailDataModel("",
Guid.NewGuid().ToString(), 10, 10)]),
}));
//Act
var data = await _reportContract.GetDataBySalesAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataBySalesByPeriod_WhenNoRecords_ShouldSuccess_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SaleDataModel>()));
//Act
var data = await
_reportContract.GetDataBySalesAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public void GetDataBySalesByPeriod_WhenIncorrectDates_ShouldFail_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(async () => await
_reportContract.GetDataBySalesAsync(date, date, CancellationToken.None),
Throws.TypeOf<IncorrectDatesException>());
Assert.That(async () => await
_reportContract.GetDataBySalesAsync(date, DateTime.UtcNow.AddDays(-1),
CancellationToken.None), Throws.TypeOf<IncorrectDatesException>());
}
[Test]
public void GetDataBySalesByPeriod_WhenStorageThrowError_ShouldFail_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(async () => await _reportContract.GetDataBySalesAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None),
Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task CreateDocumentSalesByPeriod_ShouldeSuccess_Test()
{
var cocktail1 = new CocktailDataModel(Guid.NewGuid().ToString(), "name 1", 10.5, AlcoholType.Vodka);
var cocktail2 = new CocktailDataModel(Guid.NewGuid().ToString(), "name 2", 10.5, AlcoholType.Vodka);
_saleStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.RegularCustomer, false, [new SaleCocktailDataModel("", cocktail1.Id, 10, 10, cocktail1), new SaleCocktailDataModel("", cocktail2.Id, 10, 10, cocktail2)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DiscountType.RegularCustomer, false, [new SaleCocktailDataModel("", cocktail2.Id, 10, 10, cocktail2)])
}));
_baseExcelBuilder.Setup(x => x.AddHeader(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).Returns(_baseExcelBuilder.Object);
_baseExcelBuilder.Setup(x => x.AddParagraph(It.IsAny<string>(), It.IsAny<int>())).Returns(_baseExcelBuilder.Object);
var countRows = 0;
string[] firstRow = [];
string[] secondRow = [];
string[] thirdRow = [];
_baseExcelBuilder.Setup(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()))
.Callback((int[] widths, List<string[]> data) =>
{
countRows = data.Count;
firstRow = data[0];
secondRow = data[1];
thirdRow = data[2];
})
.Returns(_baseExcelBuilder.Object);
//Act
var data = await _reportContract.CreateDocumentSalesByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.Multiple(() =>
{
Assert.That(countRows, Is.EqualTo(7));
Assert.That(firstRow, Is.Not.EqualTo(default));
Assert.That(secondRow, Is.Not.EqualTo(default));
});
Assert.Multiple(() =>
{
Assert.That(firstRow[0], Is.EqualTo("Сотрудник"));
Assert.That(firstRow[1], Is.EqualTo("Дата"));
Assert.That(firstRow[2], Is.EqualTo("Сумма"));
Assert.That(firstRow[3], Is.EqualTo("Скидка"));
Assert.That(firstRow[4], Is.EqualTo("Товар"));
Assert.That(firstRow[5], Is.EqualTo("Кол-во"));
});
Assert.Multiple(() =>
{
Assert.That(secondRow[0], Is.Empty);
Assert.That(secondRow[1], Is.Not.Empty);
Assert.That(secondRow[2], Is.EqualTo(200.ToString("N2")));
Assert.That(secondRow[3], Is.EqualTo(100.ToString("N2")));
Assert.That(secondRow[4], Is.Empty);
Assert.That(secondRow[5], Is.Empty);
});
Assert.Multiple(() =>
{
Assert.That(thirdRow[0], Is.Empty);
Assert.That(thirdRow[1], Is.Empty);
Assert.That(thirdRow[2], Is.Empty);
Assert.That(thirdRow[3], Is.Empty);
Assert.That(thirdRow[4], Is.EqualTo(cocktail1.CocktailName));
Assert.That(thirdRow[5], Is.EqualTo(10.ToString("N2")));
});
_saleStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
_baseExcelBuilder.Verify(x => x.AddHeader(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Once);
_baseExcelBuilder.Verify(x => x.AddParagraph(It.IsAny<string>(), It.IsAny<int>()), Times.Once);
_baseExcelBuilder.Verify(x => x.AddTable(It.IsAny<int[]>(), It.IsAny<List<string[]>>()), Times.Once);
_baseExcelBuilder.Verify(x => x.Build(), Times.Once);
}
[Test]
public async Task GetDataSalaryByPeriod_ShouldSuccess_Test()
{
// Arrange
var startDate = DateTime.UtcNow.AddDays(-20);
var endDate = DateTime.UtcNow.AddDays(5);
var employee1 = new EmployeeDataModel(
Guid.NewGuid().ToString(),
"fio 1",
"abc@gmail.com",
Guid.NewGuid().ToString(),
DateTime.UtcNow.AddYears(-20),
DateTime.UtcNow.AddDays(-3));
var employee2 = new EmployeeDataModel(
Guid.NewGuid().ToString(),
"fio 2",
"abc@gmail.com",
Guid.NewGuid().ToString(),
DateTime.UtcNow.AddYears(-20),
DateTime.UtcNow.AddDays(-3));
_salaryStorageContract.Setup(x =>
x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<SalaryDataModel>()
{
new(employee1.Id, DateTime.UtcNow.AddDays(-10), 100),
new(employee1.Id, endDate, 1000),
new(employee1.Id, startDate, 1000),
new(employee2.Id, DateTime.UtcNow.AddDays(-10), 100),
new(employee2.Id, DateTime.UtcNow.AddDays(-5), 200)
});
// Act
var data = await _reportContract.GetDataSalaryByPeriodAsync(
DateTime.UtcNow.AddDays(-1),
DateTime.UtcNow,
CancellationToken.None);
// Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
var totalSalary = data.First();
Assert.Multiple(() =>
{
Assert.That(totalSalary, Is.Not.Null);
Assert.That(totalSalary.TotalSalary, Is.EqualTo(2100));
});
_salaryStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(),
It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetDataSalaryByPeriod_WhenNoRecords_ShouldSuccess_Test()
{
// Arrange
_salaryStorageContract.Setup(x =>
x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<SalaryDataModel>());
// Act
var data = await _reportContract.GetDataSalaryByPeriodAsync(
DateTime.UtcNow.AddDays(-1),
DateTime.UtcNow,
CancellationToken.None);
// Assert
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x =>
x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()),
Times.Once);
}
[Test]
public void GetDataSalaryByPeriod_WhenIncorrectDates_ShouldFail_Test()
{
// Arrange
var date = DateTime.UtcNow;
// Act & Assert
Assert.That(async () => await _reportContract.GetDataSalaryByPeriodAsync(
date, date, CancellationToken.None),
Throws.TypeOf<IncorrectDatesException>());
Assert.That(async () => await _reportContract.GetDataSalaryByPeriodAsync(
date, DateTime.UtcNow.AddDays(-1), CancellationToken.None),
Throws.TypeOf<IncorrectDatesException>());
}
[Test]
public void GetDataBySalaryByPeriod_WhenStorageThrowError_ShouldFail_Test()
{
// Arrange
_salaryStorageContract.Setup(x =>
x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(async () => await _reportContract.GetDataSalaryByPeriodAsync(
DateTime.UtcNow.AddDays(-1),
DateTime.UtcNow,
CancellationToken.None),
Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x =>
x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()),
Times.Once);
}
[Test]
public async Task CreateDocumentSalaryByPeriod_ShouldeSuccess_Test()
{
//Arrange
var startDate = DateTime.UtcNow.AddDays(-20);
var endDate = DateTime.UtcNow.AddDays(5);
var employee1 = new EmployeeDataModel(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow.AddYears(-20), DateTime.UtcNow.AddDays(-3));
var employee2 = new EmployeeDataModel(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow.AddYears(-20), DateTime.UtcNow.AddDays(-3));
_salaryStorageContract.Setup(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(new List<SalaryDataModel>()
{
new(employee1.Id, DateTime.UtcNow.AddDays(-10), 100, employee1),
new(employee1.Id, endDate, 1000, employee1),
new(employee1.Id, startDate, 1000, employee1),
new(employee2.Id, DateTime.UtcNow.AddDays(-10), 100, employee2),
new(employee2.Id, DateTime.UtcNow.AddDays(-5), 200, employee2)
}));
_basePdfBuilder.Setup(x => x.AddHeader(It.IsAny<string>())).Returns(_basePdfBuilder.Object);
_basePdfBuilder.Setup(x => x.AddParagraph(It.IsAny<string>())).Returns(_basePdfBuilder.Object);
var countRows = 0;
(string, double) firstRow = default;
(string, double) secondRow = default;
_basePdfBuilder.Setup(x => x.AddPieChart(It.IsAny<string>(), It.IsAny<List<(string, double)>>()))
.Callback((string header, List<(string, double)> data) =>
{
countRows = data.Count;
firstRow = data[0];
secondRow = data[1];
})
.Returns(_basePdfBuilder.Object);
//Act
var data = await _reportContract.CreateDocumentSalaryByPeriodAsync(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow, CancellationToken.None);
//Assert
Assert.Multiple(() =>
{
Assert.That(countRows, Is.EqualTo(2));
Assert.That(firstRow, Is.Not.EqualTo(default));
Assert.That(secondRow, Is.Not.EqualTo(default));
});
Assert.Multiple(() =>
{
Assert.That(firstRow.Item1, Is.EqualTo(employee1.FIO));
Assert.That(firstRow.Item2, Is.EqualTo(2100));
Assert.That(secondRow.Item1, Is.EqualTo(employee2.FIO));
Assert.That(secondRow.Item2, Is.EqualTo(300));
});
_salaryStorageContract.Verify(x => x.GetListAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()), Times.Once);
_basePdfBuilder.Verify(x => x.AddHeader(It.IsAny<string>()), Times.Once);
_basePdfBuilder.Verify(x => x.AddParagraph(It.IsAny<string>()), Times.Once);
_basePdfBuilder.Verify(x => x.AddPieChart(It.IsAny<string>(), It.IsAny<List<(string, double)>>()), Times.Once);
_basePdfBuilder.Verify(x => x.Build(), Times.Once);
}
}

View File

@@ -5,6 +5,8 @@ using SquirrelBusinessLogic.Implementations;
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.Exceptions;
using SquirrelContract.Infastructure.PostConfigurations;
using SquirrelContract.Infrastructure;
using SquirrelContract.StoragesContracts;
namespace SquirrelTests.BusinessLogicContractsTests;
@@ -17,6 +19,7 @@ internal class SalaryBusinessLogicContractTests
private Mock<ISaleStorageContract> _saleStorageContract;
private Mock<IPostStorageContract> _postStorageContract;
private Mock<IEmployeeStorageContract> _employeeStorageContract;
private readonly ConfigurationSalaryTest _salaryConfigurationTest = new();
[OneTimeSetUp]
public void OneTimeSetUp()
@@ -26,7 +29,7 @@ internal class SalaryBusinessLogicContractTests
_postStorageContract = new Mock<IPostStorageContract>();
_employeeStorageContract = new Mock<IEmployeeStorageContract>();
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object,
_saleStorageContract.Object, _postStorageContract.Object, _employeeStorageContract.Object, new Mock<ILogger>().Object);
_saleStorageContract.Object, _postStorageContract.Object, _employeeStorageContract.Object, new Mock<ILogger>().Object, _salaryConfigurationTest);
}
[TearDown]
@@ -187,16 +190,14 @@ internal class SalaryBusinessLogicContractTests
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
var saleSum = 1.2 * 5;
var postSalary = 2000.0;
var rate = 2000.0;
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, postSalary));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = rate }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = postSalary + saleSum * 0.1;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
@@ -205,9 +206,10 @@ internal class SalaryBusinessLogicContractTests
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
Assert.That(sum, Is.EqualTo(rate));
}
[Test]
public void CalculateSalaryByMounth_WithSeveralEmployees_Test()
{
@@ -216,9 +218,9 @@ internal class SalaryBusinessLogicContractTests
var employee2Id = Guid.NewGuid().ToString();
var employee3Id = Guid.NewGuid().ToString();
var list = new List<EmployeeDataModel>() {
new(employee1Id, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
new(employee2Id, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
new(employee3Id, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)
new(employee1Id, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
new(employee2Id, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
new(employee3Id, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employee1Id, null, DiscountType.None, false, []),
@@ -227,7 +229,7 @@ internal class SalaryBusinessLogicContractTests
new SaleDataModel(Guid.NewGuid().ToString(), employee3Id, null, DiscountType.None, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), employee3Id, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.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
@@ -240,16 +242,15 @@ internal class SalaryBusinessLogicContractTests
public void CalculateSalaryByMounth_WithoutSalesByEmployee_Test()
{
//Arrange
var postSalary = 2000.0;
var rate = 2000.0;
var employeeId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, postSalary));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = rate }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = postSalary;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
@@ -258,7 +259,7 @@ internal class SalaryBusinessLogicContractTests
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
Assert.That(sum, Is.EqualTo(rate));
}
[Test]
@@ -267,9 +268,9 @@ internal class SalaryBusinessLogicContractTests
//Arrange
var employeeId = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
@@ -295,7 +296,7 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 100 }));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
@@ -308,9 +309,9 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), 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.Manager, 2000));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
@@ -325,7 +326,7 @@ internal class SalaryBusinessLogicContractTests
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
@@ -338,10 +339,72 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000));
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.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
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
[Test]
public void CalculateSalaryByMountht_WithBartenderPostConfiguration_CalculateSalary_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
var rate = 2000.0;
var percent = 0.1;
var bonus = 0.5;
var sales = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]),
new(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)]),
new(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5000, 12)])
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(sales);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new BartenderPostConfiguration() { Rate = rate, SalePercent = percent, BonusForExtraSales = bonus }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = rate + percent * (sales.Sum(x => x.Sum) / sales.Count) + sales.Where(x => x.Sum > _salaryConfigurationTest.ExtraSaleSum).Sum(x => x.Sum) * bonus;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMountht_WithManagerPostConfiguration_CalculateSalary_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
var rate = 2000.0;
var trend = 3;
var bonus = 100;
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleCocktailDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new ManagerPostConfiguration() { Rate = rate, PersonalCountTrendPremium = bonus }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "abc@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
_employeeStorageContract.Setup(x => x.GetEmployeeTrend(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
.Returns(trend);
var sum = 0.0;
var expectedSum = rate + trend * bonus;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
}

View File

@@ -1,6 +1,7 @@
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.Exceptions;
using SquirrelContract.Infastructure.PostConfigurations;
namespace SquirrelTests.DataModelsTests;
@@ -10,41 +11,49 @@ internal class PostDataModelTests
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, "name", PostType.Manager, 10);
var post = CreateDataModel(null, "name", PostType.Bartender, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "name", PostType.Manager, 10);
post = CreateDataModel(string.Empty, "name", PostType.Bartender, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", "name", PostType.Manager, 10);
var post = CreateDataModel("id", "name", PostType.Bartender, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostNameIsEmptyTest()
{
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Manager, 10);
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Bartender, new PostConfiguration() { Rate = 10 });
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Manager, 10);
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Bartender, 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);
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.Manager, 0);
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, null);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, -10);
}
[Test]
public void RateIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = 0 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Bartender, new PostConfiguration() { Rate = -10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
@@ -53,19 +62,20 @@ internal class PostDataModelTests
{
var postId = Guid.NewGuid().ToString();
var postName = "name";
var postType = PostType.Manager;
var salary = 10;
var post = CreateDataModel(postId, postName, postType, salary);
var postType = PostType.Bartender;
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.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) =>
new(id, postName, postType, salary);
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, PostConfiguration configuration) =>
new(id, postName, postType, configuration);
}

View File

@@ -0,0 +1,10 @@
using SquirrelContract.Infastructure;
namespace SquirrelContract.Infrastructure;
class ConfigurationSalaryTest : IConfigurationSalary
{
public double ExtraSaleSum => 10;
public int MaxConcurrentThreads => 4;
}

View File

@@ -2,6 +2,7 @@
using SquirrelDatabase;
using SquirrelDatabase.Models;
using Microsoft.EntityFrameworkCore;
using SquirrelContract.Infastructure.PostConfigurations;
namespace SquirrelTests.Infrastructure;
@@ -15,15 +16,15 @@ internal static class SquirrelDbContextExtensions
return client;
}
public static Post InsertPostToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Manager, double salary = 10, bool isActual = true, DateTime? changeDate = null)
{
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
dbContext.Posts.Add(post);
dbContext.SaveChanges();
return post;
}
public static Post InsertPostToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Bartender, 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 Cocktail InsertCocktailToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string cocktailName = "test", AlcoholType baseAlcohol = AlcoholType.Vodka, double price = 1)
public static Cocktail InsertCocktailToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string cocktailName = "test", AlcoholType baseAlcohol = AlcoholType.Vodka, double price = 1)
{
var cocktail = new Cocktail() { Id = id ?? Guid.NewGuid().ToString(), CocktailName = cocktailName, BaseAlcohol = baseAlcohol, Price = price };
dbContext.Cocktails.Add(cocktail);
@@ -62,15 +63,15 @@ internal static class SquirrelDbContextExtensions
return sale;
}
public static Employee InsertEmployeeToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string fio = "test", string email = "abc@gmail.com", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
{
var employee = new Employee() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
dbContext.Employees.Add(employee);
dbContext.SaveChanges();
return employee;
}
public static Employee InsertEmployeeToDatabaseAndReturn(this SquirrelDbContext dbContext, string? id = null, string fio = "test", string email = "abc@gmail.com", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false, DateTime? dateDelete = null)
{
var worker = new Employee() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted, DateOfDelete = dateDelete };
dbContext.Employees.Add(worker);
dbContext.SaveChanges();
return worker;
}
public static Client? GetClientFromDatabase(this SquirrelDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
public static Client? GetClientFromDatabase(this SquirrelDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id);
public static Post? GetPostFromDatabaseByPostId(this SquirrelDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual);

View File

@@ -13,12 +13,12 @@ namespace SquirrelTests.StorageContracts;
[TestFixture]
internal class EmployeeStorageContractTests : BaseStorageContractTest
{
private EmployeeStorageContract _employeeStorageContract;
private EmployeeStorageContract _workerStorageContract;
[SetUp]
public void SetUp()
{
_employeeStorageContract = new EmployeeStorageContract(SquirrelDbContext);
_workerStorageContract = new EmployeeStorageContract(SquirrelDbContext);
}
[TearDown]
@@ -33,7 +33,7 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com");
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com");
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
var list = _employeeStorageContract.GetList();
var list = _workerStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(3));
AssertElement(list.First(x => x.Id == employee.Id), employee);
@@ -42,7 +42,7 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
[Test]
public void Try_GetList_WhenNoRecords_Test()
{
var list = _employeeStorageContract.GetList();
var list = _workerStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.Empty);
}
@@ -51,10 +51,10 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
public void Try_GetList_ByPostId_Test()
{
var postId = Guid.NewGuid().ToString();
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
var list = _employeeStorageContract.GetList(postId: postId);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: postId);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: postId);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3");
var list = _workerStorageContract.GetList(postId: postId);
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(list.All(x => x.PostId == postId));
@@ -67,7 +67,7 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-21));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-19));
var list = _employeeStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
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));
}
@@ -79,7 +79,7 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-1));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(1));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(2));
var list = _employeeStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
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,11 +88,11 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
public void Try_GetList_ByAllParameters_Test()
{
var postId = Guid.NewGuid().ToString();
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
var list = _employeeStorageContract.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));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", postId: postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "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));
}
@@ -101,33 +101,33 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
public void Try_GetElementById_WhenHaveRecord_Test()
{
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_employeeStorageContract.GetElementById(employee.Id), employee);
AssertElement(_workerStorageContract.GetElementById(employee.Id), employee);
}
[Test]
public void Try_GetElementById_WhenNoRecord_Test()
{
Assert.That(() => _employeeStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
Assert.That(() => _workerStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
}
[Test]
public void Try_GetElementByFIO_WhenHaveRecord_Test()
{
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_employeeStorageContract.GetElementByFIO(employee.FIO), employee);
AssertElement(_workerStorageContract.GetElementByFIO(employee.FIO), employee);
}
[Test]
public void Try_GetElementByFIO_WhenNoRecord_Test()
{
Assert.That(() => _employeeStorageContract.GetElementByFIO("New Fio"), Is.Null);
Assert.That(() => _workerStorageContract.GetElementByFIO("New Fio"), Is.Null);
}
[Test]
public void Try_AddElement_Test()
{
var employee = CreateModel(Guid.NewGuid().ToString());
_employeeStorageContract.AddElement(employee);
_workerStorageContract.AddElement(employee);
AssertElement(SquirrelDbContext.GetEmployeeFromDatabaseById(employee.Id), employee);
}
@@ -136,7 +136,7 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
{
var employee = CreateModel(Guid.NewGuid().ToString());
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employee.Id);
Assert.That(() => _employeeStorageContract.AddElement(employee), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _workerStorageContract.AddElement(employee), Throws.TypeOf<ElementExistsException>());
}
[Test]
@@ -144,14 +144,14 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
{
var employee = CreateModel(Guid.NewGuid().ToString(), "New Fio");
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employee.Id);
_employeeStorageContract.UpdElement(employee);
_workerStorageContract.UpdElement(employee);
AssertElement(SquirrelDbContext.GetEmployeeFromDatabaseById(employee.Id), employee);
}
[Test]
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _employeeStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _workerStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
@@ -159,14 +159,14 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
{
var employee = CreateModel(Guid.NewGuid().ToString());
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
Assert.That(() => _employeeStorageContract.UpdElement(employee), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _workerStorageContract.UpdElement(employee), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_DelElement_Test()
{
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
_employeeStorageContract.DelElement(employee.Id);
_workerStorageContract.DelElement(employee.Id);
var element = SquirrelDbContext.GetEmployeeFromDatabaseById(employee.Id);
Assert.That(element, Is.Not.Null);
Assert.That(element.IsDeleted);
@@ -175,7 +175,7 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
[Test]
public void Try_DelElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _employeeStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _workerStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
@@ -183,7 +183,43 @@ internal class EmployeeStorageContractTests : BaseStorageContractTest
{
var employee = CreateModel(Guid.NewGuid().ToString());
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
Assert.That(() => _employeeStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _workerStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_GetEmployeeTrend_WhenNoNewAndDeletedEmployees_Test()
{
var startDate = DateTime.UtcNow.AddDays(-5);
var endDate = DateTime.UtcNow.AddDays(-3);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-9));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-1));
var count = _workerStorageContract.GetEmployeeTrend(startDate, endDate);
Assert.That(count, Is.EqualTo(0));
}
[Test]
public void Try_GetEmployeeTrend_WhenHaveNewAndNoDeletedEmployees_Test()
{
var startDate = DateTime.UtcNow.AddDays(-5);
var endDate = DateTime.UtcNow.AddDays(-3);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-4));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-1));
var count = _workerStorageContract.GetEmployeeTrend(startDate, endDate);
Assert.That(count, Is.EqualTo(1));
}
[Test]
public void Try_GetEmployeeTrend_WhenNoNewAndHaveDeletedEmployees_Test()
{
var startDate = DateTime.UtcNow.AddDays(-5);
var endDate = DateTime.UtcNow.AddDays(-3);
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-9));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-4));
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(employmentDate: DateTime.UtcNow.AddDays(-10), isDeleted: true, dateDelete: DateTime.UtcNow.AddDays(-1));
var count = _workerStorageContract.GetEmployeeTrend(startDate, endDate);
Assert.That(count, Is.EqualTo(-1));
}
private static void AssertElement(EmployeeDataModel? actual, Employee expected)
@@ -216,6 +252,7 @@ internal class EmployeeStorageContractTests : 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

@@ -2,6 +2,7 @@
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.Exceptions;
using SquirrelContract.Infastructure.PostConfigurations;
using SquirrelDatabase.Implementations;
using SquirrelDatabase.Models;
using SquirrelTests.Infrastructure;
@@ -46,6 +47,19 @@ internal class PostStorageContractTests : BaseStorageContractTest
Assert.That(list, Is.Empty);
}
[Test]
public void Try_GetList_WhenDifferentConfigTypes_Test()
{
var postSimple = SquirrelDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
var postBuilder = SquirrelDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new BartenderPostConfiguration() { SalePercent = 500 });
var postLoader = SquirrelDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new ManagerPostConfiguration() { PersonalCountTrendPremium = 20 });
var list = _postStorageContract.GetList();
Assert.That(list, Is.Not.Null);
AssertElement(list.First(x => x.Id == postSimple.PostId), postSimple);
AssertElement(list.First(x => x.Id == postBuilder.PostId), postBuilder);
AssertElement(list.First(x => x.Id == postLoader.PostId), postLoader);
}
[Test]
public void Try_GetPostWithHistory_WhenHaveRecords_Test()
{
@@ -143,6 +157,36 @@ internal class PostStorageContractTests : BaseStorageContractTest
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_AddElement_WithBartenderPostConfiguration_Test()
{
var salePercent = 10;
var post = CreateModel(Guid.NewGuid().ToString(), config: new BartenderPostConfiguration() { SalePercent = salePercent });
_postStorageContract.AddElement(post);
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(post.Id);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(BartenderPostConfiguration).Name));
Assert.That((element.Configuration as BartenderPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
});
}
[Test]
public void Try_AddElement_WithLoaderPostConfiguration_Test()
{
var trendPremium = 20;
var post = CreateModel(Guid.NewGuid().ToString(), config: new ManagerPostConfiguration() { PersonalCountTrendPremium = trendPremium });
_postStorageContract.AddElement(post);
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(post.Id);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManagerPostConfiguration).Name));
Assert.That((element.Configuration as ManagerPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test]
public void Try_UpdElement_Test()
{
@@ -179,6 +223,36 @@ internal class PostStorageContractTests : BaseStorageContractTest
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementDeletedException>());
}
[Test]
public void Try_UpdElement_WithBartenderPostConfiguration_Test()
{
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn();
var salePercent = 10;
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new BartenderPostConfiguration() { SalePercent = salePercent }));
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(post.PostId);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(BartenderPostConfiguration).Name));
Assert.That((element.Configuration as BartenderPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
});
}
[Test]
public void Try_UpdElement_WithLoaderPostConfiguration_Test()
{
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn();
var trendPremium = 20;
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new ManagerPostConfiguration() { PersonalCountTrendPremium = trendPremium }));
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(post.PostId);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManagerPostConfiguration).Name));
Assert.That((element.Configuration as ManagerPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test]
public void Try_DelElement_Test()
{
@@ -234,12 +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.ConfigurationModel.Rate, Is.EqualTo(expected.Configuration.Rate));
});
}
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.Manager, double salary = 10)
=> new(postId, postName, postType, salary);
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.Bartender, PostConfiguration? config = null)
=> new(postId, postName, postType, config ?? new PostConfiguration() { Rate = 100 });
private static void AssertElement(Post? actual, PostDataModel expected)
{
@@ -249,7 +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.Configuration.Rate, Is.EqualTo(expected.ConfigurationModel.Rate));
});
}
}

View File

@@ -1,9 +1,12 @@
using SquirrelContract.BindingModels;
using SquirrelContract.Enums;
using SquirrelContract.Infastructure.PostConfigurations;
using SquirrelContract.ViewModels;
using SquirrelDatabase.Models;
using SquirrelTests.Infrastructure;
using System.Net;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace SquirrelTests.WebApiControllersTests;
@@ -36,6 +39,24 @@ internal class PostControllerTests : BaseWebApiControllerTest
AssertElement(data.First(x => x.Id == post.PostId), post);
}
[Test]
public async Task GetRecords_WhenDifferentConfigTypes_ShouldSuccess_Test()
{
//Arrange
var postSimple = SquirrelDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
var postBartender = SquirrelDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new BartenderPostConfiguration() { SalePercent = 500 });
var postManager = SquirrelDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new ManagerPostConfiguration() { PersonalCountTrendPremium = 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 == postBartender.PostId), postBartender);
AssertElement(data.First(x => x.Id == postManager.PostId), postManager);
}
[Test]
public async Task GetHistory_WhenHaveRecords_ShouldSuccess_Test()
{
@@ -195,10 +216,10 @@ internal class PostControllerTests : BaseWebApiControllerTest
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
{
//Arrange
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manager.ToString(), Salary = 10 };
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = 10 };
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 };
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = -10 };
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Bartender.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Bartender.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.Bartender.ToString(), ConfigurationJson = null };
//Act
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
@@ -232,6 +253,44 @@ internal class PostControllerTests : BaseWebApiControllerTest
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Post_WithBartenderPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var salePercent = 10;
var postModel = CreateModel(configuration: JsonSerializer.Serialize(new BartenderPostConfiguration() { 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 = SquirrelDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(BartenderPostConfiguration).Name));
Assert.That((element.Configuration as BartenderPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
});
}
[Test]
public async Task Post_WithManagerPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var trendPremium = 20;
var postModel = CreateModel(configuration: JsonSerializer.Serialize(new ManagerPostConfiguration() { PersonalCountTrendPremium = trendPremium, Rate = 10 }));
//Act
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManagerPostConfiguration).Name));
Assert.That((element.Configuration as ManagerPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test]
public async Task Put_ShouldSuccess_Test()
{
@@ -287,10 +346,10 @@ internal class PostControllerTests : BaseWebApiControllerTest
public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test()
{
//Arrange
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manager.ToString(), Salary = 10 };
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = 10 };
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 };
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = -10 };
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Bartender.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Bartender.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.Bartender.ToString(), ConfigurationJson = null };
//Act
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
@@ -324,6 +383,48 @@ internal class PostControllerTests : BaseWebApiControllerTest
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Put_WithBartenderPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var salePercent = 10;
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn();
var postModel = CreateModel(post.PostId, configuration: JsonSerializer.Serialize(new BartenderPostConfiguration() { SalePercent = salePercent, Rate = 10 }));
//Act
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
SquirrelDbContext.ChangeTracker.Clear();
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(BartenderPostConfiguration).Name));
Assert.That((element.Configuration as BartenderPostConfiguration)!.SalePercent, Is.EqualTo(salePercent));
});
}
[Test]
public async Task Put_WithManagerPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var trendPremium = 20;
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn();
var postModel = CreateModel(post.PostId, configuration: JsonSerializer.Serialize(new ManagerPostConfiguration() { PersonalCountTrendPremium = trendPremium, Rate = 10 }));
//Act
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
SquirrelDbContext.ChangeTracker.Clear();
var element = SquirrelDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ManagerPostConfiguration).Name));
Assert.That((element.Configuration as ManagerPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test]
public async Task Delete_ShouldSuccess_Test()
{
@@ -428,17 +529,17 @@ internal class PostControllerTests : BaseWebApiControllerTest
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(actual.Salary, Is.EqualTo(expected.Salary));
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.Manager, double salary = 10)
private static PostBindingModel CreateModel(string? postId = null, string postName = "name", PostType postType = PostType.Bartender, string? configuration = null)
=> new()
{
Id = postId ?? Guid.NewGuid().ToString(),
PostName = postName,
PostType = postType.ToString(),
Salary = salary
ConfigurationJson = configuration ?? JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 })
};
private static void AssertElement(Post? actual, PostBindingModel expected)
@@ -449,7 +550,7 @@ internal class PostControllerTests : BaseWebApiControllerTest
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.Salary, Is.EqualTo(expected.Salary));
Assert.That(actual.Configuration.Type, Is.EqualTo(JsonNode.Parse(expected.ConfigurationJson!)!["Type"]!.GetValue<string>()));
});
}
}

View File

@@ -0,0 +1,238 @@
using SquirrelContract.DataModels;
using SquirrelContract.Enums;
using SquirrelContract.ViewModels;
using SquirrelTests.Infrastructure;
using System.Net;
namespace SquirrelTests.WebApiControllersTests;
[TestFixture]
internal class ReportControllerTests : BaseWebApiControllerTest
{
[TearDown]
public void TearDown()
{
SquirrelDbContext.RemoveCocktailsFromDatabase();
SquirrelDbContext.RemoveSalesFromDatabase();
SquirrelDbContext.RemoveEmployeesFromDatabase();
SquirrelDbContext.RemovePostsFromDatabase();
SquirrelDbContext.RemoveSalariesFromDatabase();
SquirrelDbContext.RemoveClientsFromDatabase();
}
[Test]
public async Task GetHistories_WhenHaveRecords_ShouldSuccess_Test()
{
//Arrange
var cocktailId1 = Guid.NewGuid().ToString();
var cocktailId2 = Guid.NewGuid().ToString();
var cocktail1 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailId1, "name1", AlcoholType.Vodka, 10);
var cocktail2 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailId2, "name2", AlcoholType.Vodka, 10);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId1, 22, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId2, 21, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId1, 33, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId1, 32, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId2, 65, DateTime.UtcNow);
//Act
var response = await HttpClient.GetAsync("/api/report/GetHistories");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var data = await GetModelFromResponseAsync<List<CocktailHistoryViewModel>>(response);
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
}
[Test]
public async Task LoadHistories_WhenHaveRecords_ShouldSuccess_Test()
{
//Arrange
var cocktailId1 = Guid.NewGuid().ToString();
var cocktailId2 = Guid.NewGuid().ToString();
var cocktail1 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailId1, "name1", AlcoholType.Vodka, 10);
var cocktail2 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailId2, "name2", AlcoholType.Vodka, 10);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId1, 22, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId2, 21, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId1, 33, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId1, 32, DateTime.UtcNow);
SquirrelDbContext.InsertCocktailHistoryToDatabaseAndReturn(cocktailId2, 65, DateTime.UtcNow);
//Act
var response = await HttpClient.GetAsync("/api/report/LoadHistories");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
using var data = await response.Content.ReadAsStreamAsync();
Assert.That(data, Is.Not.Null);
Assert.That(data.Length, Is.GreaterThan(0));
await AssertStreamAsync(response, "file.docx");
}
[Test]
public async Task GetSales_WhenHaveRecords_ShouldSuccess_Test()
{
//Arrange
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn();
var client = SquirrelDbContext.InsertClientToDatabaseAndReturn();
var cocktail1 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailName: "name 1");
var cocktail2 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailName: "name 2");
SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id, client.Id, cocktails: [(cocktail1.Id, 10, 1.1)]);
SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id, client.Id, cocktails: [(cocktail2.Id, 10, 1.1)]);
//Act
var response = await
HttpClient.GetAsync($"/api/report/getsales?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<SaleViewModel>>(response);
Assert.That(data, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(data, Has.Count.EqualTo(2));
});
}
[Test]
public async Task GetSales_WhenDateIsIncorrect_ShouldBadRequest_Test()
{
//Act
var response = await
HttpClient.GetAsync($"/api/report/getsales?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 LoadSales_WhenHaveRecords_ShouldSuccess_Test()
{
//Arrange
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn();
var cocktail1 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailName: "name 1");
var cocktail2 = SquirrelDbContext.InsertCocktailToDatabaseAndReturn(cocktailName: "name 2");
SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id, cocktails: [(cocktail1.Id, 10, 1.1), (cocktail2.Id, 10, 1.1)]);
SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id, cocktails: [(cocktail1.Id, 10, 1.1)]);
//Act
var response = await HttpClient.GetAsync($"/api/report/loadsales?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}");
//Assert
await AssertStreamAsync(response, "file.xlsx");
}
[Test]
public async Task LoadSales_WhenDateIsIncorrect_ShouldBadRequest_Test()
{
// Act
var response = await HttpClient.GetAsync(
$"/api/report/loadsales?" +
$"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 GetSalary_WhenHaveRecords_ShouldSuccess_Test()
{
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn();
var employee1 =
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId:
post.PostId);
var employee2 =
SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId:
post.PostId);
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee1.Id,
employeeSalary: 100, salaryDate: DateTime.UtcNow.AddDays(-10));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee1.Id,
employeeSalary: 1000, salaryDate: DateTime.UtcNow.AddDays(-5));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee1.Id,
employeeSalary: 200, salaryDate: DateTime.UtcNow.AddDays(5));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee2.Id,
employeeSalary: 500, salaryDate: DateTime.UtcNow.AddDays(-5));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee2.Id,
employeeSalary: 300, salaryDate: DateTime.UtcNow.AddDays(-3));
var response = await
HttpClient.GetAsync($"/api/report/getsalary?fromDate={DateTime.UtcNow.AddDays(-7):yyyy-MM-dd}&toDate={DateTime.UtcNow.AddDays(-1):yyyy-MM-dd}");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var data = await GetModelFromResponseAsync<List<EmployeeSalaryByPeriodViewModel>>(response);
Assert.That(data, Is.Not.Null);
Assert.That(data, Has.Count.EqualTo(2));
Assert.Multiple(() =>
{
Assert.That(data.First(x => x.EmployeeFIO ==
employee1.FIO).TotalSalary, Is.EqualTo(1000));
Assert.That(data.First(x => x.EmployeeFIO ==
employee2.FIO).TotalSalary, Is.EqualTo(800));
});
}
[Test]
public async Task GetSalary_WhenDateIsIncorrect_ShouldBadRequest_Test()
{
// Act
var response = await HttpClient.GetAsync(
$"/api/report/getsalary?" +
$"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 LoadSalary_WhenHaveRecords_ShouldSuccess_Test()
{
//Arrange
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn();
var employee1 = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: post.PostId).AddPost(post);
var employee2 = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: post.PostId).AddPost(post);
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee1.Id, employeeSalary: 100, salaryDate: DateTime.UtcNow.AddDays(-10));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee1.Id, employeeSalary: 1000, salaryDate: DateTime.UtcNow.AddDays(-5));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee1.Id, employeeSalary: 200, salaryDate: DateTime.UtcNow.AddDays(5));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee2.Id, employeeSalary: 500, salaryDate: DateTime.UtcNow.AddDays(-5));
SquirrelDbContext.InsertSalaryToDatabaseAndReturn(employee2.Id, employeeSalary: 300, salaryDate: DateTime.UtcNow.AddDays(-3));
//Act
var response = await HttpClient.GetAsync($"/api/report/loadsalary?fromDate={DateTime.UtcNow.AddDays(-7):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}");
//Assert
await AssertStreamAsync(response, "file.pdf");
}
[Test]
public async Task LoadSalary_WhenDateIsIncorrect_ShouldBadRequest_Test()
{
// Act
var response = await HttpClient.GetAsync(
$"/api/report/loadsalary?" +
$"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));
}
private static async Task AssertStreamAsync(HttpResponseMessage response, string fileNameForSave = "")
{
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
using var data = await response.Content.ReadAsStreamAsync();
Assert.That(data, Is.Not.Null);
Assert.That(data.Length, Is.GreaterThan(0));
await SaveStreamAsync(data, fileNameForSave);
}
private static async Task SaveStreamAsync(Stream stream, string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
return;
}
var path = Path.Combine(Directory.GetCurrentDirectory(), fileName);
if (File.Exists(path))
{
File.Delete(path);
}
stream.Position = 0;
using var fileStream = new FileStream(path, FileMode.OpenOrCreate);
await stream.CopyToAsync(fileStream);
}
}

View File

@@ -149,27 +149,27 @@ internal class SalaryControllerTests : BaseWebApiControllerTest
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Calculate_ShouldSuccess_Test()
{
//Arrange
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn(salary: 1000);
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "Иванов И.И.", postId: post.PostId);
var sale = SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id);
//[Test]
//public async Task Calculate_ShouldSuccess_Test()
//{
// //Arrange
// var post = SquirrelDbContext.InsertPostToDatabaseAndReturn(salary: 1000);
// var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "Иванов И.И.", postId: post.PostId);
// var sale = SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id);
var expectedSum = sale.Sum * 0.1 + post.Salary;
//Act
var response = await HttpClient.PostAsync($"/api/salary/calculate?date={DateTime.UtcNow:MM/dd/yyyy}", null);
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
var salaries = SquirrelDbContext.GetSalariesFromDatabaseByEmployeeId(employee.Id);
Assert.Multiple(() =>
{
Assert.That(salaries, Has.Length.EqualTo(1));
Assert.That(salaries.First().EmployeeSalary, Is.EqualTo(expectedSum));
Assert.That(salaries.First().SalaryDate.Month, Is.EqualTo(DateTime.UtcNow.Month));
});
}
// var expectedSum = sale.Sum * 0.1 + post.Salary;
// //Act
// var response = await HttpClient.PostAsync($"/api/salary/calculate?date={DateTime.UtcNow:MM/dd/yyyy}", null);
// //Assert
// Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
// var salaries = SquirrelDbContext.GetSalariesFromDatabaseByEmployeeId(employee.Id);
// Assert.Multiple(() =>
// {
// Assert.That(salaries, Has.Length.EqualTo(1));
// Assert.That(salaries.First().EmployeeSalary, Is.EqualTo(expectedSum));
// Assert.That(salaries.First().SalaryDate.Month, Is.EqualTo(DateTime.UtcNow.Month));
// });
//}
[Test]
public async Task Calculate_WithoutEmployees_ShouldSuccess_Test()
@@ -182,33 +182,33 @@ internal class SalaryControllerTests : BaseWebApiControllerTest
Assert.That(salaries, Has.Length.EqualTo(0));
}
[Test]
public async Task Calculate_WithoutSalesByEmployee_ShouldSuccess_Test()
{
//Arrange
var post = SquirrelDbContext.InsertPostToDatabaseAndReturn(salary: 1000);
var employee1 = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "name 1", postId: post.PostId);
var employee2 = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "name 2", postId: post.PostId);
var sale = SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee1.Id);
//Act
var response = await HttpClient.PostAsync($"/api/salary/calculate?date={DateTime.UtcNow:MM/dd/yyyy}", null);
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
var salary1 = SquirrelDbContext.GetSalariesFromDatabaseByEmployeeId(employee1.Id).First().EmployeeSalary;
var salary2 = SquirrelDbContext.GetSalariesFromDatabaseByEmployeeId(employee2.Id).First().EmployeeSalary;
Assert.That(salary1, Is.Not.EqualTo(salary2));
}
//[Test]
//public async Task Calculate_WithoutSalesByEmployee_ShouldSuccess_Test()
//{
// //Arrange
// var post = SquirrelDbContext.InsertPostToDatabaseAndReturn(salary: 1000);
// var employee1 = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "name 1", postId: post.PostId);
// var employee2 = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "name 2", postId: post.PostId);
// var sale = SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee1.Id);
// //Act
// var response = await HttpClient.PostAsync($"/api/salary/calculate?date={DateTime.UtcNow:MM/dd/yyyy}", null);
// //Assert
// Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
// var salary1 = SquirrelDbContext.GetSalariesFromDatabaseByEmployeeId(employee1.Id).First().EmployeeSalary;
// var salary2 = SquirrelDbContext.GetSalariesFromDatabaseByEmployeeId(employee2.Id).First().EmployeeSalary;
// Assert.That(salary1, Is.Not.EqualTo(salary2));
//}
[Test]
public async Task Calculate_PostNotFound_ShouldNotFound_Test()
{
//Arrange
SquirrelDbContext.InsertPostToDatabaseAndReturn();
var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "name", postId: Guid.NewGuid().ToString());
var sale = SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id);
//Act
var response = await HttpClient.PostAsync($"/api/salary/calculate?date={DateTime.UtcNow:MM/dd/yyyy}", null);
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
//[Test]
//public async Task Calculate_PostNotFound_ShouldNotFound_Test()
//{
// //Arrange
// SquirrelDbContext.InsertPostToDatabaseAndReturn();
// var employee = SquirrelDbContext.InsertEmployeeToDatabaseAndReturn(fio: "name", postId: Guid.NewGuid().ToString());
// var sale = SquirrelDbContext.InsertSaleToDatabaseAndReturn(employee.Id);
// //Act
// var response = await HttpClient.PostAsync($"/api/salary/calculate?date={DateTime.UtcNow:MM/dd/yyyy}", null);
// //Assert
// Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
//}
}

View File

@@ -6,6 +6,7 @@ using SquirrelContract.BusinessLogicContracts;
using SquirrelContract.DataModels;
using SquirrelContract.Exceptions;
using SquirrelContract.ViewModels;
using System.Text.Json;
namespace SquirrelWebApi.Adapters;
@@ -17,6 +18,8 @@ public class PostAdapter : IPostAdapter
private readonly Mapper _mapper;
private readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
public PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, ILogger<PostAdapter> logger)
{
_postBusinessLogicContract = postBusinessLogicContract;
@@ -24,7 +27,8 @@ public class PostAdapter : IPostAdapter
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<PostBindingModel, PostDataModel>();
cfg.CreateMap<PostDataModel, PostViewModel>();
cfg.CreateMap<PostDataModel, PostViewModel>()
.ForMember(x => x.Configuration, x => x.MapFrom(src => JsonSerializer.Serialize(src.ConfigurationModel, JsonSerializerOptions)));
});
_mapper = new Mapper(config);
}

View File

@@ -0,0 +1,208 @@
using AutoMapper;
using SquirrelContract.AdapterContracts;
using SquirrelContract.AdapterContracts.OperationResponses;
using SquirrelContract.BusinessLogicContracts;
using SquirrelContract.DataModels;
using SquirrelContract.Exceptions;
using SquirrelContract.ViewModels;
namespace SquirrelWebApi.Adapters;
public class ReportAdapter : IReportAdapter
{
private readonly IReportContract _reportContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public ReportAdapter(IReportContract reportContract, ILogger logger)
{
_reportContract = reportContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<CocktailAndCocktailHistoryDataModel, CocktailAndCocktailHistoryViewModel>()
.ForMember(dest => dest.Histories, opt => opt.MapFrom(src => src.Histories))
.ForMember(dest => dest.Data, opt => opt.MapFrom(src => src.Data));
cfg.CreateMap<SaleDataModel, SaleViewModel>();
cfg.CreateMap<SaleCocktailDataModel, SaleCocktailViewModel>();
cfg.CreateMap<EmployeeSalaryByPeriodDataModel, EmployeeSalaryByPeriodViewModel>();
});
_mapper = new Mapper(config);
}
public async Task<ReportOperationResponse> CreateDocumentCocktailsHistoryAsync(CancellationToken ct)
{
try
{
return SendStream(await _reportContract.CreateDocumentCocktailsHistoryAsync(ct), "histories.xslx");
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "InvalidOperationException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return
ReportOperationResponse.InternalServerError(ex.Message);
}
}
public async Task<ReportOperationResponse> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
try
{
return SendStream(await _reportContract.CreateDocumentSalesByPeriodAsync(dateStart, dateFinish, ct),
"sales.xslx");
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "InvalidOperationException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return
ReportOperationResponse.InternalServerError(ex.Message);
}
}
public async Task<ReportOperationResponse> GetDataCocktailsHistoryAsync(CancellationToken ct)
{
try
{
return ReportOperationResponse.OK([.. (await _reportContract.GetDataCocktailsHistoryAsync(ct)).Select(x => _mapper.Map<CocktailAndCocktailHistoryViewModel>(x))]);
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "InvalidOperationException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return
ReportOperationResponse.InternalServerError(ex.Message);
}
}
public async Task<ReportOperationResponse> GetDataBySalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
try
{
return ReportOperationResponse.OK((await _reportContract.GetDataBySalesAsync(dateStart, dateFinish, ct)).Select(x =>
_mapper.Map<SaleViewModel>(x)).ToList());
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "InvalidOperationException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return
ReportOperationResponse.InternalServerError(ex.Message);
}
}
private static ReportOperationResponse SendStream(Stream stream, string fileName)
{
stream.Position = 0;
return ReportOperationResponse.OK(stream, fileName);
}
public async Task<ReportOperationResponse> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
try
{
return ReportOperationResponse.OK((await _reportContract.GetDataSalaryByPeriodAsync(dateStart, dateFinish, ct))
.Select(x => _mapper.Map<EmployeeSalaryByPeriodViewModel>(x)).ToList());
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "InvalidOperationException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return
ReportOperationResponse.InternalServerError(ex.Message);
}
}
public async Task<ReportOperationResponse> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
{
try
{
return SendStream(await _reportContract.CreateDocumentSalaryByPeriodAsync(dateStart, dateFinish, ct), "salary.pdf");
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message} ");
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "InvalidOperationException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return
ReportOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,60 @@
using SquirrelContract.AdapterContracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace SquirrelWebApi.Controllers;
[Authorize]
[Route("api/[controller]/[action]")]
[ApiController]
public class ReportController(IReportAdapter adapter) : ControllerBase
{
private readonly IReportAdapter _adapter = adapter;
[HttpGet]
[Consumes("application/json")]
public async Task<IActionResult> GetHistories(CancellationToken ct)
{
return (await
_adapter.GetDataCocktailsHistoryAsync(ct)).GetResponse(Request, Response);
}
[HttpGet]
[Consumes("application/octet-stream")]
public async Task<IActionResult> LoadHistories(CancellationToken cancellationToken)
{
return (await
_adapter.CreateDocumentCocktailsHistoryAsync(cancellationToken)).GetResponse(Request, Response);
}
[HttpGet]
[Consumes("application/json")]
public async Task<IActionResult> GetSales(DateTime fromDate, DateTime toDate, CancellationToken cancellationToken)
{
return (await _adapter.GetDataBySalesByPeriodAsync(fromDate, toDate, cancellationToken)).GetResponse(Request, Response);
}
[HttpGet]
[Consumes("application/octet-stream")]
public async Task<IActionResult> LoadSales(DateTime fromDate, DateTime toDate, CancellationToken cancellationToken)
{
return (await _adapter.CreateDocumentSalesByPeriodAsync(fromDate,
toDate, cancellationToken)).GetResponse(Request, Response);
}
[HttpGet]
[Consumes("application/json")]
public async Task<IActionResult> GetSalary(DateTime fromDate, DateTime toDate, CancellationToken cancellationToken)
{
return (await _adapter.GetDataSalaryByPeriodAsync(fromDate, toDate,
cancellationToken)).GetResponse(Request, Response);
}
[HttpGet]
[Consumes("application/octet-stream")]
public async Task<IActionResult> LoadSalary(DateTime fromDate, DateTime toDate, CancellationToken cancellationToken)
{
return (await _adapter.CreateDocumentSalaryByPeriodAsync(fromDate, toDate, cancellationToken)).GetResponse(Request, Response);
}
}

View File

@@ -0,0 +1,17 @@
using SquirrelContract.Infastructure;
namespace SquirrelWebApi.Infrastructure;
public class ConfigurationSalary(IConfiguration configuration) : IConfigurationSalary
{
private readonly Lazy<SalarySettings> _salarySettings = new(() =>
{
var settings = configuration.GetSection("SalarySettings").Get<SalarySettings>();
return settings ?? throw new InvalidDataException("SalarySettings configuration section is missing or invalid");
});
public double ExtraSaleSum => _salarySettings.Value.ExtraSaleSum;
public int MaxConcurrentThreads => _salarySettings.Value.MaxConcurrentThreads;
}

View File

@@ -0,0 +1,8 @@
namespace SquirrelWebApi.Infrastructure;
public class SalarySettings
{
public double ExtraSaleSum { get; set; }
public int MaxConcurrentThreads { get; set; }
}

View File

@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using SquirrelBusinessLogic.Implementations;
using SquirrelBusinessLogic.OfficePackage;
using SquirrelContract.AdapterContracts;
using SquirrelContract.BusinessLogicContracts;
using SquirrelContract.Infastructure;
@@ -48,6 +49,8 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
});
builder.Services.AddSingleton<IConfigurationDatabase, ConfigurationDatabase>();
builder.Services.AddSingleton<IConfigurationSalary, ConfigurationSalary>();
builder.Services.AddTransient<IClientBusinessLogicContract, ClientBusinessLogicContract>();
builder.Services.AddTransient<IPostBusinessLogicContract, PostBusinessLogicContract>();
@@ -71,6 +74,12 @@ builder.Services.AddTransient<ISaleAdapter, SaleAdapter>();
builder.Services.AddTransient<IEmployeeAdapter, EmployeeAdapter>();
builder.Services.AddTransient<ISalaryAdapter, SalaryAdapter>();
builder.Services.AddTransient<IReportContract, ReportContract>();
builder.Services.AddTransient<IReportAdapter, ReportAdapter>();
builder.Services.AddTransient<BaseWordBuilder, OpenXmlWordBuilder>();
builder.Services.AddTransient<BaseExcelBuilder, OpenXmlExcelBuilder>();
builder.Services.AddTransient<BasePdfBuilder, MigraDocPdfBuilder>();
builder.Services.AddOpenApi();
var app = builder.Build();

View File

@@ -9,7 +9,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.6.1" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.6.1" />
@@ -26,4 +30,10 @@
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -22,7 +22,17 @@
]
},
"AllowedHosts": "*",
//"DataBaseSettings": {
// "ConnectionString": "Host=127.0.0.1;Port=5432;Database=Squirrel;Username=postgres;Password=postgres;"
//}
"DataBaseSettings": {
"ConnectionString": "Host=127.0.0.1;Port=5432;Database=Squirrel;Username=postgres;Password=postgres;"
},
"SalarySettings": {
"ExtraSaleSum": 1000,
"MaxConcurrentThreads": 4
},
"BartenderSettings": {
"SalePercent": 0.1,
"BonusForExtraSales": 200
}
}