Lab2
This commit is contained in:
parent
f18602b2c8
commit
962bd7cf42
@ -1,7 +1,11 @@
|
|||||||
using Unity.Lifetime;
|
using Unity.Lifetime;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ProjectItCompany.Repositories;
|
using ProjectItCompany.Repositories;
|
||||||
using ProjectItCompany.Repositories.Implementations;
|
using ProjectItCompany.Repositories.Implementations;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace ProjectItCompany
|
namespace ProjectItCompany
|
||||||
{
|
{
|
||||||
@ -22,13 +26,27 @@ namespace ProjectItCompany
|
|||||||
private static IUnityContainer CreateContainer()
|
private static IUnityContainer CreateContainer()
|
||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
|
|
||||||
container.RegisterType<ICustomerRepository, CustomerRepository>(new TransientLifetimeManager());
|
container.RegisterType<ICustomerRepository, CustomerRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IExecutorRepository, ExecutorRepository>(new TransientLifetimeManager());
|
container.RegisterType<IExecutorRepository, ExecutorRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IProjectRepository, ProjectRepository>(new TransientLifetimeManager());
|
container.RegisterType<IProjectRepository, ProjectRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IWageRepository, WageRepository>(new TransientLifetimeManager());
|
container.RegisterType<IWageRepository, WageRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IContractRepository, ContractRepository>(new TransientLifetimeManager());
|
container.RegisterType<IContractRepository, ContractRepository>(new TransientLifetimeManager());
|
||||||
|
container.RegisterType<IConnectionString, ConnectionString>();
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static LoggerFactory CreateLoggerFactory()
|
||||||
|
{
|
||||||
|
var loggerFactory = new LoggerFactory();
|
||||||
|
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||||
|
.AddJsonFile("appsettings.json")
|
||||||
|
.Build())
|
||||||
|
.CreateLogger());
|
||||||
|
return loggerFactory;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,7 +10,18 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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="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>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectItCompany.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
string ConnectionString { get; }
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectItCompany.Repositories.Implementations;
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Server=localhost, 5432;Database=furnitureotp;Uid=postgres;Pwd=postgres;";
|
||||||
|
}
|
@ -1,26 +1,95 @@
|
|||||||
using ProjectItCompany.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectItCompany.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.Contracts;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Unity;
|
||||||
|
|
||||||
namespace ProjectItCompany.Repositories.Implementations;
|
namespace ProjectItCompany.Repositories.Implementations;
|
||||||
|
|
||||||
internal class ContractRepository : IContractRepository
|
public class ContractRepository : IContractRepository
|
||||||
{
|
{
|
||||||
public void CreateContract(Contract contract)
|
private readonly IConnectionString _connectionString;
|
||||||
{
|
private readonly ILogger<ContractRepository> _logger;
|
||||||
|
|
||||||
|
public ContractRepository(IConnectionString connectionString, ILogger<ContractRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateContract(Entities.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 (Date, ProjectId)
|
||||||
|
VALUES (@Date, @ProjectId);
|
||||||
|
SELECT MAX(Id) FROM Contracts";
|
||||||
|
var feedReplenishmentId = connection.QueryFirst<int>(queryInsert, contract, transaction);
|
||||||
|
var querySubInsert = @"INSERT INTO CustomerOnProject (ContractId, CostomerId, Description)
|
||||||
|
VALUES (@ContractId,@CostomerId, @Description)";
|
||||||
|
foreach (var elem in contract.CustomerId)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new
|
||||||
|
{
|
||||||
|
elem.ContractId,
|
||||||
|
elem.Description,
|
||||||
|
elem.CustomerId
|
||||||
|
}, transaction);
|
||||||
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteContract(int id)
|
public void DeleteContract(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"DELETE FROM Contracts
|
||||||
|
WHERE ID=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Contract> ReadContract(DateTime? dateForm = null, DateTime? dateTo = null, decimal? typeProject = null, int? customerId = null)
|
public IEnumerable<Entities.Contract> ReadContract(DateTime? dateForm = null, DateTime? dateTo = null, decimal? typeProject = null, int? customerId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"SELECT * FROM Contracts";
|
||||||
|
var contracts = connection.Query<Entities.Contract>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts));
|
||||||
|
return contracts;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectItCompany.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectItCompany.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -7,30 +11,111 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace ProjectItCompany.Repositories.Implementations;
|
namespace ProjectItCompany.Repositories.Implementations;
|
||||||
|
|
||||||
internal class CustomerRepository : ICustomerRepository
|
public class CustomerRepository : ICustomerRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<CustomerRepository> _logger;
|
||||||
|
|
||||||
|
public CustomerRepository(IConnectionString connectionString, ILogger<CustomerRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreateCustomer(Customer customer)
|
public void CreateCustomer(Customer customer)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
}
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(customer));
|
||||||
|
try
|
||||||
public void DeleteCustomer(int id)
|
{
|
||||||
{
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Customers (NameCustomer, Description)
|
||||||
}
|
VALUES (@NameCustomer, @Description)";
|
||||||
|
connection.Execute(queryInsert, customer);
|
||||||
public Customer ReadCustomerById(int id)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
return Customer.CreateEntity(0, string.Empty, string.Empty);
|
{
|
||||||
}
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
public IEnumerable<Customer> ReadCustomers()
|
}
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateCustomer(Customer customer)
|
public void UpdateCustomer(Customer customer)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(customer));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"UPDATE Customers SET
|
||||||
|
NameCustomer=@NameCustomer,
|
||||||
|
Description=@Description
|
||||||
|
WHERE ID=@id";
|
||||||
|
connection.Execute(queryUpdate, customer);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteCustomer(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"DELETE FROM Customers WHERE ID=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer ReadCustomerById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"SELECT * FROM Customers WHERE ID=@id";
|
||||||
|
var customer = connection.QueryFirst<Customer>(querySelect, new
|
||||||
|
{
|
||||||
|
id
|
||||||
|
});
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(customer));
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Customer> ReadCustomers()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Customers";
|
||||||
|
var customers = connection.Query<Customer>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(customers));
|
||||||
|
return customers;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectItCompany.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectItCompany.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -7,30 +11,113 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace ProjectItCompany.Repositories.Implementations;
|
namespace ProjectItCompany.Repositories.Implementations;
|
||||||
|
|
||||||
internal class ExecutorRepository : IExecutorRepository
|
public class ExecutorRepository : IExecutorRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ExecutorRepository> _logger;
|
||||||
|
|
||||||
|
public ExecutorRepository(IConnectionString connectionString, ILogger<ExecutorRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreateExecutor(Executor executor)
|
public void CreateExecutor(Executor executor)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
}
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(executor));
|
||||||
|
try
|
||||||
public void DeleteExecutor(int id)
|
{
|
||||||
{
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Executors (FullName, DateOfBirth, HourlyRate, JobTitles)
|
||||||
}
|
VALUES (@FullName, @DateOfBirth, @HourlyRate, @JobTitles)";
|
||||||
|
connection.Execute(queryInsert, executor);
|
||||||
public Executor ReadExecutorById(int id)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
return Executor.CreateEntity(0, string.Empty, DateTime.Today, 1000, Entities.Enums.JobTitles.Developer);
|
{
|
||||||
}
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
public IEnumerable<Executor> ReadExecutors()
|
}
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateExecutor(Executor executor)
|
public void UpdateExecutor(Executor executor)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(executor));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"UPDATE Executors SET
|
||||||
|
FullName=@FullName,
|
||||||
|
DateOfBirth=@DateOfBirth,
|
||||||
|
HourlyRate=@HourlyRate,
|
||||||
|
JobTitles = @JobTitles
|
||||||
|
WHERE ID=@id";
|
||||||
|
connection.Execute(queryUpdate, executor);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteExecutor(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"DELETE FROM Executors WHERE ID=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Executor ReadExecutorById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"SELECT * FROM Executors WHERE ID=@id";
|
||||||
|
var executor = connection.QueryFirst<Executor>(querySelect, new
|
||||||
|
{
|
||||||
|
id
|
||||||
|
});
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(executor));
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Executor> ReadExecutors()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Executors";
|
||||||
|
var executors = connection.Query<Executor>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(executors));
|
||||||
|
return executors;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectItCompany.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectItCompany.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -7,30 +11,112 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace ProjectItCompany.Repositories.Implementations;
|
namespace ProjectItCompany.Repositories.Implementations;
|
||||||
|
|
||||||
internal class ProjectRepository : IProjectRepository
|
public class ProjectRepository : IProjectRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ProjectRepository> _logger;
|
||||||
|
|
||||||
|
public ProjectRepository(IConnectionString connectionString, ILogger<ProjectRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreateProject(Project project)
|
public void CreateProject(Project project)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
}
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(project));
|
||||||
|
try
|
||||||
public void DeleteProject(int id)
|
{
|
||||||
{
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Projects (NameProject, CustomerId, CompletedStages)
|
||||||
}
|
VALUES (@NameProject, @CustomerId, @CompletedStages)";
|
||||||
|
connection.Execute(queryInsert, project);
|
||||||
public Project ReadProjectById(int id)
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
return Project.CreateEntity(1, string.Empty, 0, Entities.Enums.CompletedStages.Planning);
|
{
|
||||||
}
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
public IEnumerable<Project> ReadProjects()
|
}
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateProject(Project project)
|
public void UpdateProject(Project project)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(project));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"UPDATE Projects SET
|
||||||
|
NameProject=@NameProject,
|
||||||
|
CustomerId=@CustomerId,
|
||||||
|
CompletedStages=@CompletedStages
|
||||||
|
WHERE ID=@id";
|
||||||
|
connection.Execute(queryUpdate, project);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteProject(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"DELETE FROM Projects WHERE ID=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Project ReadProjectById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"SELECT * FROM Projects WHERE ID=@id";
|
||||||
|
var project = connection.QueryFirst<Project>(querySelect, new
|
||||||
|
{
|
||||||
|
id
|
||||||
|
});
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(project));
|
||||||
|
return project;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Project> ReadProjects()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Projects";
|
||||||
|
var projects = connection.Query<Project>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(projects));
|
||||||
|
return projects;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectItCompany.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectItCompany.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -7,15 +11,52 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace ProjectItCompany.Repositories.Implementations;
|
namespace ProjectItCompany.Repositories.Implementations;
|
||||||
|
|
||||||
internal class WageRepository : IWageRepository
|
public class WageRepository : IWageRepository
|
||||||
{
|
{
|
||||||
public void CreateWage(Wage wages)
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<WageRepository> _logger;
|
||||||
|
|
||||||
|
public WageRepository(IConnectionString connectionString, ILogger<WageRepository> logger)
|
||||||
{
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
public void CreateWage(Wage wage)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(wage));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"INSERT INTO Wages (Month, Year, NumberOfHours, ExecutorID)
|
||||||
|
VALUES (@Month, @Year, @NumberOfHours, @ExecutorID)";
|
||||||
|
connection.Execute(queryInsert, wage);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Wage> ReadWage(int? yearForm = null, int? yearTo = null, int? numberOfHoursFrom = null, int? numberOfHoursTo = null, int? executorId = null)
|
public IEnumerable<Wage> ReadWage(int? yearForm = null, int? yearTo = null, int? numberOfHoursFrom = null, int? numberOfHoursTo = null, int? executorId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Wages";
|
||||||
|
var wages = connection.Query<Wage>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(wages));
|
||||||
|
return wages;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
ProjectItCompany/ProjectItCompany/appsettings.json
Normal file
15
ProjectItCompany/ProjectItCompany/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/itcompany_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user