126 lines
4.4 KiB
C#
126 lines
4.4 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 ClientRepository : IClientRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<ClientRepository> _logger;
|
|
|
|
public ClientRepository(IConnectionString connectionString,
|
|
ILogger<ClientRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
|
|
public IEnumerable<Client> ReadClients()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM clients";
|
|
var clients = connection.Query<Client>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
JsonSerializer.Serialize(clients));
|
|
return clients;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Client ReadClientById(int id)
|
|
{
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @"SELECT * FROM clients WHERE Id=@id";
|
|
var client = connection.QueryFirst<Client>(querySelect, new { id });
|
|
_logger.LogDebug("Найденный объект: {json}",
|
|
JsonSerializer.Serialize(client));
|
|
return client;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void CreateClient(Client client)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonSerializer.Serialize(client));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"INSERT INTO clients (name, sex, dateOfBirth, email, phonenumber, address, createdAt)
|
|
VALUES (@Name, @Sex, @DateOfBirth, @Email, @PhoneNumber, @Address, @CreatedAt);";
|
|
connection.Execute(queryInsert, client);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void UpdateClient(Client client)
|
|
{
|
|
_logger.LogInformation("Обновление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonSerializer.Serialize(client));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"UPDATE clients
|
|
SET name = @Name,
|
|
sex = @Sex,
|
|
dateofbirth = @DateOfBirth,
|
|
email = @Email,
|
|
phonenumber = @PhoneNumber,
|
|
address = @Address
|
|
WHERE id = @Id;";
|
|
connection.Execute(queryUpdate, client);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при обновлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void DeleteClient(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryDelete = @"DELETE FROM clients WHERE id = @Id;";
|
|
connection.Execute(queryDelete, new { Id = id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
} |