PIbd-21_Kudrinsky_O.S._LabWork_2 #4
@ -10,11 +10,8 @@ public class Product
|
||||
|
||||
public double Price { get; private set; }
|
||||
|
||||
public DateTime AssemblyDate { get; private set; }
|
||||
|
||||
public ProductType ProductType { get; private set; }
|
||||
|
||||
|
||||
public static Product CreateEntity(int id, string productName, double price, ProductType productType)
|
||||
{
|
||||
return new Product
|
||||
@ -22,8 +19,7 @@ public class Product
|
||||
ID = id,
|
||||
ProductName = productName ?? string.Empty,
|
||||
Price = price,
|
||||
ProductType = productType,
|
||||
AssemblyDate = DateTime.Now
|
||||
ProductType = productType
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -39,18 +39,20 @@
|
||||
//
|
||||
panel.Controls.Add(buttonAdd);
|
||||
panel.Dock = DockStyle.Right;
|
||||
panel.Location = new Point(1081, 0);
|
||||
panel.Location = new Point(582, 0);
|
||||
panel.Margin = new Padding(2, 1, 2, 1);
|
||||
panel.Name = "panel";
|
||||
panel.Size = new Size(265, 783);
|
||||
panel.Size = new Size(143, 367);
|
||||
panel.TabIndex = 0;
|
||||
//
|
||||
// buttonAdd
|
||||
//
|
||||
buttonAdd.BackgroundImage = Properties.Resources.plus;
|
||||
buttonAdd.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonAdd.Location = new Point(65, 47);
|
||||
buttonAdd.Location = new Point(35, 22);
|
||||
buttonAdd.Margin = new Padding(2, 1, 2, 1);
|
||||
buttonAdd.Name = "buttonAdd";
|
||||
buttonAdd.Size = new Size(152, 145);
|
||||
buttonAdd.Size = new Size(82, 68);
|
||||
buttonAdd.TabIndex = 0;
|
||||
buttonAdd.UseVisualStyleBackColor = true;
|
||||
buttonAdd.Click += ButtonAdd_Click;
|
||||
@ -65,22 +67,24 @@
|
||||
dataGridViewAssemblerShifts.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
dataGridViewAssemblerShifts.Dock = DockStyle.Fill;
|
||||
dataGridViewAssemblerShifts.Location = new Point(0, 0);
|
||||
dataGridViewAssemblerShifts.Margin = new Padding(2, 1, 2, 1);
|
||||
dataGridViewAssemblerShifts.MultiSelect = false;
|
||||
dataGridViewAssemblerShifts.Name = "dataGridViewAssemblerShifts";
|
||||
dataGridViewAssemblerShifts.ReadOnly = true;
|
||||
dataGridViewAssemblerShifts.RowHeadersVisible = false;
|
||||
dataGridViewAssemblerShifts.RowHeadersWidth = 82;
|
||||
dataGridViewAssemblerShifts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewAssemblerShifts.Size = new Size(1081, 783);
|
||||
dataGridViewAssemblerShifts.Size = new Size(582, 367);
|
||||
dataGridViewAssemblerShifts.TabIndex = 1;
|
||||
//
|
||||
// FormAssemblerShifts
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(13F, 32F);
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(1346, 783);
|
||||
ClientSize = new Size(725, 367);
|
||||
Controls.Add(dataGridViewAssemblerShifts);
|
||||
Controls.Add(panel);
|
||||
Margin = new Padding(2, 1, 2, 1);
|
||||
Name = "FormAssemblerShifts";
|
||||
Text = "Выходы на смену";
|
||||
Load += FormAssemblerShifts_Load;
|
||||
|
@ -41,18 +41,20 @@
|
||||
panel.Controls.Add(buttonDel);
|
||||
panel.Controls.Add(buttonAdd);
|
||||
panel.Dock = DockStyle.Right;
|
||||
panel.Location = new Point(1062, 0);
|
||||
panel.Location = new Point(572, 0);
|
||||
panel.Margin = new Padding(2, 1, 2, 1);
|
||||
panel.Name = "panel";
|
||||
panel.Size = new Size(279, 798);
|
||||
panel.Size = new Size(150, 374);
|
||||
panel.TabIndex = 0;
|
||||
//
|
||||
// buttonDel
|
||||
//
|
||||
buttonDel.BackgroundImage = Properties.Resources.minus;
|
||||
buttonDel.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonDel.Location = new Point(67, 305);
|
||||
buttonDel.Location = new Point(36, 143);
|
||||
buttonDel.Margin = new Padding(2, 1, 2, 1);
|
||||
buttonDel.Name = "buttonDel";
|
||||
buttonDel.Size = new Size(150, 151);
|
||||
buttonDel.Size = new Size(81, 71);
|
||||
buttonDel.TabIndex = 1;
|
||||
buttonDel.UseVisualStyleBackColor = true;
|
||||
buttonDel.Click += ButtonDel_Click;
|
||||
@ -61,9 +63,10 @@
|
||||
//
|
||||
buttonAdd.BackgroundImage = Properties.Resources.plus;
|
||||
buttonAdd.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonAdd.Location = new Point(67, 40);
|
||||
buttonAdd.Location = new Point(36, 19);
|
||||
buttonAdd.Margin = new Padding(2, 1, 2, 1);
|
||||
buttonAdd.Name = "buttonAdd";
|
||||
buttonAdd.Size = new Size(150, 151);
|
||||
buttonAdd.Size = new Size(81, 71);
|
||||
buttonAdd.TabIndex = 0;
|
||||
buttonAdd.UseVisualStyleBackColor = true;
|
||||
buttonAdd.Click += ButtonAdd_Click;
|
||||
@ -78,22 +81,24 @@
|
||||
dataGridViewAssemblies.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
dataGridViewAssemblies.Dock = DockStyle.Fill;
|
||||
dataGridViewAssemblies.Location = new Point(0, 0);
|
||||
dataGridViewAssemblies.Margin = new Padding(2, 1, 2, 1);
|
||||
dataGridViewAssemblies.MultiSelect = false;
|
||||
dataGridViewAssemblies.Name = "dataGridViewAssemblies";
|
||||
dataGridViewAssemblies.ReadOnly = true;
|
||||
dataGridViewAssemblies.RowHeadersVisible = false;
|
||||
dataGridViewAssemblies.RowHeadersWidth = 82;
|
||||
dataGridViewAssemblies.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewAssemblies.Size = new Size(1062, 798);
|
||||
dataGridViewAssemblies.Size = new Size(572, 374);
|
||||
dataGridViewAssemblies.TabIndex = 1;
|
||||
//
|
||||
// FormAssemblies
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(13F, 32F);
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(1341, 798);
|
||||
ClientSize = new Size(722, 374);
|
||||
Controls.Add(dataGridViewAssemblies);
|
||||
Controls.Add(panel);
|
||||
Margin = new Padding(2, 1, 2, 1);
|
||||
Name = "FormAssemblies";
|
||||
Text = "Сборки";
|
||||
Load += FormAssemblies_Load;
|
||||
|
@ -52,7 +52,7 @@
|
||||
//
|
||||
buttonDel.BackgroundImage = Properties.Resources.minus;
|
||||
buttonDel.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonDel.Location = new Point(93, 551);
|
||||
buttonDel.Location = new Point(93, 542);
|
||||
buttonDel.Name = "buttonDel";
|
||||
buttonDel.Size = new Size(150, 151);
|
||||
buttonDel.TabIndex = 2;
|
||||
@ -63,7 +63,7 @@
|
||||
//
|
||||
buttonUpd.BackgroundImage = Properties.Resources.pencil;
|
||||
buttonUpd.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonUpd.Location = new Point(93, 295);
|
||||
buttonUpd.Location = new Point(93, 294);
|
||||
buttonUpd.Name = "buttonUpd";
|
||||
buttonUpd.Size = new Size(150, 151);
|
||||
buttonUpd.TabIndex = 1;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using ProjectWorkshop.Repositories;
|
||||
using ProjectWorkshop.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectWorkshop.Repositories.Implementations;
|
||||
using ProjectWorkshop.Repositories;
|
||||
using Unity;
|
||||
using ProjectWorkshop.Repositories.Implementation;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace ProjectWorkshop
|
||||
{
|
||||
@ -22,6 +27,8 @@ namespace ProjectWorkshop
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IAssemblerRepository, AssemblerRepository>();
|
||||
container.RegisterType<IAssemblerShiftRepository, AssemblerShiftRepository>();
|
||||
container.RegisterType<IAssemblyRepository, AssemblyRepository>();
|
||||
@ -29,7 +36,21 @@ namespace ProjectWorkshop
|
||||
container.RegisterType<IProductRepository, ProductRepository>();
|
||||
container.RegisterType<IShiftRepository, ShiftRepository>();
|
||||
|
||||
container.RegisterType<IConnectionString, ConnectionString>();
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private static LoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build())
|
||||
.CreateLogger());
|
||||
return loggerFactory;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,18 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.2" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -31,4 +42,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,7 @@
|
||||
namespace ProjectWorkshop.Repositories;
|
||||
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
using ProjectWorkshop.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectWorkshop.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,25 +13,123 @@ namespace ProjectWorkshop.Repositories.Implementations;
|
||||
|
||||
public class AssemblerRepository : IAssemblerRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<AssemblerRepository> _logger;
|
||||
|
||||
public AssemblerRepository(IConnectionString connectionString, ILogger<AssemblerRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateAssembler(Assembler assembler)
|
||||
{
|
||||
}
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembler));
|
||||
|
||||
public void DeleteAssembler(int id)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Assembler (FullName, AssemblerRank, WorkExperience)
|
||||
VALUES (@FullName, @AssemblerRank, @WorkExperience)";
|
||||
connection.Execute(queryInsert, assembler);
|
||||
|
||||
public Assembler ReadAssemblerByID(int id)
|
||||
{
|
||||
return Assembler.CreateEntity(0, string.Empty, Entities.Enums.AssemblerRank.None, DateTime.Now);
|
||||
}
|
||||
|
||||
public IEnumerable<Assembler> ReadAssemblers()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateAssembler(Assembler assembler)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembler));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Assembler
|
||||
SET
|
||||
FullName=@FullName,
|
||||
AssemblerRank=@AssemblerRank,
|
||||
WorkExperience=@WorkExperience
|
||||
WHERE ID=@id";
|
||||
|
||||
connection.Execute(queryUpdate, assembler);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAssembler(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Assembler
|
||||
WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Assembler ReadAssemblerByID(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Assembler
|
||||
WHERE ID=@id";
|
||||
var assembler = connection.QueryFirst<Assembler>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(assembler));
|
||||
return assembler;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Assembler> ReadAssemblers()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Assembler";
|
||||
var assemblers = connection.Query<Assembler>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(assemblers));
|
||||
return assemblers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectWorkshop.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectWorkshop.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,12 +13,49 @@ namespace ProjectWorkshop.Repositories.Implementations;
|
||||
|
||||
public class AssemblerShiftRepository : IAssemblerShiftRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<AssemblerShiftRepository> _logger;
|
||||
public AssemblerShiftRepository(IConnectionString connectionString, ILogger<AssemblerShiftRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateAssemblerShift(AssemblerShift assemblerShift)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assemblerShift));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO AssemblerShift (WorkHours, AssemblerID_Assembler, ShiftID_Shift)
|
||||
VALUES (@WorkHours, @AssemblerID_Assembler, @ShiftID_Shift)";
|
||||
connection.Execute(queryInsert, assemblerShift);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<AssemblerShift> ReadAssemblerShifts(int? workHours = null, int? assemblerID = null, int? shiftID = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM AssemblerShift";
|
||||
var assemblerShifts = connection.Query<AssemblerShift>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(assemblerShifts));
|
||||
return assemblerShifts;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectWorkshop.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectWorkshop.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,16 +13,111 @@ namespace ProjectWorkshop.Repositories.Implementations;
|
||||
|
||||
public class AssemblyRepository : IAssemblyRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<AssemblyRepository> _logger;
|
||||
|
||||
public AssemblyRepository(IConnectionString connectionString, ILogger<AssemblyRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateAssembly(Assembly assembly)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembly));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
|
||||
var queryInsert = @"
|
||||
INSERT INTO Assembly (Count, AssemblerID_Assembler)
|
||||
VALUES (@Count, @AssemblerID_Assembler)
|
||||
RETURNING ID";
|
||||
var assemblyId = connection.QueryFirst<int>(queryInsert, new
|
||||
{
|
||||
assembly.Count,
|
||||
assembly.AssemblerID_Assembler
|
||||
}, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO ProductAssembly (ProductID_Product, AssemblyID_Assembly, Count)
|
||||
VALUES (@ProductID_Product, @AssemblyID_Assembly, @Count)";
|
||||
|
||||
foreach (var elem in assembly.ProductAssembly)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
AssemblyID_Assembly = assemblyId,
|
||||
elem.ProductID_Product,
|
||||
elem.Count
|
||||
}, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAssembly(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Assembly
|
||||
WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Assembly> ReadAssemblies(int? productID = null, int? assemblyID = null, int? count = null)
|
||||
public IEnumerable<Assembly> ReadAssemblies(int? id = null, int? assemblyID = null, int? count = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Assembly";
|
||||
var assemblies = connection.Query<Assembly>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(assemblies));
|
||||
return assemblies;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Assembler> GetAssemblers()
|
||||
{
|
||||
_logger.LogInformation("Получение всех сборщиков");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var query = "SELECT ID, Name FROM Assembler";
|
||||
var assemblers = connection.Query<Assembler>(query);
|
||||
_logger.LogDebug("Полученные сборщики: {json}", JsonConvert.SerializeObject(assemblers));
|
||||
return assemblers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при получении сборщиков");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectWorkshop.Repositories.Implementation;
|
||||
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Database=otp;Username=postgres;Password=admin";
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
using ProjectWorkshop.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectWorkshop.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -7,13 +11,55 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectWorkshop.Repositories.Implementations;
|
||||
|
||||
internal class ProductAssemblyRepository : IProductAssemblyRepository
|
||||
public class ProductAssemblyRepository : IProductAssemblyRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<AssemblerRepository> _logger;
|
||||
|
||||
public ProductAssemblyRepository(IConnectionString connectionString, ILogger<AssemblerRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateProductAssembly(ProductAssembly productAssembly)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(productAssembly));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Assembly (Count, ProductType)
|
||||
VALUES (@Count, @ProductType)
|
||||
RETURNING ID";
|
||||
connection.Execute(queryInsert, productAssembly);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteProductAssembly(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Assembly
|
||||
WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectWorkshop.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectWorkshop.Entities;
|
||||
using ProjectWorkshop.Entities.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -10,25 +14,116 @@ namespace ProjectWorkshop.Repositories.Implementations;
|
||||
|
||||
public class ProductRepository : IProductRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<ProductRepository> _logger;
|
||||
|
||||
public ProductRepository(IConnectionString connectionString, ILogger<ProductRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateProduct(Product product)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Product (ProductName, Price, ProductType)
|
||||
VALUES (@ProductName, @Price, @ProductType)
|
||||
RETURNING ID";
|
||||
connection.Execute(queryInsert, product);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void UpdateProduct(Product product)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Product
|
||||
SET ProductName=@ProductName, Price=@Price, ProductType=@ProductType
|
||||
WHERE ID=@ID";
|
||||
connection.Execute(queryUpdate, product);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteProduct(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Product
|
||||
WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Product ReadProductByID(int id)
|
||||
{
|
||||
return Product.CreateEntity(0, string.Empty, 0, ProductType.None);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Product
|
||||
WHERE ID=@id";
|
||||
var product = connection.QueryFirst<Product>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(product));
|
||||
return product;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Product> ReadProducts()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public void UpdateProduct(Product product)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Product";
|
||||
var products = connection.Query<Product>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(products));
|
||||
return products;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ProjectWorkshop.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectWorkshop.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -7,27 +11,112 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectWorkshop.Repositories.Implementations;
|
||||
|
||||
internal class ShiftRepository : IShiftRepository
|
||||
public class ShiftRepository : IShiftRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<ShiftRepository> _logger;
|
||||
public ShiftRepository(IConnectionString connectionString, ILogger<ShiftRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateShift(Shift shift)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(shift));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Shift (ShiftDate)
|
||||
VALUES (@ShiftDate)";
|
||||
connection.Execute(queryInsert, shift);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void UpdateShift(Shift shift)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(shift));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Shift
|
||||
SET ShiftDate=@ShiftDate
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, shift);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteShift(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Shift
|
||||
WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Shift ReadShiftByID(int id)
|
||||
{
|
||||
return Shift.CreateEntity(0, DateTime.Now);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Shift
|
||||
WHERE ID=@id";
|
||||
var shift = connection.QueryFirst<Shift>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(shift));
|
||||
return shift;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Shift> ReadShifts()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public void UpdateShift(Shift shift)
|
||||
{
|
||||
_logger.LogInformation("Получение всех смен");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT ID, ShiftDate FROM Shift";
|
||||
return connection.Query<Shift>(querySelect);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при получении смен");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
ProjectWorkshop/ProjectWorkshop/appsettings.json
Normal file
15
ProjectWorkshop/ProjectWorkshop/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user