124 lines
4.2 KiB
C#
124 lines
4.2 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using ProjectFamilyBudget.Entities;
|
|
using ProjectFamilyBudget.Entities.Enums;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Unity;
|
|
|
|
namespace ProjectFamilyBudget.Repositories.Implementations;
|
|
|
|
public class ExpenseRepository : IExpense
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<ExpenseRepository> _logger;
|
|
public ExpenseRepository(IConnectionString connectionString, ILogger<ExpenseRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
public void CreateExpense(Expense expense)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(expense));
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryInsert = @"
|
|
INSERT INTO Expense (ExpenseType, Name, ExpenseCategory)
|
|
VALUES (@ExpenseType, @Name, @ExpenseCategory)";
|
|
connection.Execute(queryInsert, expense);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
public void UpdateExpense(Expense expense)
|
|
{
|
|
_logger.LogInformation("Редактирование объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(expense));
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryUpdate = @"
|
|
UPDATE Expense
|
|
SET
|
|
ExpenseType=@ExpenseType,
|
|
Name=@Name,
|
|
ExpenseCategory=@ExpenseCategory,
|
|
WHERE Id=@Id";
|
|
connection.Execute(queryUpdate, expense);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void DeleteExpense(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryDelete = @"
|
|
DELETE FROM Expense
|
|
WHERE Id=@id";
|
|
connection.Execute(queryDelete, new { id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
public Expense ReadExpenseById(int id)
|
|
{
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @"
|
|
SELECT * FROM Expense
|
|
WHERE Id=@id";
|
|
var expense = connection.QueryFirst<Expense>(querySelect, new { id });
|
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(expense));
|
|
return expense;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Expense> ReadExpense()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = "SELECT * FROM Expense";
|
|
var expenses = connection.Query<Expense>(querySelect);
|
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(expenses));
|
|
return expenses;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
}
|