Реализация сущностей
This commit is contained in:
parent
736b16822b
commit
cd79ab9f26
@ -10,7 +10,18 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="Serilog" Version="4.0.2" />
|
||||||
|
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||||
|
<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" Version="5.11.10" />
|
||||||
|
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -28,4 +39,10 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -1,6 +1,10 @@
|
|||||||
using Accounting_Time_It_Company.Repositories;
|
using Accounting_Time_It_Company.Repositories;
|
||||||
using Accounting_Time_It_Company.Repositories.Implementations;
|
using Accounting_Time_It_Company.Repositories.Implementations;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
|
||||||
namespace Accounting_Time_It_Company
|
namespace Accounting_Time_It_Company
|
||||||
{
|
{
|
||||||
@ -21,13 +25,30 @@ namespace Accounting_Time_It_Company
|
|||||||
private static IUnityContainer CreateContainer()
|
private static IUnityContainer CreateContainer()
|
||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
|
|
||||||
container.RegisterType<IClientRepositories, ClientRepositories>();
|
container.RegisterType<IClientRepositories, ClientRepositories>();
|
||||||
container.RegisterType<IEmployeeRepositories, EmployeeRepositories>();
|
container.RegisterType<IEmployeeRepositories, EmployeeRepositories>();
|
||||||
container.RegisterType<IPostRepositories, PostRepositories>();
|
container.RegisterType<IPostRepositories, PostRepositories>();
|
||||||
container.RegisterType<IProductRepositories, ProductRepositories>();
|
container.RegisterType<IProductRepositories, ProductRepositories>();
|
||||||
container.RegisterType<ITypeJobRepositories, TypeJobRepositories>();
|
container.RegisterType<ITypeJobRepositories, TypeJobRepositories>();
|
||||||
container.RegisterType<IVacationRepositories, VacationRepositories>();
|
container.RegisterType<IVacationRepositories, VacationRepositories>();
|
||||||
|
|
||||||
|
container.RegisterType<IConnectionString, ConnectionString>();
|
||||||
return container;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
namespace Accounting_Time_It_Company.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
string ConnectionString { get; }
|
||||||
|
}
|
@ -1,28 +1,123 @@
|
|||||||
using Accounting_Time_It_Company.Entities;
|
using Accounting_Time_It_Company.Entities;
|
||||||
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
#pragma warning disable CS0618
|
||||||
|
|
||||||
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
internal class ClientRepositories : IClientRepositories
|
internal class ClientRepositories : IClientRepositories
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<ClientRepositories> _logger;
|
||||||
|
|
||||||
|
public ClientRepositories(IConnectionString connectionString, ILogger<ClientRepositories> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateClient(Client client)
|
public void CreateClient(Client client)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Client (NameClient, Phone)
|
||||||
|
VALUES (@NameClient, @Phone)";
|
||||||
|
connection.Execute(queryInsert, client);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавдении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateClient(Client client)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(client));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Client
|
||||||
|
SET
|
||||||
|
NameClient=@NameClient,
|
||||||
|
Phone=@Phone
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryUpdate, client);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteClient(int id)
|
public void DeleteClient(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Client
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Client ReadClientById(int id)
|
public Client ReadClientById(int id)
|
||||||
{
|
{
|
||||||
return Client.CreateEntity(0, string.Empty, string.Empty);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Client
|
||||||
|
WHERE Id=@id";
|
||||||
|
var client = connection.QueryFirst<Client>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(client));
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Client> ReadClients()
|
public IEnumerable<Client> ReadClients()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Client";
|
||||||
|
var clients = connection.Query<Client>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(clients));
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateClient(Client client)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "";
|
||||||
|
}
|
@ -1,28 +1,124 @@
|
|||||||
using Accounting_Time_It_Company.Entities;
|
using Accounting_Time_It_Company.Entities;
|
||||||
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class EmployeeRepositories : IEmployeeRepositories
|
public class EmployeeRepositories : IEmployeeRepositories
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<EmployeeRepositories> _logger;
|
||||||
|
|
||||||
|
public EmployeeRepositories(IConnectionString connectionString, ILogger<EmployeeRepositories> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateEmployee(Employee employee)
|
public void CreateEmployee(Employee employee)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
|
||||||
public void DeleteEmployee(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Employee ReadEmployeeById(int id)
|
INSERT INTO Employee (PostId, Name, Phone, Allowance)
|
||||||
{
|
VALUES (@PostId, @Name, @Phone, @Allowance)";
|
||||||
return Employee.CreateEntity(0, 0, string.Empty, string.Empty, 0f);
|
connection.Execute(queryInsert, employee);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Employee> ReadEmployees()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавдении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateEmployee(Employee employee)
|
public void UpdateEmployee(Employee employee)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(employee));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Employee
|
||||||
|
SET
|
||||||
|
PostId=@PostId,
|
||||||
|
Name=@Name,
|
||||||
|
Phone=@Phone,
|
||||||
|
Allowance=@Allowance
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryUpdate, employee);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteEmployee(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Employee
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee ReadEmployeeById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Employee
|
||||||
|
WHERE [Id]=@id";
|
||||||
|
var employee = connection.QueryFirst<Employee>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee));
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Employee> ReadEmployees()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Employee";
|
||||||
|
var employees = connection.Query<Employee>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(employees));
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,123 @@
|
|||||||
using Accounting_Time_It_Company.Entities;
|
using Accounting_Time_It_Company.Entities;
|
||||||
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class PostRepositories : IPostRepositories
|
public class PostRepositories : IPostRepositories
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<PostRepositories> _logger;
|
||||||
|
|
||||||
|
public PostRepositories(IConnectionString connectionString, ILogger<PostRepositories> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreatePost(Post post)
|
public void CreatePost(Post post)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(post));
|
||||||
public void DeletePost(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Post ReadPostById(int id)
|
INSERT INTO Post (NamePost, LevelPost, Wage)
|
||||||
{
|
VALUES (@NamePost, @LevelPost, @Wage)";
|
||||||
return Post.CreateEntity(0, 0, string.Empty, 0);
|
connection.Execute(queryInsert, post);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Post> ReadPosts()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавдении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdatePost(Post post)
|
public void UpdatePost(Post post)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(post));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Post
|
||||||
|
SET
|
||||||
|
NamePost=@NamePost,
|
||||||
|
LevelPost=@LevelPost,
|
||||||
|
Wage=@Wage
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryUpdate, post);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeletePost(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Post
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post ReadPostById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Post
|
||||||
|
WHERE Id=@id";
|
||||||
|
var post = connection.QueryFirst<Post>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(post));
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Post> ReadPosts()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Post";
|
||||||
|
var posts = connection.Query<Post>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(posts));
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,123 @@
|
|||||||
using Accounting_Time_It_Company.Entities;
|
using Accounting_Time_It_Company.Entities;
|
||||||
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
namespace Accounting_Time_It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class ProductRepositories : IProductRepositories
|
public class ProductRepositories : IProductRepositories
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<ProductRepositories> _logger;
|
||||||
|
|
||||||
|
public ProductRepositories(IConnectionString connectionString, ILogger<ProductRepositories> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateProduct(Product product)
|
public void CreateProduct(Product product)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(post));
|
||||||
public void DeleteProduct(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Product ReadProductById(int id)
|
INSERT INTO Product (ClientId, Name, Type)
|
||||||
{
|
VALUES (@ClientId, @Name, @Type)";
|
||||||
return Product.CreateEntity(0, 0, string.Empty, 0);
|
connection.Execute(queryInsert, product);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Product> ReadProducts()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавдении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateProduct(Product product)
|
public void UpdateProduct(Product product)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(product));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Product
|
||||||
|
SET
|
||||||
|
ClientId=@ClientId,
|
||||||
|
Name=@Name,
|
||||||
|
Type=@Type
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryUpdate, product);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteProduct(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Product
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product ReadProductById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Product
|
||||||
|
WHERE [Id]=@id";
|
||||||
|
var product = connection.QueryFirst<Product>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(product));
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Product> ReadProducts()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Product";
|
||||||
|
var products = connection.Query<Product>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(products));
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/company_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user