PIBD14-Boyko-M.S.-GSM-Autot.../ProjectGSM/Repositories/Implementations/CaseAdvocatesRepository.cs
2024-12-03 00:34:28 +04:00

64 lines
2.2 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 CaseAdvocatesRepository : ICaseAdvocateRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<CaseAdvocatesRepository> _logger;
public CaseAdvocatesRepository(IConnectionString connectionString,
ILogger<CaseAdvocatesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public IEnumerable<CaseAdvocate> ReadCaseAdvocates(DateTime? dateForm = null, DateTime? dateTo = null,
int? caseId = null, int? statusId = null)
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new
NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM case_advocates";
var caseAdvocates =
connection.Query<CaseAdvocate>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonSerializer.Serialize(caseAdvocates));
return caseAdvocates;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public void CreateCaseAdvocates(CaseAdvocate caseAdvocate)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}",
JsonSerializer.Serialize(caseAdvocate));
try
{
using var connection = new
NpgsqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO case_advocates (caseid, advocateid, post, createdat)
VALUES (@CaseId, @AdvocateId, @Post, @CreatedAt)";
connection.Execute(queryInsert, caseAdvocate);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
}