diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs index aed0daa..10a71d3 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs @@ -3,9 +3,10 @@ [Flags] public enum BusColors { - White = 0, - Yellow = 1, - Black = 2, - Blue = 4, - Green = 8 + None = 0, + White = 1, + Yellow = 2, + Black = 4, + Blue = 8, + Green = 16 } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs index f39a68b..dcf4aac 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs @@ -7,20 +7,20 @@ public class Itinerary { public int Id { get; private set; } public int BusId { get; private set; } - public IEnumerable ItineraryRoute { get; private set; } = []; + public IEnumerable ItineraryRoutes { get; private set; } = []; public int DriverId { get; private set; } public int ConductorId { get; private set; } public DateTime ItineraryDate { get; private set; } public string Description { get; private set; } = string.Empty; public static Itinerary CreateOperation(int id, int busId, int driverId, int conductorId, DateTime itineraryDate, string description, - IEnumerable itineraryRoute) + IEnumerable itineraryRoutes) { return new Itinerary { Id = id, BusId = busId, - ItineraryRoute = itineraryRoute, + ItineraryRoutes = itineraryRoutes, DriverId = driverId, ConductorId = conductorId, ItineraryDate = itineraryDate, diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs index eb71e7f..e8b8e86 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs @@ -70,6 +70,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 3; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs index a55c9e3..56bdd3d 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs @@ -96,6 +96,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 2; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs index 112e9c5..e13357e 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs @@ -96,6 +96,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 1; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs index 339e587..81b956b 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs @@ -14,6 +14,7 @@ public partial class FormEmployees : Form _employeeRepository = employeeRepository ?? throw new ArgumentNullException(nameof(employeeRepository)); } + private void FormEmployees_Load(object sender, EventArgs e) { try diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs index 5d64ac1..45d6d9b 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs @@ -83,6 +83,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 3; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs index 9d776cc..2d22552 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs @@ -52,6 +52,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(681, 411); dataGridViewData.TabIndex = 0; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj b/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj index 1de707e..ebed082 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj +++ b/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj @@ -13,6 +13,8 @@ + + diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs index e17307b..f8facd2 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs @@ -1,14 +1,15 @@ -using Microsoft.Extensions.Logging; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; using ProjectAutoenterprise.Entities; - +using System.Data.SqlClient; namespace ProjectAutoenterprise.Repositories.Implementations; internal class BusRepairRepository : IBusRepairRepository { private readonly IConnectionString _connectionString; - private readonly ILogger _logger; - public BusRepairRepository(IConnectionString connectionString, ILogger logger) { _connectionString = connectionString; @@ -17,10 +18,39 @@ internal class BusRepairRepository : IBusRepairRepository public void CreateBusRepair(BusRepair busRepair) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(busRepair)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO BusRepairs (BusId, RepairDate, Description) +VALUES (@BusId, @RepairDate, @Description)"; + connection.Execute(queryInsert, busRepair); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public IEnumerable ReadBusRepairs(DateTime? dateForm = null, DateTime? dateTo = null, - int? busId = null, string? description = null) + public IEnumerable ReadBusRepairs( + DateTime? dateForm = null, DateTime? dateTo = null, int? busId = null, string? description = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM BusRepairs"; + var busRepairss = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(busRepairss)); + return busRepairss; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs index bfa7c29..0198a34 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs @@ -1,13 +1,14 @@ -using Microsoft.Extensions.Logging; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; using ProjectAutoenterprise.Entities; namespace ProjectAutoenterprise.Repositories.Implementations; internal class BusRepository : IBusRepository { private readonly IConnectionString _connectionString; - private readonly ILogger _logger; - public BusRepository(IConnectionString connectionString, ILogger logger) { _connectionString = connectionString; @@ -16,19 +17,98 @@ internal class BusRepository : IBusRepository public void CreateBus(Bus bus) { - } - public void DeleteBus(int id) - { - } - public Bus ReadBusById(int id) - { - return Bus.CreateEntity(0, string.Empty, 0, 0); - } - public IEnumerable ReadBuses() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(bus)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Buses (Brand, Capacity, BusColors) +VALUES (@Brand, @Capacity, @BusColors)"; + connection.Execute(queryInsert, bus); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void UpdateBus(Bus bus) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(bus)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Buses +SET +Brand=@Brand, +Capacity=@Capacity, +BusColors=@BusColors +WHERE Id=@Id"; + connection.Execute(queryUpdate, bus); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } + public void DeleteBus(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Buses +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Bus ReadBusById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Buses +WHERE Id=@id"; + var bus = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(bus)); + return bus; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadBuses() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Buses"; + var buses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buses)); + return buses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs index 15eaea7..86c8ccf 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs @@ -2,5 +2,5 @@ internal class ConnectionString : IConnectionString { - string IConnectionString.ConnectionString => ""; + string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=autoenterprise"; } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs index fd511a0..2907bdb 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs @@ -1,14 +1,16 @@ -using Microsoft.Extensions.Logging; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; using ProjectAutoenterprise.Entities; using ProjectAutoenterprise.Entities.Enums; +using System.Data.SqlClient; namespace ProjectAutoenterprise.Repositories.Implementations; internal class EmployeeRepository : IEmployeeRepository { private readonly IConnectionString _connectionString; - private readonly ILogger _logger; - public EmployeeRepository(IConnectionString connectionString, ILogger logger) { _connectionString = connectionString; @@ -17,31 +19,115 @@ internal class EmployeeRepository : IEmployeeRepository public void CreateEmployee(Employee employee) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Employees (FirstName, LastName, BirthDate, EmployeePost) +VALUES (@FirstName, @LastName, @BirthDate, @EmployeePost)"; + connection.Execute(queryInsert, employee); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - - public void DeleteEmployee(int id) - { - - } - - public Employee ReadEmployeeById(int id) - { - return Employee.CreateEntity(0, string.Empty, string.Empty, DateTime.MinValue, EmployeePost.None); - } - - public IEnumerable ReadEmployees() - { - return []; - } - - public IEnumerable ReadEmployees(EmployeePost employeePost) - { - return []; - } - public void UpdateEmployee(Employee employee) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Employees +SET +FirstName=@FirstName, +LastName=@LastName, +BirthDate=@BirthDate, +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); + 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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee)); + return employee; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadEmployees() + { + _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; + } + } + public IEnumerable ReadEmployees(EmployeePost employeePost) + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Employees WHERE EmployeePost='" + employeePost.ToString()+"'"; + var employees = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees)); + return employees; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs index 6dde6d9..d90d2fe 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs @@ -1,6 +1,8 @@ -using Microsoft.Extensions.Logging; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; using ProjectAutoenterprise.Entities; - namespace ProjectAutoenterprise.Repositories.Implementations; internal class ItineraryRepository : IItineraryRepository @@ -17,13 +19,70 @@ internal class ItineraryRepository : IItineraryRepository public void CreateItinerary(Itinerary itinerary) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(itinerary)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" +INSERT INTO Itineraries (BusId, DriverId, ConductorId, ItineraryDate, Description) +VALUES (@BusId, @DriverId, @ConductorId, @ItineraryDate, @Description); +SELECT MAX(Id) FROM Itineraries"; + var itineraryId = connection.QueryFirst(queryInsert, itinerary, transaction); + var querySubInsert = @" +INSERT INTO ItineraryRoutes (ItineraryId, RouteId) +VALUES (@ItineraryId, @RouteId)"; + foreach (var elem in itinerary.ItineraryRoutes) + { + connection.Execute(querySubInsert, new { itineraryId, elem.RouteId }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } + public void DeleteItinerary(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Itineraries +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - public IEnumerable ReadItinerary(DateTime? dateForm = null, DateTime? dateTo = null, - int? driverId = null, int? conductorId = null) + + public IEnumerable ReadItinerary( + DateTime? dateForm = null, DateTime? dateTo = null, int? driverId = null, int? conductorId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Itineraries"; + var itineraries = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(itineraries)); + return itineraries; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs index 0aca689..4db1c21 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs @@ -1,13 +1,15 @@ -using Microsoft.Extensions.Logging; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; using ProjectAutoenterprise.Entities; +using System.Data.SqlClient; namespace ProjectAutoenterprise.Repositories.Implementations; internal class RouteRepository : IRouteRepository { private readonly IConnectionString _connectionString; - private readonly ILogger _logger; - public RouteRepository(IConnectionString connectionString, ILogger logger) { _connectionString = connectionString; @@ -16,19 +18,100 @@ internal class RouteRepository : IRouteRepository public void CreateRoute(Route route) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Routes (BeginRoutePoint, EndRoutePoint) +VALUES (@BeginRoutePoint, @EndRoutePoint)"; + connection.Execute(queryInsert, route); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteRoute(int id) - { - } - public Route ReadRouteById(int id) - { - return Route.CreateEntity(0, string.Empty, string.Empty); - } - public IEnumerable ReadRoutes() - { - return []; - } + public void UpdateRoute(Route route) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Routes +SET +BeginRoutePoint=@BeginRoutePoint, +EndRoutePoint=@EndRoutePoint +WHERE Id=@Id"; + connection.Execute(queryUpdate, route); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } + + public void DeleteRoute(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Routes +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + + public Route ReadRouteById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Routes +WHERE Id=@id"; + var route = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(route)); + return route; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadRoutes() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Routes"; + var routes = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(routes)); + return routes; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file