122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using ProjectFuel.Entities;
|
|
using ProjectFuel.Entities;
|
|
using ProjectFuel.Repositories;
|
|
|
|
namespace ProjectFuel.Repositories.Implementations;
|
|
|
|
public class RouteRepository : IRouteRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
private readonly ILogger<RouteRepository> _logger;
|
|
|
|
public RouteRepository(IConnectionString connectionString, ILogger<RouteRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
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 Route (Start_Point, End_Point, Route_Length)
|
|
VALUES (@Start_Point, @End_Point, @Route_Length)";
|
|
connection.Execute(queryInsert, 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 Route
|
|
WHERE Route_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 Route
|
|
WHERE Route_ID=@id";
|
|
var route = connection.QueryFirst<Route>(querySelect, new { id });
|
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(route));
|
|
return route;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Route> ReadRoutes()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM Route";
|
|
var routes = connection.Query<Route>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(routes));
|
|
return routes;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void UpdateRoute(Route route)
|
|
{
|
|
_logger.LogInformation("Редактирование объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route));
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"
|
|
UPDATE Route
|
|
SET
|
|
Start_Point=@Start_Point,
|
|
End_Point=@End_Point,
|
|
Route_Length=@Route_Length
|
|
WHERE Route_ID=@Route_ID";
|
|
connection.Execute(queryUpdate, route);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
|
throw;
|
|
}
|
|
}
|
|
}
|