122 lines
4.0 KiB
C#
122 lines
4.0 KiB
C#
using System.Data.SqlClient;
|
|
using System.Text.Json;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Npgsql;
|
|
using ProjectGSM.Entities;
|
|
|
|
namespace ProjectGSM.Repositories.Implementations;
|
|
|
|
public class CourtRepository : ICourtRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<CourtRepository> _logger;
|
|
|
|
public CourtRepository(IConnectionString connectionString,
|
|
ILogger<CourtRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
|
|
public IEnumerable<Court> ReadCourts()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM courts";
|
|
var courts = connection.Query<Court>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
JsonSerializer.Serialize(courts));
|
|
return courts;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Court ReadCourtById(int id)
|
|
{
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @"SELECT * FROM courts WHERE Id = @Id";
|
|
var court = connection.QueryFirst<Court>(querySelect, new { id });
|
|
_logger.LogDebug("Полученный объект: {json}",
|
|
JsonSerializer.Serialize(court));
|
|
return court;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void CreateCourt(Court court)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonSerializer.Serialize(court));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"INSERT INTO courts (name, address, createdat)
|
|
VALUES (@Name, @Address, @CreatedAt);";
|
|
connection.Execute(queryInsert, court);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void UpdateCourt(Court court)
|
|
{
|
|
_logger.LogInformation("Обновление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonSerializer.Serialize(court));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"UPDATE courts
|
|
SET name = @Name,
|
|
address = @Address
|
|
WHERE id = @Id;";
|
|
connection.Execute(queryUpdate, court);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при обновлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void DeleteCourt(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryDelete = @"DELETE FROM courts WHERE id = @Id;";
|
|
connection.Execute(queryDelete, new { Id = id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
} |