LabWork02
This commit is contained in:
parent
a369e88614
commit
4656445077
@ -10,9 +10,9 @@ public enum JockeyTitle
|
|||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
|
|
||||||
SecondCategory = 1,
|
FirstCategory = 1,
|
||||||
|
|
||||||
FirstCategory = 2,
|
SecondCategory = 2,
|
||||||
|
|
||||||
Master = 3
|
Master = 3
|
||||||
}
|
}
|
||||||
|
2
ProjectHorseRacingOrg/Forms/FormRace.Designer.cs
generated
2
ProjectHorseRacingOrg/Forms/FormRace.Designer.cs
generated
@ -81,6 +81,7 @@
|
|||||||
buttonSave.TabIndex = 4;
|
buttonSave.TabIndex = 4;
|
||||||
buttonSave.Text = "Сохранить";
|
buttonSave.Text = "Сохранить";
|
||||||
buttonSave.UseVisualStyleBackColor = true;
|
buttonSave.UseVisualStyleBackColor = true;
|
||||||
|
buttonSave.Click += ButtonSave_Click;
|
||||||
//
|
//
|
||||||
// buttonCancel
|
// buttonCancel
|
||||||
//
|
//
|
||||||
@ -90,6 +91,7 @@
|
|||||||
buttonCancel.TabIndex = 5;
|
buttonCancel.TabIndex = 5;
|
||||||
buttonCancel.Text = "Отмена";
|
buttonCancel.Text = "Отмена";
|
||||||
buttonCancel.UseVisualStyleBackColor = true;
|
buttonCancel.UseVisualStyleBackColor = true;
|
||||||
|
buttonCancel.Click += ButtonCancel_Click;
|
||||||
//
|
//
|
||||||
// textBoxName
|
// textBoxName
|
||||||
//
|
//
|
||||||
|
@ -61,7 +61,7 @@ namespace ProjectHorseRacingOrg.Forms
|
|||||||
var list = new List<RaceEntryDetails>();
|
var list = new List<RaceEntryDetails>();
|
||||||
foreach (DataGridViewRow row in dataGridView.Rows)
|
foreach (DataGridViewRow row in dataGridView.Rows)
|
||||||
{
|
{
|
||||||
if (row.Cells["ColumnFeed"].Value == null ||
|
if (row.Cells["ColumnRace"].Value == null ||
|
||||||
row.Cells["ColumnCount"].Value == null)
|
row.Cells["ColumnCount"].Value == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
@ -2,6 +2,10 @@ using Unity.Lifetime;
|
|||||||
using Unity;
|
using Unity;
|
||||||
using ProjectHorseRacingOrg.Repositories;
|
using ProjectHorseRacingOrg.Repositories;
|
||||||
using ProjectHorseRacingOrg.Repositories.Implementations;
|
using ProjectHorseRacingOrg.Repositories.Implementations;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace ProjectHorseRacingOrg
|
namespace ProjectHorseRacingOrg
|
||||||
{
|
{
|
||||||
@ -22,17 +26,32 @@ namespace ProjectHorseRacingOrg
|
|||||||
private static IUnityContainer CreateContainer()
|
private static IUnityContainer CreateContainer()
|
||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
container.RegisterType<IJockeyRepository,
|
|
||||||
JockeyRepository>(new TransientLifetimeManager());
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
container.RegisterType<IHorseRepository,
|
|
||||||
HorseRepository>(new TransientLifetimeManager());
|
container.RegisterType<IJockeyRepository, JockeyRepository>(new TransientLifetimeManager());
|
||||||
|
container.RegisterType<IHorseRepository, HorseRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IRaceRepository, RaceRepository>(new TransientLifetimeManager());
|
container.RegisterType<IRaceRepository, RaceRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IRacingHorsesRepository,
|
container.RegisterType<IRacingHorsesRepository, RacingHorsesRepository>(new TransientLifetimeManager());
|
||||||
RacingHorsesRepository>(new TransientLifetimeManager());
|
container.RegisterType<IRaceEntriesRepository, RaceEntriesRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IRaceEntriesRepository,
|
|
||||||
RaceEntriesRepository>(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()
|
||||||
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile("appsettings.json")
|
||||||
|
.Build())
|
||||||
|
.CreateLogger());
|
||||||
|
return loggerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,20 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||||
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" 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="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>
|
||||||
|
13
ProjectHorseRacingOrg/Repositories/IConnectionString.cs
Normal file
13
ProjectHorseRacingOrg/Repositories/IConnectionString.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectHorseRacingOrg.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
string ConnectionString { get; }
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
||||||
|
|
||||||
|
internal class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Server=localhost, 5432;Database=racinghorse;Uid=postgres;Pwd=postgres;";
|
||||||
|
}
|
@ -1,36 +1,146 @@
|
|||||||
using ProjectHorseRacingOrg.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.VisualBasic.Devices;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using ProjectHorseRacingOrg.Entities;
|
||||||
|
using Serilog.Core;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
||||||
|
|
||||||
public class HorseRepository : IHorseRepository
|
public class HorseRepository : IHorseRepository
|
||||||
|
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<HorseRepository> _logger;
|
||||||
|
|
||||||
|
public HorseRepository(IConnectionString connectionString, ILogger<HorseRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
|
||||||
|
}
|
||||||
public void CreateHorse(Horse horse)
|
public void CreateHorse(Horse horse)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(horse));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Horses (HorseSpecies, HorseNickName, Age, Weight)
|
||||||
|
VALUES (@HorseSpecies, @HorseNickName, @Age, @Weight)";
|
||||||
|
connection.Execute(queryInsert, horse);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public void UpdateHorse(Horse horse)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(horse));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Horses
|
||||||
|
SET
|
||||||
|
HorseSpecies=@HorseSpecies,
|
||||||
|
HorseNickName=@HorseNickName,
|
||||||
|
Age=@Age,
|
||||||
|
Weight=@Weight
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, horse);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteHorse(int id)
|
public void DeleteHorse(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Horses
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Horse ReadHorseById(int id)
|
public Horse ReadHorseById(int id)
|
||||||
{
|
{
|
||||||
return Horse.CreateEntity(0, string.Empty, string.Empty, 0, 0);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Horses
|
||||||
|
WHERE Id=@id";
|
||||||
|
var horse = connection.QueryFirst<Horse>(querySelect, new
|
||||||
|
{
|
||||||
|
id
|
||||||
|
});
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(horse));
|
||||||
|
return horse;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Horse> ReadHorses()
|
public IEnumerable<Horse> ReadHorses()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
}
|
try
|
||||||
|
{
|
||||||
public void UpdateHorse(Horse horse)
|
using var connection = new
|
||||||
{
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Horses";
|
||||||
|
var horses = connection.Query<Horse>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(horses));
|
||||||
|
return horses;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectHorseRacingOrg.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectHorseRacingOrg.Entities;
|
||||||
using ProjectHorseRacingOrg.Entities.Enums;
|
using ProjectHorseRacingOrg.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,29 +14,129 @@ namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
|||||||
|
|
||||||
public class JockeyRepository : IJockeyRepository
|
public class JockeyRepository : IJockeyRepository
|
||||||
{
|
{
|
||||||
public void CreateJockey(Jockey jockey)
|
private readonly IConnectionString _connectionString;
|
||||||
{
|
|
||||||
|
|
||||||
|
private readonly ILogger<JockeyRepository> _logger;
|
||||||
|
|
||||||
|
public JockeyRepository(IConnectionString connectionString, ILogger<JockeyRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CreateJockey(Jockey jockey)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(jockey));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Jockeys (FirstName, LastName, JockeyTitle)
|
||||||
|
VALUES (@FirstName, @LastName, @JockeyTitle)";
|
||||||
|
connection.Execute(queryInsert, jockey);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public void UpdateJockey(Jockey jockey)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
|
||||||
|
JsonConvert.SerializeObject(jockey));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Jockeys
|
||||||
|
SET
|
||||||
|
FirstName=@FirstName,
|
||||||
|
LastName=@LastName,
|
||||||
|
JockeyTitle=@JockeyTitle
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, jockey);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
public void DeleteJockey(int id)
|
public void DeleteJockey(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Jockeys
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Jockey ReadJockeyById(int id)
|
public Jockey ReadJockeyById(int id)
|
||||||
{
|
{
|
||||||
return Jockey.CreateEntity(0, string.Empty, string.Empty, JockeyTitle.None);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Jockeys
|
||||||
|
WHERE Id=@id";
|
||||||
|
var jockey = connection.QueryFirst<Jockey>(querySelect,
|
||||||
|
new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(jockey));
|
||||||
|
return jockey;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Jockey> ReadJockeys()
|
public IEnumerable<Jockey> ReadJockeys()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Jockeys";
|
||||||
|
var jockeys = connection.Query<Jockey>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(jockeys));
|
||||||
|
return jockeys;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateJockey(Jockey jockey)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
using ProjectHorseRacingOrg.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectHorseRacingOrg.Entities;
|
||||||
|
using Serilog.Core;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,18 +14,94 @@ namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
|||||||
|
|
||||||
public class RaceEntriesRepository : IRaceEntriesRepository
|
public class RaceEntriesRepository : IRaceEntriesRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<RaceEntriesRepository> _logger;
|
||||||
|
public RaceEntriesRepository(IConnectionString connectionString,
|
||||||
|
ILogger<RaceEntriesRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void CreateRaceEntries(RaceEntries raceEntries)
|
public void CreateRaceEntries(RaceEntries raceEntries)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(raceEntries));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO RaceEntries (JockeyId, DateReceipt)
|
||||||
|
VALUES (@JockeyId, @DateReceipt);
|
||||||
|
SELECT MAX(Id) FROM RaceEntries";
|
||||||
|
var raceEntryId =
|
||||||
|
connection.QueryFirst<int>(queryInsert, raceEntries, transaction);
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO RaceEntryDetails (RaceEntryId, RaceId, Count)
|
||||||
|
VALUES (@RaceEntryId,@RaceId, @Count)";
|
||||||
|
foreach (var elem in raceEntries.RaceEntryDetails)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new
|
||||||
|
{
|
||||||
|
raceEntryId,
|
||||||
|
elem.RaceId,
|
||||||
|
elem.Count
|
||||||
|
}, transaction);
|
||||||
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteRaceEntries(int id)
|
public void DeleteRaceEntries(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM RaceEntries
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<RaceEntries> ReadRaceEntries(DateTime? dateForm = null, DateTime? dateTo = null, int? raceId = null, int? jockeyId = null)
|
public IEnumerable<RaceEntries> ReadRaceEntries(DateTime? dateForm = null, DateTime? dateTo = null, int? raceId = null, int? jockeyId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM RaceEntries";
|
||||||
|
var raceEntries =
|
||||||
|
connection.Query<RaceEntries>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(raceEntries));
|
||||||
|
return raceEntries;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
using Microsoft.VisualBasic.FileIO;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.VisualBasic.FileIO;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
using ProjectHorseRacingOrg.Entities;
|
using ProjectHorseRacingOrg.Entities;
|
||||||
using ProjectHorseRacingOrg.Entities.Enums;
|
using ProjectHorseRacingOrg.Entities.Enums;
|
||||||
|
using Serilog.Core;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -11,30 +17,131 @@ namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
|||||||
|
|
||||||
public class RaceRepository : IRaceRepository
|
public class RaceRepository : IRaceRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<RaceRepository> _logger;
|
||||||
|
|
||||||
|
public RaceRepository(IConnectionString connectionString,
|
||||||
|
ILogger<RaceRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void CreateRace(Race race)
|
public void CreateRace(Race race)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
}
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(race));
|
||||||
public void DeleteRace(int id)
|
try
|
||||||
{
|
{
|
||||||
|
using var connection = new
|
||||||
}
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Race ReadRaceById(int id)
|
INSERT INTO Races (RaceType, Name, Description)
|
||||||
{
|
VALUES (@RaceType, @Name, @Description)";
|
||||||
return Race.CreateEntity(0, RaceType.None, string.Empty, string.Empty);
|
connection.Execute(queryInsert, race);
|
||||||
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
|
{
|
||||||
public IEnumerable<Race> ReadRaces()
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
{
|
throw;
|
||||||
return [];
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateRace(Race race)
|
public void UpdateRace(Race race)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(race));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Races
|
||||||
|
SET
|
||||||
|
RaceType=@RaceType,
|
||||||
|
Name=@Name,
|
||||||
|
Description=@Description
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, race);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DeleteRace(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Races
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Race ReadRaceById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Races
|
||||||
|
WHERE Id=@id";
|
||||||
|
var race = connection.QueryFirst<Race>(querySelect, new
|
||||||
|
{
|
||||||
|
id
|
||||||
|
});
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(race));
|
||||||
|
return race;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Race> ReadRaces()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Races";
|
||||||
|
var races = connection.Query<Race>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(races));
|
||||||
|
return races;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectHorseRacingOrg.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectHorseRacingOrg.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,13 +13,55 @@ namespace ProjectHorseRacingOrg.Repositories.Implementations;
|
|||||||
|
|
||||||
public class RacingHorsesRepository : IRacingHorsesRepository
|
public class RacingHorsesRepository : IRacingHorsesRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<RacingHorsesRepository> _logger;
|
||||||
|
public RacingHorsesRepository(IConnectionString connectionString,
|
||||||
|
ILogger<RacingHorsesRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreateRacingHorse(RacingHorses racingHorses)
|
public void CreateRacingHorse(RacingHorses racingHorses)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(racingHorses));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO RacingHorses (RaceId, JockeyId, HorseId, RacingDate, Result)
|
||||||
|
VALUES (@RaceId, @JockeyId, @HorseId, @RacingDate, @Result)";
|
||||||
|
connection.Execute(queryInsert, racingHorses);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<RacingHorses> ReadRacingHorses(DateTime? dateForm = null, DateTime? dateTo = null, int? raceId = null, int? jockeyId = null, int? horseId = null)
|
public IEnumerable<RacingHorses> ReadRacingHorses(DateTime? dateForm = null, DateTime? dateTo = null, int? raceId = null, int? jockeyId = null, int? horseId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM RacingHorses";
|
||||||
|
var racingHorses =
|
||||||
|
connection.Query<RacingHorses>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(racingHorses));
|
||||||
|
return racingHorses;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
ProjectHorseRacingOrg/appsettings.json
Normal file
15
ProjectHorseRacingOrg/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/HorseRacing_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user