Лабораторная работа №2
This commit is contained in:
parent
a0ab3b7c38
commit
2249ed93ff
@ -1,7 +1,11 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ProjectSchedule.Repositories;
|
using ProjectSchedule.Repositories;
|
||||||
using ProjectSchedule.Repositories.Implementations;
|
using ProjectSchedule.Repositories.Implementations;
|
||||||
|
using Serilog;
|
||||||
using Unity;
|
using Unity;
|
||||||
using Unity.Lifetime;
|
using Unity.Lifetime;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
|
|
||||||
namespace ProjectSchedule
|
namespace ProjectSchedule
|
||||||
{
|
{
|
||||||
@ -23,6 +27,8 @@ namespace ProjectSchedule
|
|||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer();
|
||||||
|
|
||||||
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
|
|
||||||
container.RegisterType<IAudienceRepository, AudienceRepository>(new TransientLifetimeManager());
|
container.RegisterType<IAudienceRepository, AudienceRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<ICurriculumSupplementRepository, CurriculumSupplementRepository>(new TransientLifetimeManager());
|
container.RegisterType<ICurriculumSupplementRepository, CurriculumSupplementRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<IDisciplineRepository, DisciplineRepository>(new TransientLifetimeManager());
|
container.RegisterType<IDisciplineRepository, DisciplineRepository>(new TransientLifetimeManager());
|
||||||
@ -30,7 +36,21 @@ namespace ProjectSchedule
|
|||||||
container.RegisterType<IGroupStudentsRepository, GroupStudentsRepository>(new TransientLifetimeManager());
|
container.RegisterType<IGroupStudentsRepository, GroupStudentsRepository>(new TransientLifetimeManager());
|
||||||
container.RegisterType<ICompilingScheduleRepository, CompilingScheduleRepository>(new TransientLifetimeManager());
|
container.RegisterType<ICompilingScheduleRepository, CompilingScheduleRepository>(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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,24 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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" Version="5.11.10" />
|
||||||
|
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace ProjectSchedule.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; }
|
||||||
|
}
|
@ -1,29 +1,120 @@
|
|||||||
using ProjectSchedule.Entities;
|
using Dapper;
|
||||||
using ProjectSchedule.Entities.Enums;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectSchedule.Entities;
|
||||||
|
|
||||||
namespace ProjectSchedule.Repositories.Implementations;
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
public class AudienceRepository : IAudienceRepository
|
public class AudienceRepository : IAudienceRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<AudienceRepository> _logger;
|
||||||
|
|
||||||
|
public AudienceRepository(IConnectionString connectionString, ILogger<AudienceRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateAudience(Audience audience)
|
public void CreateAudience(Audience audience)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(audience));
|
||||||
public void DeleteAudience(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Audience ReadAudienceById(int id)
|
INSERT INTO Audiences (NumberAudience, TypeAudience, QuantitySeats)
|
||||||
{
|
VALUES (@NumberAudience, @TypeAudience, @QuantitySeats)";
|
||||||
return Audience.CreateEntity(0, string.Empty, TypeAudience.None, 0);
|
connection.Execute(queryInsert, audience);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Audience> ReadAudiences()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateAudience(Audience audience)
|
public void UpdateAudience(Audience audience)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(audience));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Audiences
|
||||||
|
SET
|
||||||
|
NumberAudience=@NumberAudience,
|
||||||
|
TypeAudience=@TypeAudience,
|
||||||
|
QuantitySeats=@QuantitySeats
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, audience);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteAudience(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Audiences
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Audience ReadAudienceById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Audiences
|
||||||
|
WHERE Id=@id";
|
||||||
|
var audience = connection.QueryFirst<Audience>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(audience));
|
||||||
|
return audience;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Audience> ReadAudiences()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Audiences";
|
||||||
|
var audiences = connection.Query<Audience>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(audiences));
|
||||||
|
return audiences;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,16 +1,60 @@
|
|||||||
using ProjectSchedule.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectSchedule.Entities;
|
||||||
|
|
||||||
namespace ProjectSchedule.Repositories.Implementations;
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
public class CompilingScheduleRepository : ICompilingScheduleRepository
|
public class CompilingScheduleRepository : ICompilingScheduleRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<CompilingScheduleRepository> _logger;
|
||||||
|
|
||||||
|
public CompilingScheduleRepository(IConnectionString connectionString, ILogger<CompilingScheduleRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateCompilingSchedule(CompilingSchedule compilingSchedule)
|
public void CreateCompilingSchedule(CompilingSchedule compilingSchedule)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(compilingSchedule));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO CompilingSchedules (EducatorId, DisciplineId, GroupStudentsId, AudienceId, DateDay,
|
||||||
|
TypeWeek, NumberDay, NumberPair, TypeActivity)
|
||||||
|
VALUES (@EducatorId, @DisciplineId, @GroupStudentsId, @AudienceId, @DateDay,
|
||||||
|
@TypeWeek, @NumberDay, @NumberPair, @TypeActivity)";
|
||||||
|
connection.Execute(queryInsert, compilingSchedule);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<CompilingSchedule> ReadCompilingSchedules(DateTime? dateForm = null, DateTime? dateTo = null,
|
public IEnumerable<CompilingSchedule> ReadCompilingSchedules(DateTime? dateForm = null, DateTime? dateTo = null,
|
||||||
int? educatorId = null, int? disciplineId = null, int? groupStudentsId = null, int? audienceId = null)
|
int? educatorId = null, int? disciplineId = null, int? groupStudentsId = null, int? audienceId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM CompilingSchedules";
|
||||||
|
var compilingSchedules = connection.Query<CompilingSchedule>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(compilingSchedules));
|
||||||
|
return compilingSchedules;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
|
internal class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Host=127.0.0.1;Database=schedule;Username=postgres;Password=732005";
|
||||||
|
}
|
@ -1,20 +1,90 @@
|
|||||||
using ProjectSchedule.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectSchedule.Entities;
|
||||||
|
|
||||||
namespace ProjectSchedule.Repositories.Implementations;
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
public class CurriculumSupplementRepository : ICurriculumSupplementRepository
|
public class CurriculumSupplementRepository : ICurriculumSupplementRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<CurriculumSupplementRepository> _logger;
|
||||||
|
|
||||||
|
public CurriculumSupplementRepository(IConnectionString connectionString, ILogger<CurriculumSupplementRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateCurriculumSupplement(CurriculumSupplement curriculumSupplement)
|
public void CreateCurriculumSupplement(CurriculumSupplement curriculumSupplement)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(curriculumSupplement));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO CurriculumSupplements (GroupStudentsId, NameCurriculum, Semester, DateAdoptionPlan)
|
||||||
|
VALUES (@GroupStudentsId, @NameCurriculum, @Semester, @DateAdoptionPlan);
|
||||||
|
SELECT MAX(Id) FROM CurriculumSupplements";
|
||||||
|
var curriculumSupplementId = connection.QueryFirst<int>(queryInsert, curriculumSupplement, transaction);
|
||||||
|
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO DisciplineCurriculumSupplements (CurriculumSupplementId, DisciplineId, QuantityLectures, QuantityPractices)
|
||||||
|
VALUES (@CurriculumSupplementId, @DisciplineId, @QuantityLectures, @QuantityPractices)";
|
||||||
|
foreach (var elem in curriculumSupplement.DisciplineCurriculumSupplements)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new { curriculumSupplementId, elem.DisciplineId, elem.QuantityLectures, elem.QuantityPractices }, transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteCurriculumSupplement(int id)
|
public void DeleteCurriculumSupplement(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM CurriculumSupplements
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<CurriculumSupplement> ReadCurriculumSupplements(DateTime? dateForm = null, DateTime? dateTo = null,
|
public IEnumerable<CurriculumSupplement> ReadCurriculumSupplements(DateTime? dateForm = null, DateTime? dateTo = null,
|
||||||
int? disciplineId = null, int? groupStudentsId = null)
|
int? disciplineId = null, int? groupStudentsId = null)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM CurriculumSupplements";
|
||||||
|
var curriculumSupplements = connection.Query<CurriculumSupplement>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(curriculumSupplements));
|
||||||
|
return curriculumSupplements;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +1,118 @@
|
|||||||
using ProjectSchedule.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectSchedule.Entities;
|
||||||
|
|
||||||
namespace ProjectSchedule.Repositories.Implementations;
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
public class DisciplineRepository : IDisciplineRepository
|
public class DisciplineRepository : IDisciplineRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<DisciplineRepository> _logger;
|
||||||
|
|
||||||
|
public DisciplineRepository(IConnectionString connectionString, ILogger<DisciplineRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateDiscipline(Discipline discipline)
|
public void CreateDiscipline(Discipline discipline)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(discipline));
|
||||||
public void DeleteDiscipline(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Discipline ReadDisciplineById(int id)
|
INSERT INTO Disciplines (NameDiscipline)
|
||||||
{
|
VALUES (@NameDiscipline)";
|
||||||
return Discipline.CreateEntity(0, string.Empty);
|
connection.Execute(queryInsert, discipline);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Discipline> ReadDisciplines()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateDiscipline(Discipline discipline)
|
public void UpdateDiscipline(Discipline discipline)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(discipline));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Disciplines
|
||||||
|
SET
|
||||||
|
NameDiscipline=@NameDiscipline
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, discipline);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteDiscipline(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Disciplines
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Discipline ReadDisciplineById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Disciplines
|
||||||
|
WHERE Id=@id";
|
||||||
|
var discipline = connection.QueryFirst<Discipline>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(discipline));
|
||||||
|
return discipline;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Discipline> ReadDisciplines()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Disciplines";
|
||||||
|
var disciplines = connection.Query<Discipline>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(disciplines));
|
||||||
|
return disciplines;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +1,120 @@
|
|||||||
using ProjectSchedule.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectSchedule.Entities;
|
||||||
|
|
||||||
namespace ProjectSchedule.Repositories.Implementations;
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
public class EducatorRepository : IEducatorRepository
|
public class EducatorRepository : IEducatorRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<EducatorRepository> _logger;
|
||||||
|
|
||||||
|
public EducatorRepository(IConnectionString connectionString, ILogger<EducatorRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateEducator(Educator educator)
|
public void CreateEducator(Educator educator)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(educator));
|
||||||
public void DeleteEducator(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public Educator ReadEducatorById(int id)
|
INSERT INTO Educators (Surname, Name, Patronymic)
|
||||||
{
|
VALUES (@Surname, @Name, @Patronymic)";
|
||||||
return Educator.CreateEntity(0, string.Empty, string.Empty, string.Empty);
|
connection.Execute(queryInsert, educator);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<Educator> ReadEducators()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateEducator(Educator educator)
|
public void UpdateEducator(Educator educator)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(educator));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Educators
|
||||||
|
SET
|
||||||
|
Surname=@Surname,
|
||||||
|
Name=@Name,
|
||||||
|
Patronymic=@Patronymic
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, educator);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteEducator(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Educators
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Educator ReadEducatorById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Educators
|
||||||
|
WHERE Id=@id";
|
||||||
|
var educator = connection.QueryFirst<Educator>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(educator));
|
||||||
|
return educator;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Educator> ReadEducators()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Educators";
|
||||||
|
var educators = connection.Query<Educator>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(educators));
|
||||||
|
return educators;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +1,120 @@
|
|||||||
using ProjectSchedule.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectSchedule.Entities;
|
||||||
|
|
||||||
namespace ProjectSchedule.Repositories.Implementations;
|
namespace ProjectSchedule.Repositories.Implementations;
|
||||||
|
|
||||||
public class GroupStudentsRepository : IGroupStudentsRepository
|
public class GroupStudentsRepository : IGroupStudentsRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
|
private readonly ILogger<GroupStudentsRepository> _logger;
|
||||||
|
|
||||||
|
public GroupStudentsRepository(IConnectionString connectionString, ILogger<GroupStudentsRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateGroupStudents(GroupStudents groupStudents)
|
public void CreateGroupStudents(GroupStudents groupStudents)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(groupStudents));
|
||||||
public void DeleteGroupStudents(int id)
|
try
|
||||||
{
|
{
|
||||||
}
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
public GroupStudents ReadGroupStudentsById(int id)
|
INSERT INTO GroupsStudents (AbbreviationGroup, GroupNumber, QuantityStudents)
|
||||||
{
|
VALUES (@AbbreviationGroup, @GroupNumber, @QuantityStudents)";
|
||||||
return GroupStudents.CreateEntity(0, string.Empty, string.Empty, 0);
|
connection.Execute(queryInsert, groupStudents);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public IEnumerable<GroupStudents> ReadGroupsStudents()
|
{
|
||||||
{
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
return [];
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateGroupStudents(GroupStudents groupStudents)
|
public void UpdateGroupStudents(GroupStudents groupStudents)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(groupStudents));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE GroupsStudents
|
||||||
|
SET
|
||||||
|
AbbreviationGroup=@AbbreviationGroup,
|
||||||
|
GroupNumber=@GroupNumber,
|
||||||
|
QuantityStudents=@QuantityStudents
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, groupStudents);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteGroupStudents(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM GroupsStudents
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupStudents ReadGroupStudentsById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM GroupsStudents
|
||||||
|
WHERE Id=@id";
|
||||||
|
var groupStudents = connection.QueryFirst<GroupStudents>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(groupStudents));
|
||||||
|
return groupStudents;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<GroupStudents> ReadGroupsStudents()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM GroupsStudents";
|
||||||
|
var groupsStudents = connection.Query<GroupStudents>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(groupsStudents));
|
||||||
|
return groupsStudents;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
ProjectSchedule/ProjectSchedule/appsettings.json
Normal file
15
ProjectSchedule/ProjectSchedule/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs/schedule_log.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user