2024-11-16 14:06:34 +04:00
|
|
|
|
using Dapper;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using ProjectFamilyBudget.Entities;
|
2024-11-05 17:54:04 +04:00
|
|
|
|
using ProjectFamilyBudget.Entities.Enums;
|
2024-11-02 12:36:04 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectFamilyBudget.Repositories.Implementations;
|
|
|
|
|
|
|
|
|
|
public class PeopleRepository : IPeople
|
|
|
|
|
{
|
2024-11-16 14:06:34 +04:00
|
|
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
|
private readonly ILogger<PeopleRepository> _logger;
|
|
|
|
|
public PeopleRepository(IConnectionString connectionString, ILogger<PeopleRepository> logger)
|
|
|
|
|
{
|
|
|
|
|
_connectionString = connectionString;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
2024-11-02 12:36:04 +04:00
|
|
|
|
public void CreatePeople(People people)
|
|
|
|
|
{
|
2024-11-16 14:06:34 +04:00
|
|
|
|
_logger.LogInformation("Добавление объекта");
|
|
|
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(people));
|
2024-11-02 12:36:04 +04:00
|
|
|
|
|
2024-11-16 14:06:34 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var queryInsert = @"
|
|
|
|
|
INSERT INTO People (Name, LastName, Age, MemberType)
|
|
|
|
|
VALUES (@Name, @LastName, @Age, @MemberType)";
|
|
|
|
|
connection.Execute(queryInsert, people);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void UpdatePeople(People people)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Редактирование объекта");
|
|
|
|
|
_logger.LogDebug("Объект: {json}",JsonConvert.SerializeObject(people));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var queryUpdate = @"
|
|
|
|
|
UPDATE People
|
|
|
|
|
SET
|
|
|
|
|
Name=@Name,
|
|
|
|
|
LastName=@LastName,
|
|
|
|
|
Age=@Age,
|
|
|
|
|
MemberType=@MemberType
|
|
|
|
|
WHERE Id=@Id";
|
|
|
|
|
connection.Execute(queryUpdate, people);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-02 12:36:04 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeletePeople(int id)
|
|
|
|
|
{
|
2024-11-16 14:06:34 +04:00
|
|
|
|
_logger.LogInformation("Удаление объекта");
|
|
|
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var queryDelete = @"
|
|
|
|
|
DELETE FROM People
|
|
|
|
|
WHERE Id=@id";
|
|
|
|
|
connection.Execute(queryDelete, new { id });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-02 12:36:04 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public People ReadPeopleById(int id)
|
|
|
|
|
{
|
2024-11-16 14:06:34 +04:00
|
|
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
|
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var querySelect = @"
|
|
|
|
|
SELECT * FROM People
|
|
|
|
|
WHERE Id=@id";
|
|
|
|
|
var people = connection.QueryFirst<People>(querySelect, new { id });
|
|
|
|
|
_logger.LogDebug("Найденный объект: {json}",
|
|
|
|
|
JsonConvert.SerializeObject(people));
|
|
|
|
|
return people;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-02 12:36:04 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<People> ReadPeople()
|
|
|
|
|
{
|
2024-11-16 14:06:34 +04:00
|
|
|
|
_logger.LogInformation("Получение всех объектов");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var querySelect = "SELECT * FROM People";
|
|
|
|
|
var peoples = connection.Query<People>(querySelect);
|
|
|
|
|
_logger.LogDebug("Полученные объекты: {json}",JsonConvert.SerializeObject(peoples));
|
|
|
|
|
return peoples;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-02 12:36:04 +04:00
|
|
|
|
}
|
|
|
|
|
}
|