74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
using System.Data.SqlClient;
|
|
using System.Text.Json;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Npgsql;
|
|
using ProjectGSM.Entities;
|
|
using ProjectGSM.Query;
|
|
|
|
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
|
|
{
|
|
var builder = new QueryBuilder();
|
|
if (dateTo.HasValue)
|
|
{
|
|
builder.AddCondition("ca.CreatedAt <= @dateTo");
|
|
}
|
|
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect =
|
|
"SELECT ca.*, a.name as AdvocateName, cs.description as CaseDescription " +
|
|
"FROM case_advocates as ca LEFT JOIN cases as cs ON cs.id = ca.caseid " +
|
|
$"LEFT JOIN advocates as a ON a.id = ca.advocateid {builder.Build()}";
|
|
var caseAdvocates =
|
|
connection.Query<CaseAdvocate>(querySelect, param: new { dateTo });
|
|
_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;
|
|
}
|
|
}
|
|
} |