diff --git a/ProjectGasStation/ProjectGasStation/Implementations/ConnectionString.cs b/ProjectGasStation/ProjectGasStation/Implementations/ConnectionString.cs index 6e46797..e233741 100644 --- a/ProjectGasStation/ProjectGasStation/Implementations/ConnectionString.cs +++ b/ProjectGasStation/ProjectGasStation/Implementations/ConnectionString.cs @@ -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;"; } diff --git a/ProjectGasStation/ProjectGasStation/Implementations/EmployeeRepository.cs b/ProjectGasStation/ProjectGasStation/Implementations/EmployeeRepository.cs index 138a4f0..90262af 100644 --- a/ProjectGasStation/ProjectGasStation/Implementations/EmployeeRepository.cs +++ b/ProjectGasStation/ProjectGasStation/Implementations/EmployeeRepository.cs @@ -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 _logger; + + public EmployeeRepository(IConnectionString connectionString, ILogger logger) { _connectionstring = connectionString; + _logger = logger; } public void CreateEmployee(Employee employee) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee)); + try + { + using var connection = new NpgsqlConnection(_connectionstring.ConnectionString); + connection.Open(); + + var queryInsert = @" +INSERT INTO Employees (Name, EmployeePost) +VALUES (@Name, @EmployeePost)"; + + connection.Execute(queryInsert, employee); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } + + 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) { - return Employee.CreateEntity(0, string.Empty, EmployeePost.None); + _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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee)); + return employee; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } - public IEnumerable ReadEmployees() + public IEnumerable ReadEmployees() { - return []; - } - - public void UpdateEmployee(Employee employee) - { - + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionstring.ConnectionString); + var querySelect = "SELECT * FROM Employees"; + var employees = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees)); + return employees; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectGasStation/ProjectGasStation/Implementations/SupplierRepository.cs b/ProjectGasStation/ProjectGasStation/Implementations/SupplierRepository.cs index 334659f..e0ad554 100644 --- a/ProjectGasStation/ProjectGasStation/Implementations/SupplierRepository.cs +++ b/ProjectGasStation/ProjectGasStation/Implementations/SupplierRepository.cs @@ -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 _logger; + + public SupplierRepository(IConnectionString connectionString, ILogger 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(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(supplier)); + return supplier; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadSuppliers() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Suppliers"; + var suppliers = connection.Query(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; + } } } diff --git a/ProjectGasStation/ProjectGasStation/Program.cs b/ProjectGasStation/ProjectGasStation/Program.cs index db71b73..aab910d 100644 --- a/ProjectGasStation/ProjectGasStation/Program.cs +++ b/ProjectGasStation/ProjectGasStation/Program.cs @@ -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(); - container.RegisterType(); - container.RegisterType(); - container.RegisterType(); - container.RegisterType(); - container.RegisterType(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + + + container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(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; + } } } \ No newline at end of file diff --git a/ProjectGasStation/ProjectGasStation/ProjectGasStation.csproj b/ProjectGasStation/ProjectGasStation/ProjectGasStation.csproj index 4e184fc..6aef442 100644 --- a/ProjectGasStation/ProjectGasStation/ProjectGasStation.csproj +++ b/ProjectGasStation/ProjectGasStation/ProjectGasStation.csproj @@ -2,19 +2,27 @@ WinExe - net8.0-windows7.0 + net8.0-windows8.0 enable true enable + 8.0 + + + + + + +