Лабораторная работа №2
This commit is contained in:
parent
5112c074d3
commit
745617d8b5
@ -41,7 +41,7 @@
|
|||||||
panel1.Controls.Add(buttonDel);
|
panel1.Controls.Add(buttonDel);
|
||||||
panel1.Controls.Add(buttonAdd);
|
panel1.Controls.Add(buttonAdd);
|
||||||
panel1.Dock = DockStyle.Right;
|
panel1.Dock = DockStyle.Right;
|
||||||
panel1.Location = new Point(712, 0);
|
panel1.Location = new Point(792, 0);
|
||||||
panel1.Name = "panel1";
|
panel1.Name = "panel1";
|
||||||
panel1.Size = new Size(122, 361);
|
panel1.Size = new Size(122, 361);
|
||||||
panel1.TabIndex = 0;
|
panel1.TabIndex = 0;
|
||||||
@ -80,14 +80,14 @@
|
|||||||
dataGridView.Name = "dataGridView";
|
dataGridView.Name = "dataGridView";
|
||||||
dataGridView.RowHeadersVisible = false;
|
dataGridView.RowHeadersVisible = false;
|
||||||
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridView.Size = new Size(712, 361);
|
dataGridView.Size = new Size(792, 361);
|
||||||
dataGridView.TabIndex = 1;
|
dataGridView.TabIndex = 1;
|
||||||
//
|
//
|
||||||
// FormServicesProvision
|
// FormServicesProvision
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(834, 361);
|
ClientSize = new Size(914, 361);
|
||||||
Controls.Add(dataGridView);
|
Controls.Add(dataGridView);
|
||||||
Controls.Add(panel1);
|
Controls.Add(panel1);
|
||||||
Name = "FormServicesProvision";
|
Name = "FormServicesProvision";
|
||||||
|
@ -2,6 +2,10 @@ using ProjectAthletesAccommodation.Repositories.Implementations;
|
|||||||
using ProjectAthletesAccommodation.Repositories;
|
using ProjectAthletesAccommodation.Repositories;
|
||||||
using Unity.Lifetime;
|
using Unity.Lifetime;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
|
||||||
namespace ProjectAthletesAccommodation
|
namespace ProjectAthletesAccommodation
|
||||||
{
|
{
|
||||||
@ -23,13 +27,29 @@ namespace ProjectAthletesAccommodation
|
|||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
|
|
||||||
container.RegisterType<IAthleteRepository, AthleteRepository>(new TransientLifetimeManager());
|
container.RegisterType<IAthleteRepository, AthleteRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IHotelRepository, HotelRepository>(new TransientLifetimeManager());
|
container.RegisterType<IHotelRepository, HotelRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IRoomRepository, RoomRepository>(new TransientLifetimeManager());
|
container.RegisterType<IRoomRepository, RoomRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IAthleteAccommodationRepository, AthleteAccommodationRepository>(new TransientLifetimeManager());
|
container.RegisterType<IAthleteAccommodationRepository, AthleteAccommodationRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IServiceProvisionRepository, ServiceProvisionRepository>(new TransientLifetimeManager());
|
container.RegisterType<IServiceProvisionRepository, ServiceProvisionRepository>(new TransientLifetimeManager());
|
||||||
|
|
||||||
|
container.RegisterType<IConnectionString, ConnectionString>(new SingletonLifetimeManager());
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -13,7 +13,19 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="Npgsql" Version="9.0.1" />
|
||||||
|
<PackageReference Include="Serilog" Version="4.0.2" />
|
||||||
|
<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="System.Data.SqlClient" Version="4.9.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>
|
||||||
@ -31,4 +43,10 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace ProjectAthletesAccommodation.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; }
|
||||||
|
}
|
@ -1,19 +1,86 @@
|
|||||||
using ProjectAthletesAccommodation.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectAthletesAccommodation.Entities;
|
||||||
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
||||||
|
|
||||||
public class AthleteAccommodationRepository : IAthleteAccommodationRepository
|
public class AthleteAccommodationRepository : IAthleteAccommodationRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<AthleteAccommodationRepository> _logger;
|
||||||
|
|
||||||
|
public AthleteAccommodationRepository(IConnectionString connectionString,
|
||||||
|
ILogger<AthleteAccommodationRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateAthleteAccommodation(AthleteAccommodation athleteAccommodation)
|
public void CreateAthleteAccommodation(AthleteAccommodation athleteAccommodation)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(athleteAccommodation));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryInsert =
|
||||||
|
@"INSERT INTO AthleteAccommodation (RoomId, AthleteId, DateStart, DateEnd)
|
||||||
|
VALUES (@RoomId, @AthleteId, @DateStart, @DateEnd)";
|
||||||
|
|
||||||
|
connection.Execute(queryInsert, athleteAccommodation);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteAthleteAccommodation(int id)
|
public void DeleteAthleteAccommodation(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryDelete = @"DELETE FROM AthleteAccommodation WHERE Id=@id";
|
||||||
|
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<AthleteAccommodation> ReadAthleteAccommodation(DateTime? dateFrom = null, DateTime? dateTo = null,
|
public IEnumerable<AthleteAccommodation> ReadAthleteAccommodation(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||||
int? roomId = null, int? athleteId = null)
|
int? roomId = null, int? athleteId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = "SELECT * FROM AthleteAccommodation";
|
||||||
|
|
||||||
|
var athleteAccommodation = connection.Query<AthleteAccommodation>(querySelect);
|
||||||
|
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(athleteAccommodation));
|
||||||
|
|
||||||
|
return athleteAccommodation;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +1,133 @@
|
|||||||
using ProjectAthletesAccommodation.Entities.Enums;
|
using ProjectAthletesAccommodation.Entities;
|
||||||
using ProjectAthletesAccommodation.Entities;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Dapper;
|
||||||
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
||||||
|
|
||||||
public class AthleteRepository : IAthleteRepository
|
public class AthleteRepository : IAthleteRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<AthleteRepository> _logger;
|
||||||
|
|
||||||
|
public AthleteRepository(IConnectionString connectionString, ILogger<AthleteRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateAthlete(Athlete athlete)
|
public void CreateAthlete(Athlete athlete)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(athlete));
|
||||||
|
|
||||||
public void DeleteAthlete(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
public Athlete ReadAthleteById(int id)
|
var queryInsert =
|
||||||
{
|
@"INSERT INTO Athlete (Name, Surname, KindOfSport, AthleteClassification)
|
||||||
return Athlete.CreateEntity(0, string.Empty, string.Empty, KindOfSport.None, AthleteClassification.None);
|
VALUES (@Name, @Surname, @KindOfSport, @AthleteClassification)";
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<Athlete> ReadAthletes()
|
connection.Execute(queryInsert, athlete);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateAthlete(Athlete athlete)
|
public void UpdateAthlete(Athlete athlete)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(athlete));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"UPDATE Athlete SET
|
||||||
|
Name=@Name,
|
||||||
|
Surname=@Surname,
|
||||||
|
KindOfSport=@KindOfSport,
|
||||||
|
AthleteClassification=@AthleteClassification
|
||||||
|
WHERE Id=@Id";
|
||||||
|
|
||||||
|
connection.Execute(queryUpdate, athlete);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteAthlete(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryDelete = @"DELETE FROM Athlete WHERE Id=@id";
|
||||||
|
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Athlete ReadAthleteById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = @"SELECT * FROM Athlete WHERE Id=@id";
|
||||||
|
|
||||||
|
var ahlete = connection.QueryFirst<Athlete>(querySelect, new { id });
|
||||||
|
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(ahlete));
|
||||||
|
|
||||||
|
return ahlete;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Athlete> ReadAthletes()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = "SELECT * FROM Athlete";
|
||||||
|
|
||||||
|
var ahletes = connection.Query<Athlete>(querySelect);
|
||||||
|
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(ahletes));
|
||||||
|
|
||||||
|
return ahletes;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString
|
||||||
|
=> "Host=localhost;Port=5432;Database=accommodation;Username=postgres;Password=postgres";
|
||||||
|
}
|
@ -1,27 +1,128 @@
|
|||||||
using ProjectAthletesAccommodation.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectAthletesAccommodation.Entities;
|
||||||
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
||||||
|
|
||||||
public class HotelRepository : IHotelRepository
|
public class HotelRepository : IHotelRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<HotelRepository> _logger;
|
||||||
|
|
||||||
|
public HotelRepository(IConnectionString connectionString, ILogger<HotelRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateHotel(Hotel hotel)
|
public void CreateHotel(Hotel hotel)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(hotel));
|
||||||
|
|
||||||
public void DeleteHotel(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
public Hotel ReadHotelById(int id)
|
var queryInsert = @"INSERT INTO Hotel (Name) VALUES (@Name)";
|
||||||
{
|
|
||||||
return Hotel.CreateEntity(0, string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<Hotel> ReadHotels()
|
connection.Execute(queryInsert, hotel);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateHotel(Hotel hotel)
|
public void UpdateHotel(Hotel hotel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(hotel));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"UPDATE Hotel SET
|
||||||
|
Name=@Name
|
||||||
|
WHERE Id=@Id";
|
||||||
|
|
||||||
|
connection.Execute(queryUpdate, hotel);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteHotel(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryDelete = @"DELETE FROM Hotel WHERE Id=@id";
|
||||||
|
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Hotel ReadHotelById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = @"SELECT * FROM Hotel WHERE Id=@id";
|
||||||
|
|
||||||
|
var hotel = connection.QueryFirst<Hotel>(querySelect, new { id });
|
||||||
|
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(hotel));
|
||||||
|
|
||||||
|
return hotel;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Hotel> ReadHotels()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = "SELECT * FROM Hotel";
|
||||||
|
|
||||||
|
var hotels = connection.Query<Hotel>(querySelect);
|
||||||
|
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(hotels));
|
||||||
|
|
||||||
|
return hotels;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +1,132 @@
|
|||||||
using ProjectAthletesAccommodation.Entities.Enums;
|
using ProjectAthletesAccommodation.Entities;
|
||||||
using ProjectAthletesAccommodation.Entities;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using Dapper;
|
||||||
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
||||||
|
|
||||||
public class RoomRepository : IRoomRepository
|
public class RoomRepository : IRoomRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<RoomRepository> _logger;
|
||||||
|
|
||||||
|
public RoomRepository(IConnectionString connectionString, ILogger<RoomRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateRoom(Room room)
|
public void CreateRoom(Room room)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(room));
|
||||||
|
|
||||||
public void DeleteRoom(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
public Room ReadRoomById(int id)
|
var queryInsert =
|
||||||
{
|
@"INSERT INTO Room (HotelId, Name, TypeOfRoom)
|
||||||
return Room.CreateEntity(0, 0, string.Empty, TypeOfRoom.None);
|
VALUES (@HotelId, @Name, @TypeOfRoom)";
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<Room> ReadRooms()
|
connection.Execute(queryInsert, room);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateRoom(Room room)
|
public void UpdateRoom(Room room)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(room));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryUpdate = @"UPDATE Room SET
|
||||||
|
HotelId=@HotelId,
|
||||||
|
Name=@Name,
|
||||||
|
TypeOfRoom=@TypeOfRoom
|
||||||
|
WHERE Id=@Id";
|
||||||
|
|
||||||
|
connection.Execute(queryUpdate, room);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteRoom(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryDelete = @"DELETE FROM Room WHERE Id=@id";
|
||||||
|
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Room ReadRoomById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = @"SELECT * FROM Room WHERE Id=@id";
|
||||||
|
|
||||||
|
var room = connection.QueryFirst<Room>(querySelect, new { id });
|
||||||
|
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(room));
|
||||||
|
|
||||||
|
return room;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Room> ReadRooms()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = "SELECT * FROM Room";
|
||||||
|
|
||||||
|
var rooms = connection.Query<Room>(querySelect);
|
||||||
|
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(rooms));
|
||||||
|
|
||||||
|
return rooms;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,19 +1,102 @@
|
|||||||
using ProjectAthletesAccommodation.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectAthletesAccommodation.Entities;
|
||||||
|
using Serilog.Core;
|
||||||
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
namespace ProjectAthletesAccommodation.Repositories.Implementations;
|
||||||
|
|
||||||
public class ServiceProvisionRepository : IServiceProvisionRepository
|
public class ServiceProvisionRepository : IServiceProvisionRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<ServiceProvisionRepository> _logger;
|
||||||
|
|
||||||
|
public ServiceProvisionRepository(IConnectionString connectionString,
|
||||||
|
ILogger<ServiceProvisionRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateServiceProvision(ServiceProvision serviceProvision)
|
public void CreateServiceProvision(ServiceProvision serviceProvision)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(serviceProvision));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
|
||||||
|
var queryInsert =
|
||||||
|
@"INSERT INTO ServiceProvision (HotelId, Name, Date)
|
||||||
|
VALUES (@HotelId, @Name, @Date);
|
||||||
|
SELECT MAX(Id) FROM ServiceProvision";
|
||||||
|
|
||||||
|
var serviceProvisionId = connection.QueryFirst<int>(queryInsert, serviceProvision, transaction);
|
||||||
|
|
||||||
|
var querySubInsert =
|
||||||
|
@"INSERT INTO ServiceProvisionConnection (ServiceProvisionId, AthleteId, Price)
|
||||||
|
VALUES (@ServiceProvisionId, @AthleteId, @Price)";
|
||||||
|
|
||||||
|
foreach (var elem in serviceProvision.ServiceProvisionConnection)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new { serviceProvisionId, elem.AthleteId, elem.Price }, transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteServiceProvision(int id)
|
public void DeleteServiceProvision(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryDelete = @"DELETE FROM ServiceProvision WHERE Id=@id";
|
||||||
|
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ServiceProvision> ReadServiceProvision(DateTime? dateFrom = null, DateTime? dateTo = null,
|
public IEnumerable<ServiceProvision> ReadServiceProvision(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||||
int? hotelId = null, int? athleteId = null)
|
int? hotelId = null, int? athleteId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var querySelect = @"SELECT * FROM ServiceProvision";
|
||||||
|
|
||||||
|
var serviceProvision = connection.Query<ServiceProvision>(querySelect);
|
||||||
|
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(serviceProvision));
|
||||||
|
|
||||||
|
return serviceProvision;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/accomodation_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user