126 lines
3.9 KiB
C#
126 lines
3.9 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using ProjectAtelier.Entities;
|
|
|
|
|
|
namespace ProjectAtelier.Repositories.Implementations;
|
|
|
|
public class ClientRepository : IClientRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<Client> _logger;
|
|
public ClientRepository(IConnectionString connectionString, ILogger<Client> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
|
|
public void CreateClient(Client client)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonConvert.SerializeObject(client));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"
|
|
INSERT INTO Clients (Name, Phone, Gender)
|
|
VALUES (@Name, @Phone, @Gender)";
|
|
connection.Execute(queryInsert, client);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
public void UpdateClient(Client client)
|
|
{
|
|
_logger.LogInformation("Редактирование объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonConvert.SerializeObject(client));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"
|
|
UPDATE Clients
|
|
SET
|
|
Name=@Name,
|
|
Phone=@Phone,
|
|
Gender=@Gender
|
|
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 });
|
|
}
|
|
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}", JsonConvert.SerializeObject(client));
|
|
return client;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Client> ReadClients()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM Clients";
|
|
var Client = connection.Query<Client>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
JsonConvert.SerializeObject(Client));
|
|
return Client;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
} |