From 09d4234288d7d9b39cd545b703ae78cbdc0c27e6 Mon Sep 17 00:00:00 2001 From: artur-kalimullin <144933634+artur-kalimullin@users.noreply.github.com> Date: Wed, 20 Nov 2024 18:06:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectSchedule/ProjectSchedule/Program.cs | 20 +++ .../ProjectSchedule/ProjectSchedule.csproj | 17 +++ .../Repositories/IConnectionString.cs | 6 + .../Implementations/AudienceRepository.cs | 123 +++++++++++++++--- .../CompilingScheduleRepository.cs | 48 ++++++- .../Implementations/ConnectionString.cs | 6 + .../CurriculumSupplementRepository.cs | 74 ++++++++++- .../Implementations/DisciplineRepository.cs | 120 ++++++++++++++--- .../Implementations/EducatorRepository.cs | 122 ++++++++++++++--- .../GroupStudentsRepository.cs | 122 ++++++++++++++--- .../ProjectSchedule/appsettings.json | 15 +++ 11 files changed, 608 insertions(+), 65 deletions(-) create mode 100644 ProjectSchedule/ProjectSchedule/Repositories/IConnectionString.cs create mode 100644 ProjectSchedule/ProjectSchedule/Repositories/Implementations/ConnectionString.cs create mode 100644 ProjectSchedule/ProjectSchedule/appsettings.json diff --git a/ProjectSchedule/ProjectSchedule/Program.cs b/ProjectSchedule/ProjectSchedule/Program.cs index 8bcc656..a3a8d0f 100644 --- a/ProjectSchedule/ProjectSchedule/Program.cs +++ b/ProjectSchedule/ProjectSchedule/Program.cs @@ -2,6 +2,10 @@ using Unity.Lifetime; using Unity; using ProjectSchedule.Repositories; using ProjectSchedule.Repositories.Implementations; +using Microsoft.Extensions.Logging; +using Serilog; +using Microsoft.Extensions.Configuration; +using Unity.Microsoft.Logging; namespace ProjectSchedule { @@ -23,6 +27,8 @@ namespace ProjectSchedule { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); @@ -30,7 +36,21 @@ namespace ProjectSchedule container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(new SingletonLifetimeManager()); + 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; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/ProjectSchedule.csproj b/ProjectSchedule/ProjectSchedule/ProjectSchedule.csproj index accbdf0..e3c2e4e 100644 --- a/ProjectSchedule/ProjectSchedule/ProjectSchedule.csproj +++ b/ProjectSchedule/ProjectSchedule/ProjectSchedule.csproj @@ -9,7 +9,18 @@ + + + + + + + + + + + @@ -27,4 +38,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/IConnectionString.cs b/ProjectSchedule/ProjectSchedule/Repositories/IConnectionString.cs new file mode 100644 index 0000000..eebeaf0 --- /dev/null +++ b/ProjectSchedule/ProjectSchedule/Repositories/IConnectionString.cs @@ -0,0 +1,6 @@ +namespace ProjectSchedule.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get; } +} \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/AudienceRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/AudienceRepository.cs index 0a341ac..f4f1dba 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/AudienceRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/AudienceRepository.cs @@ -1,29 +1,120 @@ -using ProjectSchedule.Entities; -using ProjectSchedule.Entities.Enums; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectSchedule.Entities; namespace ProjectSchedule.Repositories.Implementations; public class AudienceRepository : IAudienceRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public AudienceRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateAudience(Audience audience) { - } - - public void DeleteAudience(int id) - { - } - - public Audience ReadAudienceById(int id) - { - return Audience.CreateEntity(0, string.Empty, TypeAudience.None, 0); - } - - public IEnumerable ReadAudiences() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(audience)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Audiences (NumberAudience, TypeAudience, QuantitySeats) + VALUES (@NumberAudience, @TypeAudience, @QuantitySeats)"; + connection.Execute(queryInsert, audience); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(audience)); + return audience; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadAudiences() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Audiences"; + var audiences = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(audiences)); + return audiences; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CompilingScheduleRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CompilingScheduleRepository.cs index ecbb3ab..5e89537 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CompilingScheduleRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CompilingScheduleRepository.cs @@ -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; public class CompilingScheduleRepository : ICompilingScheduleRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public CompilingScheduleRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + 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, +TypeWeek, NumberDay, NumberPair, TypeActivity) + VALUES (@EducatorId, @DisciplineId, @GroupStudentsId, @AudienceId, +@TypeWeek, @NumberDay, @NumberPair, @TypeActivity)"; + connection.Execute(queryInsert, compilingSchedule); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadCompilingSchedules(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(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(compilingSchedules)); + return compilingSchedules; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ConnectionString.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..9b07c44 --- /dev/null +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ConnectionString.cs @@ -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"; +} \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CurriculumSupplementRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CurriculumSupplementRepository.cs index 48f21c9..1498e17 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CurriculumSupplementRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/CurriculumSupplementRepository.cs @@ -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; public class CurriculumSupplementRepository : ICurriculumSupplementRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public CurriculumSupplementRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + 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(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) { + _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 ReadCurriculumSupplements(DateTime? dateForm = null, DateTime? dateTo = 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(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(curriculumSupplements)); + return curriculumSupplements; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/DisciplineRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/DisciplineRepository.cs index c94d026..0b52477 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/DisciplineRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/DisciplineRepository.cs @@ -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; public class DisciplineRepository : IDisciplineRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public DisciplineRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateDiscipline(Discipline discipline) { - } - - public void DeleteDiscipline(int id) - { - } - - public Discipline ReadDisciplineById(int id) - { - return Discipline.CreateEntity(0, string.Empty); - } - - public IEnumerable ReadDisciplines() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(discipline)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Disciplines (NameDiscipline) + VALUES (@NameDiscipline)"; + connection.Execute(queryInsert, discipline); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(discipline)); + return discipline; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadDisciplines() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Disciplines"; + var disciplines = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(disciplines)); + return disciplines; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/EducatorRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/EducatorRepository.cs index f189849..3763a97 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/EducatorRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/EducatorRepository.cs @@ -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; public class EducatorRepository : IEducatorRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public EducatorRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateEducator(Educator educator) { - } - - public void DeleteEducator(int id) - { - } - - public Educator ReadEducatorById(int id) - { - return Educator.CreateEntity(0, string.Empty, string.Empty, string.Empty); - } - - public IEnumerable ReadEducators() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(educator)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Educators (Surname, Name, Patronymic) + VALUES (@Surname, @Name, @Patronymic)"; + connection.Execute(queryInsert, educator); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(educator)); + return educator; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadEducators() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Educators"; + var educators = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(educators)); + return educators; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupStudentsRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupStudentsRepository.cs index 0a076ee..7b10c00 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupStudentsRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupStudentsRepository.cs @@ -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; public class GroupStudentsRepository : IGroupStudentsRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public GroupStudentsRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateGroupStudents(GroupStudents groupStudents) { - } - - public void DeleteGroupStudents(int id) - { - } - - public GroupStudents ReadGroupStudentsById(int id) - { - return GroupStudents.CreateEntity(0, string.Empty, string.Empty, 0); - } - - public IEnumerable ReadGroupsStudents() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(groupStudents)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO GroupsStudents (AbbreviationGroup, GroupNumber, QuantityStudents) + VALUES (@AbbreviationGroup, @GroupNumber, @QuantityStudents)"; + connection.Execute(queryInsert, groupStudents); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } 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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(groupStudents)); + return groupStudents; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadGroupsStudents() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM GroupsStudents"; + var groupsStudents = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(groupsStudents)); + return groupsStudents; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/appsettings.json b/ProjectSchedule/ProjectSchedule/appsettings.json new file mode 100644 index 0000000..20fc501 --- /dev/null +++ b/ProjectSchedule/ProjectSchedule/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/schedule_log.txt", + "rollingInterval": "Day" + } + } + ] + } +}