ПИбд-23. Гуторов И.А. Лабораторная работа 2 #2
@ -15,6 +15,7 @@
|
||||
<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.1" />
|
||||
<PackageReference Include="Serilog" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||
|
@ -8,5 +8,5 @@ namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
|
||||
|
||||
public class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "";
|
||||
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=OTP";
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
|
||||
@ -24,11 +25,11 @@ public class ContractRepository : IContractRepository
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Contracts (CustomerId, ExecutorId, Category, ConclusionDate, Deadline, PaymentAmount)
|
||||
VALUES (@CustomerId, @ExecutorId, @Category, @ConclusionDate, @Deadline, @PaymentAmount)";
|
||||
INSERT INTO Contracts (CustomerId, ExecutorId, Category, ConclusionDate, Deadline, PaymentAmount)
|
||||
VALUES (@CustomerId, @ExecutorId, @Category, @ConclusionDate, @Deadline, @PaymentAmount)";
|
||||
connection.Execute(queryInsert, contract);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -45,11 +46,11 @@ VALUES (@CustomerId, @ExecutorId, @Category, @ConclusionDate, @Deadline, @Paymen
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var queryDelete = @"
|
||||
DELETE FROM Contracts
|
||||
WHERE Id=@id";
|
||||
DELETE FROM Contracts
|
||||
WHERE Id = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -65,7 +66,7 @@ WHERE Id=@id";
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new SqlConnection(_connectionString.ConnectionString);
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"SELECT * FROM Contracts";
|
||||
var contracts = connection.Query<Entities.Contract>(querySelect);
|
||||
|
@ -1,9 +1,13 @@
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
|
||||
namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
|
||||
|
||||
public class CustomerExecutorReviewRepository : ICustomerExecutorReviewRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<CustomerExecutorReviewRepository> _logger;
|
||||
|
||||
public void CreateCustomerExecutorReview(CustomerExecutorReview customerContractReview)
|
||||
{
|
||||
}
|
||||
|
@ -1,28 +1,125 @@
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
|
||||
namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
|
||||
|
||||
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)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(customer));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Customers (Name, Contact, Address)
|
||||
VALUES (@Name, @Contact, @Address);
|
||||
";
|
||||
connection.Execute(queryInsert, 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)
|
||||
{
|
||||
return Customer.CreateEntity(0, string.Empty, string.Empty, string.Empty);
|
||||
_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()
|
||||
{
|
||||
return [];
|
||||
_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;
|
||||
}
|
||||
}
|
||||
|
||||
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 Name = @Name, Contact = @Contact, Address = @Address
|
||||
WHERE Id = @Id;
|
||||
";
|
||||
connection.Execute(queryUpdate, customer);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,13 @@
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
|
||||
namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
|
||||
|
||||
public class ExecutorRepository : IExecutorRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<ExecutorRepository> _logger;
|
||||
|
||||
public void CreateExecutor(Executor executor)
|
||||
{
|
||||
}
|
||||
|
@ -1,28 +1,125 @@
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using PIbd_23_Gutorov_I.A._IT_Company.Entities;
|
||||
|
||||
namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations;
|
||||
|
||||
internal 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)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Services (Description, ContractId)
|
||||
VALUES (@Description, @ContractId);
|
||||
";
|
||||
connection.Execute(queryInsert, service);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return Service.CreateEntity(0, string.Empty, 0);
|
||||
_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(int? contractId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
SELECT * FROM Services;
|
||||
";
|
||||
var services = connection.Query<Service>(queryInsert);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(services));
|
||||
return services;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
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 Description = @Description, ContractId = @ContractId
|
||||
WHERE Id = @Id;
|
||||
";
|
||||
connection.Execute(queryUpdate, service);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user