151 lines
4.9 KiB
C#
151 lines
4.9 KiB
C#
using ProjectSellPC.Entites;
|
|
using ProjectSellPC.Entites.Enums;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using System.Data;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace ProjectSellPC.Repos.Impements
|
|
{
|
|
public class ProductRepo : IProductRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
private readonly ILogger<ProductRepo> _logger;
|
|
|
|
public ProductRepo(IConnectionString connectionString, ILoggerFactory loggerFactory)
|
|
{
|
|
_connectionString = connectionString;
|
|
//_logger = logger;
|
|
_logger = loggerFactory.CreateLogger<ProductRepo>();
|
|
}
|
|
|
|
private IDbConnection CreateConnection() => new NpgsqlConnection(_connectionString.ConnectionString);
|
|
public void Create(Product product)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
|
|
|
try
|
|
{
|
|
using (var connection = CreateConnection())
|
|
{
|
|
var sql = "INSERT INTO \"product\" (\"name\", \"description\", \"price\", \"producttype\") " +
|
|
"VALUES (@Name, @Description, @Price, @ProductType)";
|
|
|
|
connection.Execute(sql, new
|
|
{
|
|
Name = product.Name,
|
|
Description = product.Description,
|
|
Price = product.Price,
|
|
ProductType = (int)product.ProductType
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void Delete(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
try
|
|
{
|
|
using (var connection = CreateConnection())
|
|
{
|
|
var sql = "DELETE FROM \"product\" WHERE \"id\" = @Id";
|
|
connection.Execute(sql, new { Id = id });
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public Product Read(int id)
|
|
{
|
|
_logger.LogInformation("Получение объекта по идентификатору");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
|
|
try
|
|
{
|
|
using (var connection = CreateConnection())
|
|
{
|
|
var sql = $"SELECT * FROM \"product\" WHERE \"id\" = {id}";
|
|
return connection.QuerySingleOrDefault<Product>(sql);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Ошибка при поиске объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Product> ReadAll()
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
|
|
try
|
|
{
|
|
using (var connection = CreateConnection())
|
|
{
|
|
var sql = "SELECT * FROM \"product\"";
|
|
return connection.Query<Product>(sql).ToList();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
public Product Update(Product product)
|
|
{
|
|
_logger.LogInformation("Редактирование объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
|
|
|
try
|
|
{
|
|
using (var connection = CreateConnection())
|
|
{
|
|
var sql = "UPDATE \"product\" " +
|
|
"SET \"name\" = @Name, " +
|
|
"\"description\" = @Description, " +
|
|
"\"price\" = @Price," +
|
|
"\"producttype\" = @ProductType" +
|
|
" WHERE \"id\" = @Id";
|
|
|
|
connection.Execute(sql, new
|
|
{
|
|
Id = product.ID,
|
|
Name = product.Name,
|
|
Description = product.Description,
|
|
Price = product.Price,
|
|
ProductType = (int)product.ProductType
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при изменении объекта");
|
|
throw;
|
|
}
|
|
return product;
|
|
|
|
}
|
|
}
|
|
}
|