From b9a4d027d70419864b40b82fb385eed2ecf95bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B7=D0=B0=D0=BC=D0=B0=D1=82=20=D0=98=D1=88=D1=82?= =?UTF-8?q?=D1=83=D0=B3=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sun, 22 Dec 2024 18:36:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectSchedule/ProjectSchedule/Program.cs | 18 +++ .../Repositories/IConnectionString.cs | 6 + .../Implementations/AudienceRepository.cs | 123 +++++++++++++++--- .../Implementations/ConnectionString.cs | 6 + .../Implementations/DisciplineRepository.cs | 120 ++++++++++++++--- .../Implementations/GroupRepository.cs | 120 +++++++++++++++-- .../Implementations/ScheduleRepository.cs | 48 ++++++- .../StudingPlanSupplementRepository.cs | 76 ++++++++++- .../Implementations/TeacherRepository.cs | 120 +++++++++++++++-- .../ProjectSchedule/appsettings.json | 15 +++ 10 files changed, 593 insertions(+), 59 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 acf3d64..44cabe0 100644 --- a/ProjectSchedule/ProjectSchedule/Program.cs +++ b/ProjectSchedule/ProjectSchedule/Program.cs @@ -1,7 +1,11 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using ProjectSchedule.Repositories; using ProjectSchedule.Repositories.Implementations; +using Serilog; using Unity; using Unity.Lifetime; +using Unity.Microsoft.Logging; namespace ProjectSchedule { @@ -22,13 +26,27 @@ namespace ProjectSchedule private static IUnityContainer CreateUnityContainer() { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); 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/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..36ee52d 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 Audience (Number, TypeAudience, SeatsCount) + VALUES (@Number, @TypeAudience, @SeatsCount)"; + 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 Audience + SET + Number=@Number, + TypeAudience=@TypeAudience, + SeatsCount=@SeatsCount + 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 Audience + 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 Audience + 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 Audience"; + 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/ConnectionString.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..eb23207 --- /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=postgres;Username=postgres;Password=ishtuganov"; +} \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/DisciplineRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/DisciplineRepository.cs index fed1768..044a69e 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 Discipline (Name) + VALUES (@Name)"; + 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 Discipline + SET + Name=@Name + 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 Discipline + 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 Discipline + 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 Discipline"; + 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/GroupRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupRepository.cs index 5a32cf3..cfbfeee 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/GroupRepository.cs @@ -1,24 +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 GroupRepository : IGroupRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public GroupRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateGroup(Group group) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(group)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Groups (Name, GroupNumber, StudentsCount) + VALUES (@Name, @GroupNumber, @StudentsCount)"; + connection.Execute(queryInsert, group); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteGroup(int id) - { - } - public Group ReadGroupById(int id) - { - return Group.CreateEntity(0, string.Empty, string.Empty, 0); - } - public IEnumerable ReadGroups() - { - return []; - } + public void UpdateGroup(Group group) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(group)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Groups + SET + Name=@Name, + GroupNumber=@GroupNumber, + StudentsCount=@StudentsCount + WHERE Id=@Id"; + connection.Execute(queryUpdate, group); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } + + public void DeleteGroup(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Groups + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + + public Group ReadGroupById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Groups + WHERE Id=@id"; + var group = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(group)); + return group; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadGroups() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Groups"; + var groups = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(groups)); + return groups; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ScheduleRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ScheduleRepository.cs index 172f254..f0824e3 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ScheduleRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/ScheduleRepository.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 ScheduleRepository : IScheduleRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public ScheduleRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateSchedule(Schedule schedule) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(schedule)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Schedule (TeacherId, DisciplineId, GroupStudentsId, AudienceId, DateDay, +TypeWeek, NumberDay, NumberPair, TypePair) + VALUES (@TeacherId, @DisciplineId, @GroupStudentsId, @AudienceId, @DateDay, +@TypeWeek, @NumberDay, @NumberPair, @TypePair)"; + connection.Execute(queryInsert, schedule); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadSchedules(DateTime? dateForm = null, DateTime? dateTo = null, int? teacherId = 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 Schedule"; + var schedules = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(schedules)); + return schedules; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/StudingPlanSupplementRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/StudingPlanSupplementRepository.cs index bea1e88..a82da64 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/StudingPlanSupplementRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/StudingPlanSupplementRepository.cs @@ -1,18 +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 StudingPlanSupplementRepository : IStudingPlanSupplementRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public StudingPlanSupplementRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateStudingPlanSupplement(StudingPlanSupplement studingPlanSupplement) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(studingPlanSupplement)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" + INSERT INTO StudingPlanSupplement (GroupId, NameStudingPlan, Semester, DateApprovePlan) + VALUES (@GroupId, @NameStudingPlan, @Semester, @DateApprovePlan); + SELECT MAX(Id) FROM StudingPlanSupplement"; + var studingPlanSupplementId = connection.QueryFirst(queryInsert, studingPlanSupplement, transaction); + + var querySubInsert = @" + INSERT INTO Discipline_StudingPlanSupplement (StudingPlanSupplementId, DisciplineId, LecturesCount, PracticesCount) + VALUES (@StudingPlanSupplementId, @DisciplineId, @LecturesCount, @PracticesCount)"; + foreach (var elem in studingPlanSupplement.DisciplineStudingPlanSupplements) + { + connection.Execute(querySubInsert, new { studingPlanSupplementId, elem.DisciplineId, elem.LecturesCount, elem.PracticesCount }, transaction); + } + + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } + public void DeleteStudingPlanSupplement(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM StudingPlanSupplement + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } + public IEnumerable ReadStudingPlanSupplements(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 StudingPlanSupplement"; + var studingPlanSupplements = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(studingPlanSupplements)); + return studingPlanSupplements; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/TeacherRepository.cs b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/TeacherRepository.cs index 8f266e2..2dd6cb7 100644 --- a/ProjectSchedule/ProjectSchedule/Repositories/Implementations/TeacherRepository.cs +++ b/ProjectSchedule/ProjectSchedule/Repositories/Implementations/TeacherRepository.cs @@ -1,24 +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 TeacherRepository : ITeacherRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public TeacherRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateTeacher(Teacher teacher) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(teacher)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Teacher (Surname, Name, Patronymic) + VALUES (@Surname, @Name, @Patronymic)"; + connection.Execute(queryInsert, teacher); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteTeacher(int id) - { - } - public Teacher ReadTeacherById(int id) - { - return Teacher.CreateEntity(0, string.Empty, string.Empty, string.Empty); - } - public IEnumerable ReadTeachers() - { - return []; - } + public void UpdateTeacher(Teacher teacher) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(teacher)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Teacher + SET + Surname=@Surname, + Name=@Name, + Patronymic=@Patronymic + WHERE Id=@Id"; + connection.Execute(queryUpdate, teacher); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } + + public void DeleteTeacher(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Teacher + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + + public Teacher ReadTeacherById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Teacher + WHERE Id=@id"; + var teacher = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(teacher)); + return teacher; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadTeachers() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Teacher"; + var teachers = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(teachers)); + return teachers; + } + 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..b91cc3c --- /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" + } + } + ] + } +} \ No newline at end of file