готовый SuppliersRepository
This commit is contained in:
parent
1e9db99ae2
commit
38ab28a71e
@ -4,10 +4,11 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Npgsql;
|
||||
|
||||
namespace ProjectGasStation.Implementations;
|
||||
|
||||
public class ConnectionString : IConnectionString
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "";
|
||||
string IConnectionString.ConnectionString => "Server=localhost;Port=5432;Database=GasStation;Username=postgres;Password=525252;";
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectGasStation.Entities;
|
||||
using ProjectGasStation.Entities.Enums;
|
||||
using ProjectGasStation.Repositories;
|
||||
using System;
|
||||
@ -6,6 +7,11 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using System.Data.SqlClient;
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
|
||||
|
||||
namespace ProjectGasStation.Implementations;
|
||||
|
||||
@ -13,32 +19,121 @@ public class EmployeeRepository : IEmployeeRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionstring;
|
||||
|
||||
public EmployeeRepository(IConnectionString connectionString)
|
||||
private readonly ILogger<EmployeeRepository> _logger;
|
||||
|
||||
public EmployeeRepository(IConnectionString connectionString, ILogger<EmployeeRepository> logger)
|
||||
{
|
||||
_connectionstring = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateEmployee(Employee employee)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
|
||||
|
||||
}
|
||||
|
||||
public void DeleteEmployee(int id)
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionstring.ConnectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryInsert = @"
|
||||
INSERT INTO Employees (Name, EmployeePost)
|
||||
VALUES (@Name, @EmployeePost)";
|
||||
|
||||
connection.Execute(queryInsert, employee);
|
||||
}
|
||||
|
||||
public Employee ReadEmployeeById(int id)
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Employee.CreateEntity(0, string.Empty, EmployeePost.None);
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
public IEnumerable<Employee> ReadEmployees()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public void UpdateEmployee(Employee employee)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionstring.ConnectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryUpdate = @"
|
||||
UPDATE Employees
|
||||
SET
|
||||
Name=@Name,
|
||||
EmployeePost=@EmployeePost
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, employee);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteEmployee(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionstring.ConnectionString);
|
||||
connection.Open();
|
||||
|
||||
var queryDelete = @"
|
||||
DELETE FROM Employees
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Employee ReadEmployeeById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionstring.ConnectionString);
|
||||
|
||||
var querySelect = @"
|
||||
SELECT * FROM Employees
|
||||
WHERE [Id]=@id";
|
||||
var employee = connection.QueryFirst<Employee>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee));
|
||||
return employee;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Employee> ReadEmployees()
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionstring.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Employees";
|
||||
var employees = connection.Query<Employee>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees));
|
||||
return employees;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,125 @@
|
||||
using ProjectGasStation.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectGasStation.Entities;
|
||||
using ProjectGasStation.Repositories;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
namespace ProjectGasStation.Implementations;
|
||||
|
||||
public class SupplierRepository : ISupplierRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<SupplierRepository> _logger;
|
||||
|
||||
public SupplierRepository(IConnectionString connectionString, ILogger<SupplierRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateSupplier(Supplier supplier)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(supplier));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Suppliers (Name, inn)
|
||||
VALUES (@Name, @inn)";
|
||||
connection.Execute(queryInsert, supplier);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteSupplier(int id)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Suppliers
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Supplier ReadSupplierById(int id)
|
||||
{
|
||||
return Supplier.CreateEntity(0, string.Empty, string.Empty);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Suppliers
|
||||
WHERE Id=@id";
|
||||
var supplier = connection.QueryFirst<Supplier>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(supplier));
|
||||
return supplier;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Supplier> ReadSuppliers()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Suppliers";
|
||||
var suppliers = connection.Query<Supplier>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(suppliers));
|
||||
return suppliers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateSupplier(Supplier supplier)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(supplier));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Suppliers
|
||||
SET
|
||||
Name=@Name,
|
||||
inn=@inn
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, supplier);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,13 @@
|
||||
using ProjectGasStation.Implementations;
|
||||
using ProjectGasStation.Repositories;
|
||||
using Unity;
|
||||
using Unity.Lifetime;
|
||||
using Serilog;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Unity.Microsoft.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
namespace ProjectGasStation
|
||||
{
|
||||
@ -22,14 +29,29 @@ namespace ProjectGasStation
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
|
||||
container.RegisterType<IProductRepository, ProductRepository>();
|
||||
container.RegisterType<IPurchaseRepository, PurchaseRepository>();
|
||||
container.RegisterType<ISaleRepository, SaleRepository>();
|
||||
container.RegisterType<ISupplierRepository, SupplierRepository>();
|
||||
container.RegisterType<IConnectionString, ConnectionString>();
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
|
||||
container.RegisterType<IEmployeeRepository, EmployeeRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IProductRepository, ProductRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IPurchaseRepository, PurchaseRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ISaleRepository, SaleRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ISupplierRepository, SupplierRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new TransientLifetimeManager());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,19 +2,27 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows7.0</TargetFramework>
|
||||
<TargetFramework>net8.0-windows8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<SupportedOSPlatformVersion>8.0</SupportedOSPlatformVersion>
|
||||
</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="8.0.5" />
|
||||
<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="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Loading…
x
Reference in New Issue
Block a user