106 lines
3.8 KiB
C#
106 lines
3.8 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using ProjectFamilyBudget.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ProjectFamilyBudget.Repositories.Implementations;
|
|
|
|
public class PeopleIncomeRepository : IPeopleIncome
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<PeopleIncomeRepository> _logger;
|
|
public PeopleIncomeRepository(IConnectionString connectionString, ILogger<PeopleIncomeRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
public void CreatePeopleIncome(PeopleIncome peopleIncome)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonConvert.SerializeObject(peopleIncome));
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
connection.Open();
|
|
using var transaction = connection.BeginTransaction();
|
|
var queryInsert = @"
|
|
INSERT INTO PeopleIncome (PeopleId, Date)
|
|
VALUES (@PeopleId, @Date);
|
|
SELECT MAX(Id) FROM PeopleIncome";
|
|
var peopleIncomeId =
|
|
connection.QueryFirst<int>(queryInsert, peopleIncome, transaction);
|
|
var querySubInsert = @"
|
|
INSERT INTO IncomePeopleIncome (IncomeId, PeopleIncomeId, Sum)
|
|
VALUES (@IncomeId, @PeopleIncomeId, @Sum)";
|
|
foreach (var elem in peopleIncome.IncomePeopleIncomes)
|
|
{
|
|
connection.Execute(querySubInsert, new
|
|
{
|
|
peopleIncomeId,
|
|
elem.IncomeId,
|
|
elem.Sum
|
|
}, transaction);
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
public void DeletPeopleIncome(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
connection.Open();
|
|
using var transaction = connection.BeginTransaction();
|
|
var queryDeleteSub = @"
|
|
DELETE FROM IncomePeopleIncome
|
|
WHERE PeopleIncomeId = @id";
|
|
connection.Execute(queryDeleteSub, new { id }, transaction);
|
|
|
|
var queryDelete = @"
|
|
DELETE FROM PeopleIncome
|
|
WHERE Id = @id";
|
|
connection.Execute(queryDelete, new { id }, transaction);
|
|
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
public IEnumerable<PeopleIncome> ReadPeopleIncome(DateTime? dateForm = null, DateTime? dateTo = null, int? peopleId = null, int? incomeId = null)
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM PeopleIncome";
|
|
var peopleIncomes = connection.Query<PeopleIncome>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
JsonConvert.SerializeObject(peopleIncomes));
|
|
return peopleIncomes;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
}
|