5 Commits
Lab1 ... Lab2

22 changed files with 548 additions and 159 deletions

View File

@@ -6,17 +6,17 @@ namespace RealEstateTransactions.Entities
{
public int Id { get; private set; }
public AgencyType AgencyId { get; private set; }
public AgencyType Agency_ID { get; private set; }
public FormFactorType FormFactorId { get; private set; }
public FormFactorType Form_factor_ID { get; private set; }
public float Area { get; private set; }
public float PricePerSM { get; private set; }
public float Price_per_SM { get; private set; }
public float BasePrice { get; private set; }
public float Base_price { get; private set; }
public float DesiredPrice { get; private set; }
public float Desired_price { get; private set; }
public static Apartment CreateApartment(int id, AgencyType agencyId, FormFactorType formFactorId, float area,
float pricePerSM, float basePrice, float desiredPrice)
@@ -24,12 +24,12 @@ namespace RealEstateTransactions.Entities
return new Apartment
{
Id = id,
AgencyId = agencyId,
FormFactorId = formFactorId,
Agency_ID = agencyId,
Form_factor_ID = formFactorId,
Area = area,
PricePerSM = pricePerSM,
BasePrice = basePrice,
DesiredPrice = desiredPrice
Price_per_SM = pricePerSM,
Base_price = basePrice,
Desired_price = desiredPrice
};
}
}

View File

@@ -4,20 +4,20 @@
{
public int Id { get; private set; }
public string FullName { get; private set; } = string.Empty;
public string Full_name { get; private set; } = string.Empty;
public int PassportSeries { get; private set; }
public int Passport_series { get; private set; }
public int PassportNumber { get; private set; }
public int Passport_number { get; private set; }
public static Buyer CreateBuyer(int id, string fullName, int passportSeries, int passportNumber)
{
return new Buyer
{
Id = id,
FullName = fullName,
PassportSeries = passportSeries,
PassportNumber = passportNumber
Full_name = fullName,
Passport_series = passportSeries,
Passport_number = passportNumber
};
}
}

View File

@@ -4,13 +4,13 @@
{
public int Id { get; private set; }
public int ApartmentId { get; private set; }
public int Apartment_ID { get; private set; }
public int BuyerId { get; private set; }
public int Buyer_ID { get; private set; }
public float DealPrice { get; private set; }
public float Deal_price { get; private set; }
public DateTime DealDate { get; private set; }
public DateTime Deal_date { get; private set; }
public IEnumerable<ServicesDeal> DealServices { get; private set; } = [];
@@ -20,10 +20,10 @@
return new Deal
{
Id = id,
ApartmentId = apartmentId,
BuyerId = buyerId,
DealPrice = dealPrice,
DealDate = dealDate,
Apartment_ID = apartmentId,
Buyer_ID = buyerId,
Deal_price = dealPrice,
Deal_date = dealDate,
DealServices = dealServices
};
}

View File

@@ -4,7 +4,7 @@
{
public int Id { get; private set; }
public int ApartmentId { get; private set; }
public int Apartment_ID { get; private set; }
public string Name { get; private set; } = string.Empty;
@@ -15,7 +15,7 @@
return new PreSalesServices
{
Id = id,
ApartmentId = apartmentId,
Apartment_ID = apartmentId,
Name = name,
Cost = cost
};

View File

@@ -2,19 +2,19 @@
{
public class ServicesDeal
{
public int ServicesId { get; private set; }
public int Services_ID { get; private set; }
public int DealId { get; private set; }
public int Deal_ID { get; private set; }
public float ExecutionTime { get; private set; }
public double Execution_time { get; private set; }
public static ServicesDeal CreateServicesDeal(int servicesId, int dealId, float executionTime)
public static ServicesDeal CreateServicesDeal(int servicesId, int dealId, double executionTime)
{
return new ServicesDeal
{
ServicesId = servicesId,
DealId = dealId,
ExecutionTime = executionTime
Services_ID = servicesId,
Deal_ID = dealId,
Execution_time = executionTime
};
}
}

View File

@@ -22,15 +22,15 @@ namespace RealEstateTransactions.Forms
foreach (FormFactorType elem in Enum.GetValues(typeof(FormFactorType)))
{
if ((elem & apartment.FormFactorId) != 0)
if ((elem & apartment.Form_factor_ID) != 0)
checkedListBoxFormFactor.SetItemChecked(checkedListBoxFormFactor.Items.IndexOf(elem), true);
}
comboBoxAgency.SelectedIndex = (int)apartment.AgencyId;
comboBoxAgency.SelectedIndex = (int)apartment.Agency_ID;
numericUpDownArea.Value = (decimal)apartment.Area;
numericUpDownPricePerSM.Value = (decimal)apartment.PricePerSM;
numericUpDownBasePrice.Value = (decimal)apartment.BasePrice;
numericUpDownDesiredPrice.Value = (decimal)apartment.DesiredPrice;
numericUpDownPricePerSM.Value = (decimal)apartment.Price_per_SM;
numericUpDownBasePrice.Value = (decimal)apartment.Base_price;
numericUpDownDesiredPrice.Value = (decimal)apartment.Desired_price;
_apartmentId = value;
}

View File

@@ -19,9 +19,9 @@ namespace RealEstateTransactions.Forms
if (buyer == null) throw new InvalidDataException(nameof(buyer));
textBoxFullName.Text = buyer.FullName;
numericUpDownPassportSeries.Value = buyer.PassportSeries;
numericUpDownPassportNumber.Value = buyer.PassportNumber;
textBoxFullName.Text = buyer.Full_name;
numericUpDownPassportSeries.Value = buyer.Passport_series;
numericUpDownPassportNumber.Value = buyer.Passport_number;
_buyerId = value;
}

View File

@@ -60,7 +60,8 @@ namespace RealEstateTransactions.Forms
{
continue;
}
list.Add(ServicesDeal.CreateServicesDeal((int)row.Cells["ColumnService"].Value, 0, (float)row.Cells["ColumnTimeSpan"].Value));
list.Add(ServicesDeal.CreateServicesDeal((int)row.Cells["ColumnService"].Value,
0, Convert.ToDouble(row.Cells["ColumnTimeSpan"].Value)));
}
return list;
}

View File

@@ -1,6 +1,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using RealEstateTransactions.Repositories;
using RealEstateTransactions.Repositories.Implementations;
using Serilog;
using Unity;
using Unity.Microsoft.Logging;
namespace RealEstateTransactions
{
@@ -26,10 +30,25 @@ namespace RealEstateTransactions
container.RegisterType<IBuyerRepository, BuyerRepository>();
container.RegisterType<IDealRepository, DealRepository>();
container.RegisterType<IPreSalesServicesRepository, PreSalesServicesRepository>();
container.RegisterType<IServicesDealRepository, ServicesDealRepository>();
container.RegisterType<IServicesRepository, ServicesRepository>();
container.RegisterType<IConnectionString, ConnectionString>();
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
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;
}
}
}

View File

@@ -9,7 +9,18 @@
</PropertyGroup>
<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.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" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Unity" Version="5.11.10" />
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
</ItemGroup>
<ItemGroup>
@@ -27,4 +38,10 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,7 @@
namespace RealEstateTransactions.Repositories
{
public interface IConnectionString
{
string ConnectionString { get; }
}
}

View File

@@ -4,14 +4,11 @@ namespace RealEstateTransactions.Repositories
{
public interface IDealRepository
{
IEnumerable<Deal> ReadDeals();
Deal ReadDeal(int id);
IEnumerable<Deal> ReadDeals(DateTime? dateForm = null, DateTime? dateTo = null,
int? servicesID = null, int? dealID = null);
void CreateDeal(Deal deal);
void UpdateDeal(Deal deal);
void DeleteDeal(int id);
}
}

View File

@@ -1,11 +0,0 @@
using RealEstateTransactions.Entities;
namespace RealEstateTransactions.Repositories
{
public interface IServicesDealRepository
{
IEnumerable<ServicesDeal> ReadServicesDeal();
void CreateServicesDeal(ServicesDeal servicesDeal);
}
}

View File

@@ -1,38 +1,125 @@
using RealEstateTransactions.Entities;
using RealEstateTransactions.Entities.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using RealEstateTransactions.Entities;
using Npgsql;
namespace RealEstateTransactions.Repositories.Implementations
{
public class ApartmentRepository : IApartmentRepository
{
public Apartment ReadApartment(int id)
{
return Apartment.CreateApartment(0, AgencyType.None, FormFactorType.None, 0, 0, 0, 0);
}
private readonly IConnectionString _connectionString;
public IEnumerable<Apartment> ReadApartments()
private readonly ILogger<ApartmentRepository> _logger;
public ApartmentRepository(IConnectionString connectionString, ILogger<ApartmentRepository> logger)
{
return [];
_connectionString = connectionString;
_logger = logger;
}
public void CreateApartment(Apartment apartment)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(apartment));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var insertQuery = @"
INSERT INTO Apartment (Agency_ID, Form_factor_ID, Area, Price_per_SM, Base_price, Desired_price)
VALUES (@Agency_ID, @Form_factor_ID, @Area, @Price_per_SM, @Base_price, @Desired_price)";
connection.Execute(insertQuery, apartment);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void UpdateApartment(Apartment apartment)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(apartment));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var updateQuery = @"
UPDATE Apartment
SET
Agency_ID = @Agency_ID,
Form_Factor_ID = @Form_factor_ID,
Area = @Area,
Price_per_SM = @Price_per_SM,
Base_price = @Base_price,
Desired_price = @Desired_price
WHERE Id = @Id";
connection.Execute(updateQuery, apartment);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании оьъекта");
throw;
}
}
public Apartment ReadApartment(int id)
{
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = @"
SELECT *
FROM Apartment
WHERE ID = @id";
var apartment = connection.QueryFirst<Apartment>(selectQuery, new { id });
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(apartment));
return apartment;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Apartment> ReadApartments()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = @"SELECT * FROM Apartment";
var apartments = connection.Query<Apartment>(selectQuery);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(apartments));
return apartments;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public void DeleteApartment(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var deleteQuery = @"
DELETE FROM Apartment
WHERE ID = @id";
connection.Execute(deleteQuery, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public void UpdateApartment(Apartment apartment)
{
}
}
}
}

View File

@@ -1,38 +1,122 @@
using RealEstateTransactions.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using RealEstateTransactions.Entities;
namespace RealEstateTransactions.Repositories.Implementations
{
public class BuyerRepository : IBuyerRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<BuyerRepository> _logger;
public BuyerRepository(IConnectionString connectionString, ILogger<BuyerRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateBuyer(Buyer buyer)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(buyer));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var insertQuery = @"
INSERT INTO Buyer (Full_name, Passport_series, Passport_number)
VALUES (@Full_name, @Passport_series, @Passport_number)";
connection.Execute(insertQuery, buyer);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteBuyer(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var deleteQuery = @"
DELETE FROM Buyer
WHERE Id = @id";
connection.Execute(deleteQuery, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Buyer ReadBuyer(int id)
{
return Buyer.CreateBuyer(0, "", 0, 0);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = @"
SELECT *
FROM Buyer
WHERE Id = @id";
var buyer = connection.QueryFirst<Buyer>(selectQuery, new { id });
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(buyer));
return buyer;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Buyer> ReadBuyers()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = "SELECT * FROM Buyer";
var buyers = connection.Query<Buyer>(selectQuery);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buyers));
return buyers;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public void UpdateBuyer(Buyer buyer)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(buyer));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var updateQuery = @"
UPDATE Buyer
SET
Full_name = @Full_name,
Passport_series = @Passport_series,
Passport_number = @Passport_number
WHERE Id = @Id";
connection.Execute(updateQuery, buyer);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Ошибка при редактировании объекта");
throw;
}
}
}
}
}

View File

@@ -0,0 +1,7 @@
namespace RealEstateTransactions.Repositories.Implementations
{
public class ConnectionString : IConnectionString
{
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=Roma12345;Database=otp";
}
}

View File

@@ -1,32 +1,92 @@
using RealEstateTransactions.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using RealEstateTransactions.Entities;
namespace RealEstateTransactions.Repositories.Implementations
{
public class DealRepository : IDealRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<DealRepository> _logger;
public DealRepository(IConnectionString connectionString, ILogger<DealRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateDeal(Deal deal)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(deal));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
using var transaction = connection.BeginTransaction();
var insertQuery = @"
INSERT INTO Deal (Apartment_ID, Buyer_ID, Deal_price, Deal_date)
VALUES (@Apartment_ID, @Buyer_ID, @Deal_price, @Deal_date);
SELECT MAX(Id) FROM Deal";
var dealId = connection.QueryFirst<int>(insertQuery, deal, transaction);
var subInsertQuery = @"
INSERT INTO Services_Deal (Services_ID, Deal_ID, Execution_time)
VALUES (@Services_ID, @Deal_ID, @Execution_time)";
foreach (var elem in deal.DealServices)
{
connection.Execute(subInsertQuery, new { elem.Services_ID,
Deal_ID = dealId, elem.Execution_time }, transaction);
}
transaction.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteDeal(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var deleteQuery = @"
DELETE FROM Deal
WHERE Id = @id";
connection.Execute(deleteQuery, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Deal ReadDeal(int id)
public IEnumerable<Deal> ReadDeals(DateTime? dateForm = null, DateTime? dateTo = null,
int? servicesID = null, int? dealID = null)
{
return Deal.CreateDeal(0, 0, 0, 0, DateTime.Now, []);
}
public IEnumerable<Deal> ReadDeals()
{
return [];
}
public void UpdateDeal(Deal deal)
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = @"SELECT * FROM Deal";
var deals = connection.Query<Deal>(selectQuery);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deals));
return deals;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}
}
}

View File

@@ -1,28 +1,77 @@
using RealEstateTransactions.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using RealEstateTransactions.Entities;
namespace RealEstateTransactions.Repositories.Implementations
{
public class PreSalesServicesRepository : IPreSalesServicesRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<PreSalesServicesRepository> _logger;
public PreSalesServicesRepository(IConnectionString connectionString, ILogger<PreSalesServicesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreatePreSalesService(PreSalesServices preSalesServices)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(preSalesServices));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var insertQuery = @"
INSERT INTO Pre_Sales_Services (Apartment_ID, Name, Cost)
VALUES (@Apartment_ID, @Name, @Cost)";
connection.Execute(insertQuery, preSalesServices);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeletePreSalesService(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var deleteQuery = @"
DELETE FROM Pre_Sales_Services
WHERE Id = @id";
connection.Execute(deleteQuery, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public IEnumerable<PreSalesServices> ReadPreSalesServices()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = "SELECT * FROM Pre_Sales_Services";
var preSalesServices = connection.Query<PreSalesServices>(selectQuery);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(preSalesServices));
return preSalesServices;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}
}
}

View File

@@ -1,27 +0,0 @@
using RealEstateTransactions.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RealEstateTransactions.Repositories.Implementations
{
public class ServicesDealRepository : IServicesDealRepository
{
public void CreateServicesDeal(ServicesDeal servicesDeal)
{
}
public void DeleteServicesDeal(int id)
{
}
public IEnumerable<ServicesDeal> ReadServicesDeal()
{
return [];
}
}
}

View File

@@ -1,37 +1,121 @@
using RealEstateTransactions.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using RealEstateTransactions.Entities;
namespace RealEstateTransactions.Repositories.Implementations
{
public class ServicesRepository : IServicesRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<ServicesRepository> _logger;
public ServicesRepository(IConnectionString connectionString, ILogger<ServicesRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateService(Services service)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var insertQuery = @"
INSERT INTO Services (Name, Price)
VALUES (@Name, @Price)";
connection.Execute(insertQuery, 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 deleteQuery = @"
DELETE FROM Services
WHERE Id = @id";
connection.Execute(deleteQuery, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Services ReadService(int id)
{
return Services.CreateService(0, "", 0);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {Id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = @"
SELECT *
FROM Services
WHERE Id = @id";
var service = connection.QueryFirst<Services>(selectQuery, new { id });
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(service));
return service;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Services> ReadServices()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = "SELECT * FROM Services";
var services = connection.Query<Services>(selectQuery);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(services));
return services;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public void UpdateService(Services service)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var updateQuery = @"
UPDATE Services
SET
Name = @Name,
Price = @Price
WHERE Id = @Id";
connection.Execute(updateQuery, service);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Ошибка при редактировании объекта");
throw;
}
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:/my/курс 2 сим 1/ОТП/Lab/Log.txt",
"rollingInterval": "Day"
}
}
]
}
}

View File

@@ -1 +0,0 @@