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 _logger; public CaseAdvocatesRepository(IConnectionString connectionString, ILogger logger) { _connectionString = connectionString; _logger = logger; } public IEnumerable 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(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; } } }