Лабоартарная работа №2
This commit is contained in:
parent
e422a50929
commit
f7ce61f850
@ -1,16 +1,34 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<TargetFramework>net8.0-windows</TargetFramework>
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
<RootNamespace>ISEbd_22_Vasin_E.D._It_Company</RootNamespace>
|
<RootNamespace>ISEbd_22_Vasin_E.D._It_Company</RootNamespace>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<UseWindowsForms>true</UseWindowsForms>
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Unity" Version="5.11.10" />
|
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||||
</ItemGroup>
|
<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>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -1,7 +1,10 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company;
|
|
||||||
using ISEbd_22_Vasin_E.D._It_Company.Repositories;
|
using ISEbd_22_Vasin_E.D._It_Company.Repositories;
|
||||||
using ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
using ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company
|
namespace ISEbd_22_Vasin_E.D._It_Company
|
||||||
{
|
{
|
||||||
@ -23,6 +26,8 @@ namespace ISEbd_22_Vasin_E.D._It_Company
|
|||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
|
|
||||||
container.RegisterType<IClientRepository, ClientRepository>();
|
container.RegisterType<IClientRepository, ClientRepository>();
|
||||||
container.RegisterType<IContractorRepository, ContractorRepository>();
|
container.RegisterType<IContractorRepository, ContractorRepository>();
|
||||||
container.RegisterType<IContractorVacationRepository, ContractorVacationRepository>();
|
container.RegisterType<IContractorVacationRepository, ContractorVacationRepository>();
|
||||||
@ -30,7 +35,20 @@ namespace ISEbd_22_Vasin_E.D._It_Company
|
|||||||
container.RegisterType<IServiceRepository, ServiceRepository>();
|
container.RegisterType<IServiceRepository, ServiceRepository>();
|
||||||
container.RegisterType<IVacationRepository, VacationRepository>();
|
container.RegisterType<IVacationRepository, VacationRepository>();
|
||||||
|
|
||||||
|
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,6 @@
|
|||||||
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; }
|
||||||
|
}
|
@ -1,28 +1,125 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
using Dapper;
|
||||||
|
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class ClientRepository : IClientRepository
|
public class ClientRepository : IClientRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ClientRepository> _logger;
|
||||||
|
|
||||||
|
public ClientRepository(IConnectionString connectionString, ILogger<ClientRepository> 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 NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Clients (Name, LegalForm)
|
||||||
|
VALUES (@Name, @LegalForm);
|
||||||
|
";
|
||||||
|
connection.Execute(queryInsert, 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 NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Clients 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 NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Clients 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 NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Clients;
|
||||||
|
";
|
||||||
|
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)
|
public void UpdateClient(Client client)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Clients
|
||||||
|
SET Name = @Name, LegalForm = @LegalForm
|
||||||
|
WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryUpdate, client);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=dbotp";
|
||||||
|
}
|
||||||
|
|
@ -1,18 +1,82 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
using Dapper;
|
||||||
|
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities.Enums;
|
using ISEbd_22_Vasin_E.D._It_Company.Entities.Enums;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class ContractRepository : IContractRepository
|
public class ContractRepository : IContractRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ContractRepository> _logger;
|
||||||
|
|
||||||
|
public ContractRepository(IConnectionString connectionString, ILogger<ContractRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateContract(Contract contract)
|
public void CreateContract(Contract contract)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contract));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Contracts (ClientId, ContractorId,
|
||||||
|
Amount, StartDate, Deadline, ContractType)
|
||||||
|
VALUES (@ClientId, @ContractorId,
|
||||||
|
@Amount, @StartDate, @Deadline, @ContractType);
|
||||||
|
SELECT MAX(Id) FROM Contracts;
|
||||||
|
";
|
||||||
|
var contractId = connection.QueryFirst<int>(queryInsert, contract, transaction);
|
||||||
|
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO ServiceContracts (ContractId, ServiceId)
|
||||||
|
VALUES (@ContractId, @ServiceId);
|
||||||
|
";
|
||||||
|
foreach (var elem in contract.ServiceContracts)
|
||||||
|
{
|
||||||
|
var serviceId = elem.ServiceId;
|
||||||
|
connection.Execute(querySubInsert, new { contractId, serviceId }, transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Contract> ReadContracts(int? clientId = null, int? contractorId = null, int? amountFrom = null,
|
public IEnumerable<Contract> ReadContracts(int? clientId = null, int? contractorId = null, int? amountFrom = null,
|
||||||
int? amountTo = null, DateTime? startDateFrom = null, DateTime? startDateTo = null,
|
int? amountTo = null, DateTime? startDateFrom = null, DateTime? startDateTo = null,
|
||||||
DateTime? deadlineFrom = null, DateTime? deadlineTo = null, ContractType? contractType = null)
|
DateTime? deadlineFrom = null, DateTime? deadlineTo = null, ContractType? contractType = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Contracts
|
||||||
|
";
|
||||||
|
var contracts = connection.Query<Contract>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts));
|
||||||
|
return contracts;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,125 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
using Dapper;
|
||||||
|
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class ContractorRepository : IContractorRepository
|
public class ContractorRepository : IContractorRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ContractorRepository> _logger;
|
||||||
|
|
||||||
|
public ContractorRepository(IConnectionString connectionString, ILogger<ContractorRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateContractor(Contractor contractor)
|
public void CreateContractor(Contractor contractor)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractor));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Contractors (Name, Surname)
|
||||||
|
VALUES (@Name, @Surname);
|
||||||
|
";
|
||||||
|
connection.Execute(queryInsert, contractor);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteContractor(int id)
|
public void DeleteContractor(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Contractors WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contractor ReadContractorById(int id)
|
public Contractor ReadContractorById(int id)
|
||||||
{
|
{
|
||||||
return Contractor.CreateEntity(0, string.Empty, string.Empty);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Contractors WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
var contractor = connection.QueryFirst<Contractor>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(contractor));
|
||||||
|
return contractor;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Contractor> ReadContractors()
|
public IEnumerable<Contractor> ReadContractors()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Contractors;
|
||||||
|
";
|
||||||
|
var contractors = connection.Query<Contractor>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contractors));
|
||||||
|
return contractors;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateContractor(Contractor contractor)
|
public void UpdateContractor(Contractor contractor)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractor));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Contractors
|
||||||
|
SET Name = @Name, Surname = @Surname
|
||||||
|
WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryUpdate, contractor);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,83 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
using Dapper;
|
||||||
|
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class ContractorVacationRepository : IContractorVacationRepository
|
public class ContractorVacationRepository : IContractorVacationRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ContractorVacationRepository> _logger;
|
||||||
|
|
||||||
|
public ContractorVacationRepository(IConnectionString connectionString, ILogger<ContractorVacationRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateContractorVacation(ContractorVacation contractorVacation)
|
public void CreateContractorVacation(ContractorVacation contractorVacation)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractorVacation));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO ContractorVacations (ContractorId, VacationId)
|
||||||
|
VALUES (@ContractorId, @VacationId);
|
||||||
|
";
|
||||||
|
connection.Execute(queryInsert, contractorVacation);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ContractorVacation> ReadContractorVacations(int? contractorId = null,
|
public IEnumerable<ContractorVacation> ReadContractorVacations(int? contractorId = null,
|
||||||
int? vacationId = null)
|
int? vacationId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM ContractorVacations;
|
||||||
|
";
|
||||||
|
var contractorVacations = connection.Query<ContractorVacation>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contractorVacations));
|
||||||
|
return contractorVacations;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteContractorVacation(int id)
|
public void DeleteContractorVacation(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM ContractorVacations
|
||||||
|
WHERE Id = @id
|
||||||
|
";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,125 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
using Dapper;
|
||||||
|
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class ServiceRepository : IServiceRepository
|
public class ServiceRepository : IServiceRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ServiceRepository> _logger;
|
||||||
|
|
||||||
|
public ServiceRepository(IConnectionString connectionString, ILogger<ServiceRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateService(Service service)
|
public void CreateService(Service service)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Services (Name)
|
||||||
|
VALUES (@Name);
|
||||||
|
";
|
||||||
|
connection.Execute(queryInsert, service);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteService(int id)
|
public void DeleteService(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Services WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Service ReadServiceById(int id)
|
public Service ReadServiceById(int id)
|
||||||
{
|
{
|
||||||
return Service.CreateEntity(0, string.Empty);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Services WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
var service = connection.QueryFirst<Service>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(service));
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Service> ReadServices()
|
public IEnumerable<Service> ReadServices()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Services;
|
||||||
|
";
|
||||||
|
var services = connection.Query<Service>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(services));
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateService(Service service)
|
public void UpdateService(Service service)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Services
|
||||||
|
SET Name = @Name
|
||||||
|
WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryUpdate, service);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,29 +1,125 @@
|
|||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
using Dapper;
|
||||||
using ISEbd_22_Vasin_E.D._It_Company.Entities.Enums;
|
using ISEbd_22_Vasin_E.D._It_Company.Entities;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations;
|
||||||
|
|
||||||
public class VacationRepository : IVacationRepository
|
public class VacationRepository : IVacationRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<VacationRepository> _logger;
|
||||||
|
|
||||||
|
public VacationRepository(IConnectionString connectionString, ILogger<VacationRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateVacation(Vacation vacation)
|
public void CreateVacation(Vacation vacation)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(vacation));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Vacations (StartDate, EndDate, VacationType)
|
||||||
|
VALUES (@StartDate, @EndDate, @VacationType);
|
||||||
|
";
|
||||||
|
connection.Execute(queryInsert, vacation);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteVacation(int id)
|
public void DeleteVacation(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Vacations WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vacation ReadVacationById(int id)
|
public Vacation ReadVacationById(int id)
|
||||||
{
|
{
|
||||||
return Vacation.CreateEntity(0, DateTime.Now, DateTime.Now, VacationType.None);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Vacations WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
var vacation = connection.QueryFirst<Vacation>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(vacation));
|
||||||
|
return vacation;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Vacation> ReadVacations()
|
public IEnumerable<Vacation> ReadVacations()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Vacations;
|
||||||
|
";
|
||||||
|
var vacations = connection.Query<Vacation>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(vacations));
|
||||||
|
return vacations;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateVacation(Vacation vacation)
|
public void UpdateVacation(Vacation vacation)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(vacation));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Vacations
|
||||||
|
SET StartDate = @StartDate, EndDate = @EndDate, VacationType = @VacationType
|
||||||
|
WHERE Id = @Id;
|
||||||
|
";
|
||||||
|
connection.Execute(queryUpdate, vacation);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/it_company_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user