diff --git a/PIbd-23_Gutorov_I.A._IT-Company/PIbd-23_Gutorov_I.A._IT-Company.csproj b/PIbd-23_Gutorov_I.A._IT-Company/PIbd-23_Gutorov_I.A._IT-Company.csproj
index 5159f6f..a3a964f 100644
--- a/PIbd-23_Gutorov_I.A._IT-Company/PIbd-23_Gutorov_I.A._IT-Company.csproj
+++ b/PIbd-23_Gutorov_I.A._IT-Company/PIbd-23_Gutorov_I.A._IT-Company.csproj
@@ -10,7 +10,18 @@
+
+
+
+
+
+
+
+
+
+
+
@@ -28,4 +39,10 @@
+
+
+ PreserveNewest
+
+
+
\ No newline at end of file
diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Program.cs b/PIbd-23_Gutorov_I.A._IT-Company/Program.cs
index 3c5e10d..8114fc4 100644
--- a/PIbd-23_Gutorov_I.A._IT-Company/Program.cs
+++ b/PIbd-23_Gutorov_I.A._IT-Company/Program.cs
@@ -1,6 +1,10 @@
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
using PIbd_23_Gutorov_I.A._IT_Company.Repositories;
using PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
+using Serilog;
using Unity;
+using Unity.Microsoft.Logging;
namespace PIbd_23_Gutorov_I.A._IT_Company
{
@@ -22,13 +26,28 @@ namespace PIbd_23_Gutorov_I.A._IT_Company
{
var container = new UnityContainer();
+ container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
+
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
+ container.RegisterType();
+
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;
+ }
}
}
\ No newline at end of file
diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/IConnectionString.cs b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/IConnectionString.cs
new file mode 100644
index 0000000..10298a0
--- /dev/null
+++ b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/IConnectionString.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories;
+
+public interface IConnectionString
+{
+ public string ConnectionString { get; }
+}
diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ConnectionString.cs b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ConnectionString.cs
new file mode 100644
index 0000000..c4b3c3f
--- /dev/null
+++ b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ConnectionString.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
+
+public class ConnectionString : IConnectionString
+{
+ string IConnectionString.ConnectionString => "";
+}
diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs
index 1e14ec1..04e2130 100644
--- a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs
+++ b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs
@@ -1,19 +1,81 @@
-using PIbd_23_Gutorov_I.A._IT_Company.Entities;
+using Dapper;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using System.Data.SqlClient;
namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
public class ContractRepository : IContractRepository
{
- public void CreateContract(Contract contract)
+ private readonly IConnectionString _connectionString;
+
+ private readonly ILogger _logger;
+
+ public ContractRepository(IConnectionString connectionString, ILogger logger)
{
+ _connectionString = connectionString;
+ _logger = logger;
+ }
+
+ public void CreateContract(Entities.Contract contract)
+ {
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contract));
+
+ try
+ {
+ using var connection = new SqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ var queryInsert = @"
+INSERT INTO Contracts (CustomerId, ExecutorId, Category, ConclusionDate, Deadline, PaymentAmount)
+VALUES (@CustomerId, @ExecutorId, @Category, @ConclusionDate, @Deadline, @PaymentAmount)";
+ connection.Execute(queryInsert, contract);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при добавлении объекта");
+ throw;
+ }
}
public void DeleteContract(int id)
{
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {id}", id);
+
+ try
+ {
+ using var connection = new SqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ var queryDelete = @"
+DELETE FROM Contracts
+WHERE Id=@id";
+ connection.Execute(queryDelete, new { id });
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
- public IEnumerable ReadContracts(DateTime? dateFrom = null, DateTime? dateTo = null, int? customerId = null, int? executorId = null)
+ public IEnumerable ReadContracts(DateTime? dateFrom = null, DateTime? dateTo = null, int? customerId = null, int? executorId = null)
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+
+ try
+ {
+ using var connection = new SqlConnection(_connectionString.ConnectionString);
+ connection.Open();
+ var querySelect = @"SELECT * FROM Contracts";
+ var contracts = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts));
+ return contracts;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
}
diff --git a/PIbd-23_Gutorov_I.A._IT-Company/appsettings.json b/PIbd-23_Gutorov_I.A._IT-Company/appsettings.json
new file mode 100644
index 0000000..8578938
--- /dev/null
+++ b/PIbd-23_Gutorov_I.A._IT-Company/appsettings.json
@@ -0,0 +1,15 @@
+{
+ "Serilog": {
+ "Using": [ "Serilog.Sinks.File" ],
+ "MinimumLevel": "Debug",
+ "WriteTo": [
+ {
+ "Name": "File",
+ "Args": {
+ "path": "Logs/it-company_log.txt",
+ "rollingInterval": "Day"
+ }
+ }
+ ]
+ }
+}
\ No newline at end of file