2024-11-19 00:15:28 +04:00
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Dapper;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using ProjectGSM.Entities;
|
2024-11-05 03:59:13 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectGSM.Repositories.Implementations;
|
|
|
|
|
|
|
|
|
|
public class CaseRepository : ICaseRepository
|
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
|
private readonly ILogger<CaseRepository> _logger;
|
|
|
|
|
|
|
|
|
|
public CaseRepository(IConnectionString connectionString,
|
|
|
|
|
ILogger<CaseRepository> logger)
|
2024-11-05 03:59:13 +04:00
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_connectionString = connectionString;
|
|
|
|
|
_logger = logger;
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 00:15:28 +04:00
|
|
|
|
public IEnumerable<Case> ReadCases()
|
2024-11-05 03:59:13 +04:00
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_logger.LogInformation("Получение всех объектов");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new
|
|
|
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var querySelect = "SELECT * FROM cases";
|
|
|
|
|
var cases = connection.Query<Case>(querySelect);
|
|
|
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
|
|
|
JsonSerializer.Serialize(cases));
|
|
|
|
|
return cases;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 00:15:28 +04:00
|
|
|
|
public Case ReadCaseById(int id)
|
2024-11-05 03:59:13 +04:00
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
|
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new
|
|
|
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var querySelect = @"SELECT * FROM cases WHERE Id=@id";
|
|
|
|
|
var caseE = connection.QueryFirst<Case>(querySelect, new { id });
|
|
|
|
|
_logger.LogDebug("Найденный объект: {json}",
|
|
|
|
|
JsonSerializer.Serialize(caseE));
|
|
|
|
|
return caseE;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 00:15:28 +04:00
|
|
|
|
public void CreateCase(Case caseEntity)
|
2024-11-05 03:59:13 +04:00
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_logger.LogInformation("Добавление объекта");
|
|
|
|
|
_logger.LogDebug("Объект: {json}",
|
|
|
|
|
JsonSerializer.Serialize(caseEntity));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
connection.Open();
|
|
|
|
|
using var transaction = connection.BeginTransaction();
|
|
|
|
|
var queryInsert = @"INSERT INTO Cases (typeappeal, Payment, Price, victoryprice, Verdict, courtid, clientid, Description, createdat)
|
|
|
|
|
VALUES (@TypeAppeal, @Payment, @Price, @VictoryPrice, @Verdict, @CourtId, @ClientId, @Description, @CreatedAt);
|
|
|
|
|
SELECT MAX(Id) FROM cases;";
|
|
|
|
|
var caseId =
|
|
|
|
|
connection.QueryFirst<int>(queryInsert, caseEntity, transaction);
|
|
|
|
|
var querySubInsert = @"
|
|
|
|
|
INSERT INTO case_advocates (caseid, advocateid, post, createdat)
|
|
|
|
|
VALUES (@CaseId, @AdvocateId, @Post, @CreatedAt)";
|
|
|
|
|
foreach (var elem in caseEntity.Advocates)
|
|
|
|
|
{
|
|
|
|
|
connection.Execute(querySubInsert, new {
|
|
|
|
|
caseId, elem.AdvocateId, elem.Post, elem.CreatedAt }, transaction);
|
|
|
|
|
}
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteCase(int id)
|
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_logger.LogInformation("Удаление объекта");
|
|
|
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new
|
|
|
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var queryDelete = @"DELETE FROM cases WHERE id = @Id;";
|
|
|
|
|
connection.Execute(queryDelete, new { Id = id });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|