ПИбд-22. Кукушкина.Е.О. Лабораторная работа 2 #2
@ -1,4 +1,5 @@
|
||||
using ProjectConfectFactory.Entities.Enums;
|
||||
using ConfectFactory.Entities;
|
||||
using ProjectConfectFactory.Entities.Enums;
|
||||
|
||||
namespace ProjectConfectFactory.Entities;
|
||||
|
||||
@ -21,4 +22,17 @@ public class Product
|
||||
Price = price
|
||||
};
|
||||
}
|
||||
|
||||
public static Product CreateEntity(TempRecipe tempRecipe,
|
||||
IEnumerable<Recipe> recipe)
|
||||
{
|
||||
return new Product
|
||||
{
|
||||
Id = tempRecipe.Id,
|
||||
Name = tempRecipe.Name,
|
||||
ProductType = tempRecipe.ProductType,
|
||||
Recipe = recipe,
|
||||
Price = tempRecipe.Price
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,9 @@ public class Recipe
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public int ComponentId { get; private set; }
|
||||
public double CountComponent { get; private set; }
|
||||
public decimal CountComponent { get; private set; }
|
||||
|
||||
public static Recipe CreateElement(int id, int componentId, double countComponent)
|
||||
public static Recipe CreateElement(int id, int componentId, decimal countComponent)
|
||||
{
|
||||
return new Recipe
|
||||
{
|
||||
|
@ -0,0 +1,13 @@
|
||||
using ProjectConfectFactory.Entities.Enums;
|
||||
|
||||
namespace ConfectFactory.Entities;
|
||||
|
||||
public class TempRecipe
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
public ProductType ProductType { get; private set; }
|
||||
public decimal Price { get; private set; }
|
||||
public int ComponentId { get; private set; }
|
||||
public decimal CountComponent { get; private set; }
|
||||
}
|
@ -49,6 +49,13 @@ public partial class FormProduct : Form
|
||||
textBoxProductName.Text = product.Name;
|
||||
numericUpDownProductPrice.Value = (decimal)product.Price;
|
||||
_productId = value;
|
||||
|
||||
foreach (var elem in product.Recipe)
|
||||
{
|
||||
var row = dataGridViewRecipe.Rows[dataGridViewRecipe.Rows.Add()];
|
||||
row.Cells["ColumnComponent"].Value = elem.ComponentId;
|
||||
row.Cells["ColumnCount"].Value = elem.CountComponent;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -2,6 +2,12 @@ using ProjectConfectFactory.Repositories.Implementations;
|
||||
using ProjectConfectFactory.Repositories;
|
||||
using Unity;
|
||||
using Unity.Lifetime;
|
||||
using ConfectFactory.Repositories.Implementations;
|
||||
using ConfectFactory.Repositories;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace ProjectConfectFactory
|
||||
{
|
||||
@ -23,13 +29,28 @@ namespace ProjectConfectFactory
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IOrderRepository, OrderRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IProductRepository, MyProductRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IComponentRepository, ComponentRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IPurchaseRepository, PurchaseRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ISellerRepository, SellerRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new TransientLifetimeManager());
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private static LoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build())
|
||||
.CreateLogger());
|
||||
return loggerFactory;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,19 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.2" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -27,4 +39,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,7 @@
|
||||
namespace ConfectFactory.Repositories
|
||||
{
|
||||
public interface IConnectionString
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
||||
}
|
@ -1,28 +1,132 @@
|
||||
using ProjectConfectFactory.Entities;
|
||||
using ConfectFactory.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectConfectFactory.Entities;
|
||||
|
||||
namespace ProjectConfectFactory.Repositories.Implementations;
|
||||
|
||||
public class ComponentRepository : IComponentRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<ComponentRepository> _logger;
|
||||
public ComponentRepository(IConnectionString connectionString, ILogger<ComponentRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateComponent(Component component)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(component));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Components (Name, Unit, Count)
|
||||
VALUES (@Name, @Unit, @Count)";
|
||||
connection.Execute(queryInsert, component);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteComponent(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM Components
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Component ReadComponentById(int id)
|
||||
{
|
||||
return Component.CreateEntity(0, string.Empty, string.Empty, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM Components
|
||||
WHERE Id=@id";
|
||||
var component = connection.QueryFirst<Component>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(component));
|
||||
return component;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Component> ReadComponents()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = "SELECT * FROM Components";
|
||||
var components = connection.Query<Component>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(components));
|
||||
return components;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateComponent(Component component)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(component));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryUpdate = @"
|
||||
UPDATE Components
|
||||
SET
|
||||
Name=@Name,
|
||||
Unit=@Unit,
|
||||
Count=@Count
|
||||
WHERE Id=@Id";
|
||||
|
||||
connection.Execute(queryUpdate, component);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
|
||||
namespace ConfectFactory.Repositories.Implementations;
|
||||
|
||||
public class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "host=localhost;port=5432;database=con_factory;username=postgres;password=postgres";
|
||||
}
|
@ -1,29 +1,168 @@
|
||||
using ProjectConfectFactory.Entities;
|
||||
using ProjectConfectFactory.Entities.Enums;
|
||||
using ConfectFactory.Entities;
|
||||
using ConfectFactory.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectConfectFactory.Entities;
|
||||
|
||||
namespace ProjectConfectFactory.Repositories.Implementations;
|
||||
|
||||
public class MyProductRepository : IProductRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<MyProductRepository> _logger;
|
||||
public MyProductRepository(IConnectionString connectionString, ILogger<MyProductRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateProduct(Product product)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Products (Name, ProductType, Price)
|
||||
VALUES (@Name, @ProductType, @Price);
|
||||
SELECT MAX(Id) FROM Products";
|
||||
var productId =
|
||||
connection.QueryFirst<int>(queryInsert, product, transaction);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO Recipe (productId, ComponentId, CountComponent)
|
||||
VALUES (@productId,@ComponentId, @CountComponent)";
|
||||
foreach (var elem in product.Recipe)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
productId,
|
||||
elem.ComponentId,
|
||||
elem.CountComponent
|
||||
}, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteProduct(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Products
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Product ReadProductById(int id)
|
||||
{
|
||||
return Product.CreateEntity(0, string.Empty, ProductType.None, [] ,0); ;
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT p.*, pc.ComponentId, pc.CountComponent FROM Products p
|
||||
INNER JOIN Recipe pc ON pc.ProductId = p.Id
|
||||
WHERE p.Id=@id";
|
||||
var product = connection.Query<TempRecipe>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(product));
|
||||
return product.GroupBy(x => x.Id, y => y,
|
||||
(key, value) => Product.CreateEntity(value.First(),
|
||||
value.Select(z => Recipe.
|
||||
CreateElement(0, z.ComponentId, z.CountComponent)))).ToList().First();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Product> ReadProducts()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT p.*, pc.ComponentId, pc.CountComponent FROM Products p
|
||||
INNER JOIN Recipe pc ON pc.ProductId = p.Id";
|
||||
var products = connection.Query<TempRecipe>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(products));
|
||||
return products.GroupBy(x => x.Id, y => y,
|
||||
(key, value) => Product.CreateEntity(value.First(),
|
||||
value.Select(z => Recipe.
|
||||
CreateElement(0, z.ComponentId, z.CountComponent)))).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateProduct(Product product)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(product));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryUpdate = @"
|
||||
UPDATE Products
|
||||
SET
|
||||
Name=@Name,
|
||||
ProductType=@ProductType,
|
||||
Price=@Price
|
||||
WHERE Id=@Id;
|
||||
DELETE FROM Recipe
|
||||
WHERE ProductId=@Id";
|
||||
connection.Execute(queryUpdate, product);
|
||||
var querySubInsert = @"
|
||||
INSERT INTO Recipe (ProductId, ComponentId, CountComponent)
|
||||
VALUES (@Id,@ComponentId, @CountComponent)";
|
||||
foreach (var elem in product.Recipe)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
product.Id,
|
||||
elem.ComponentId,
|
||||
elem.CountComponent
|
||||
}, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,96 @@
|
||||
using ProjectConfectFactory.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectFactory.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectConfectFactory.Entities;
|
||||
|
||||
namespace ProjectConfectFactory.Repositories.Implementations;
|
||||
|
||||
public class OrderRepository : IOrderRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<OrderRepository> _logger;
|
||||
public OrderRepository(IConnectionString connectionString,
|
||||
ILogger<OrderRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateOrder(Order order)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(order));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Orders (Name,Phone,DateTime)
|
||||
VALUES (@Name,@Phone, @DateTime);
|
||||
SELECT MAX(Id) FROM Orders";
|
||||
var orderId = connection.QueryFirst<int>(queryInsert, order, transaction);
|
||||
var querySubInsert = @"
|
||||
INSERT INTO OrderProduct (OrderId, ProductId, Count, Price)
|
||||
VALUES (@OrderId,@ProductId, @Count, @Price)";
|
||||
foreach (var elem in order.OrderProducts)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
orderId,
|
||||
elem.ProductId,
|
||||
elem.Count,
|
||||
elem.Price
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteOrder(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Orders
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Order> ReadOrder(DateTime? dateForm = null, DateTime? dateTo = null, int? ProductId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Orders";
|
||||
var orders = connection.Query<Order>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(orders));
|
||||
return orders;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
using ProjectConfectFactory.Entities;
|
||||
using ConfectFactory.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectConfectFactory.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,12 +14,52 @@ namespace ProjectConfectFactory.Repositories.Implementations;
|
||||
|
||||
public class PurchaseRepository : IPurchaseRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<PurchaseRepository> _logger;
|
||||
|
||||
public PurchaseRepository(IConnectionString connectionString, ILogger<PurchaseRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreatePurchase(Purchase purchase)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(purchase));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Purchases (DateTime, SellerId, ComponentId)
|
||||
VALUES (@DateTime, @SellerId, @ComponentId)";
|
||||
connection.Execute(queryInsert, purchase);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Purchase> ReadPurchases(DateTime? dateForm = null, DateTime? dateTo = null, int? SellerId = null, int? ComponentId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Purchases";
|
||||
var purchases =
|
||||
connection.Query<Purchase>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(purchases));
|
||||
return purchases;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,132 @@
|
||||
using ProjectConfectFactory.Entities;
|
||||
using ProjectConfectFactory.Entities.Enums;
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectFactory.Repositories;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using Dapper;
|
||||
|
||||
namespace ProjectConfectFactory.Repositories.Implementations;
|
||||
|
||||
public class SellerRepository : ISellerRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<SellerRepository> _logger;
|
||||
public SellerRepository(IConnectionString connectionString, ILogger<SellerRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateSeller(Seller seller)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(seller));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Sellers (Name, Phone, DeliveryTime)
|
||||
VALUES (@Name, @Phone, @DeliveryTime)";
|
||||
connection.Execute(queryInsert, seller);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteSeller(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM Sellers
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Seller ReadSellerById(int id)
|
||||
{
|
||||
return Seller.CreateEntity(0, string.Empty, string.Empty, DeliveryType.None); ;
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM Sellers
|
||||
WHERE Id=@id";
|
||||
var seller = connection.QueryFirst<Seller>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(seller));
|
||||
return seller;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Seller> ReadSellers()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = "SELECT * FROM Sellers";
|
||||
var sellers = connection.Query<Seller>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(sellers));
|
||||
return sellers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateSeller(Seller seller)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(seller));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryUpdate = @"
|
||||
UPDATE Sellers
|
||||
SET
|
||||
Name=@Name,
|
||||
Phone=@Phone,
|
||||
DeliveryTime=@DeliveryTime
|
||||
WHERE Id=@Id";
|
||||
|
||||
connection.Execute(queryUpdate, seller);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
ProjectConfectFactory/ProjectConfectFactory/appsettings.json
Normal file
15
ProjectConfectFactory/ProjectConfectFactory/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/conFactory_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user