diff --git a/ProjectTimeTable/ProjectTimeTable/Entites/Enums/ClassType.cs b/ProjectTimeTable/ProjectTimeTable/Entites/Enums/ClassType.cs index 4945a80..6b585e1 100644 --- a/ProjectTimeTable/ProjectTimeTable/Entites/Enums/ClassType.cs +++ b/ProjectTimeTable/ProjectTimeTable/Entites/Enums/ClassType.cs @@ -11,5 +11,5 @@ public enum ClassType None = 0, Lection = 1, Computer =2, - Standart = 3 + Standart = 4 } diff --git a/ProjectTimeTable/ProjectTimeTable/Entites/GroupTimetable.cs b/ProjectTimeTable/ProjectTimeTable/Entites/GroupTimetable.cs index 51b51c4..ec33cb9 100644 --- a/ProjectTimeTable/ProjectTimeTable/Entites/GroupTimetable.cs +++ b/ProjectTimeTable/ProjectTimeTable/Entites/GroupTimetable.cs @@ -10,12 +10,19 @@ public class GroupTimetable public int GroupId { get; private set; } public int TimeTableId { get; private set; } + public int DisciplineID { get; private set; } + public int ClassID { get; private set; } + public int GroupID { get; private set; } + public int TeacherID { get; private set; } - public static GroupTimetable CreateElement(int idGroup, int idTimeTable) + public static GroupTimetable CreateElement(int idTimeTable, int idDiscipline, int idClass, int idGroup, int idTeacher) { return new GroupTimetable() { + DisciplineID = idDiscipline, + ClassID = idClass, GroupId = idGroup, + TeacherID = idTeacher, TimeTableId = idTimeTable }; } diff --git a/ProjectTimeTable/ProjectTimeTable/Entites/TimeTable.cs b/ProjectTimeTable/ProjectTimeTable/Entites/TimeTable.cs index 5a26338..f6f9c5e 100644 --- a/ProjectTimeTable/ProjectTimeTable/Entites/TimeTable.cs +++ b/ProjectTimeTable/ProjectTimeTable/Entites/TimeTable.cs @@ -12,10 +12,6 @@ public class TimeTable public int NumberLesson { get; private set; } public int Day { get; private set; } public int Week { get; private set; } - public int DisciplineID { get; private set; } - public int ClassID { get; private set; } - public int GroupID { get; private set; } - public int TeacherID { get; private set; } public IEnumerable GroupTimetable { get; @@ -24,7 +20,7 @@ public class TimeTable - public static TimeTable CreateOperation(int id, int numberLesson, int day, int week, int disciplineID, int classID, int groupID, int teacherID, IEnumerable groupTimetable) + public static TimeTable CreateOperation(int id, int numberLesson, int day, int week, IEnumerable groupTimetable) { return new TimeTable() { @@ -32,10 +28,6 @@ public class TimeTable NumberLesson = numberLesson, Day = day, Week = week, - DisciplineID = disciplineID, - ClassID = classID, - GroupID = groupID, - TeacherID = teacherID, GroupTimetable = groupTimetable }; } diff --git a/ProjectTimeTable/ProjectTimeTable/Forms/FormAuditorium.cs b/ProjectTimeTable/ProjectTimeTable/Forms/FormAuditorium.cs index 6570845..9d21d22 100644 --- a/ProjectTimeTable/ProjectTimeTable/Forms/FormAuditorium.cs +++ b/ProjectTimeTable/ProjectTimeTable/Forms/FormAuditorium.cs @@ -73,7 +73,7 @@ namespace ProjectTimeTable.Forms if (_auditoriumId.HasValue) { - _auditoriumRepository.UpdateAuditorium(CreateAuditorium(_auditoriumId.Value)); + _auditoriumRepository.UpdateAuditorium(CreateAuditorium((int)_auditoriumId)); } else { @@ -81,6 +81,7 @@ namespace ProjectTimeTable.Forms } Close(); + } catch (Exception ex) { diff --git a/ProjectTimeTable/ProjectTimeTable/Forms/FormGroup.cs b/ProjectTimeTable/ProjectTimeTable/Forms/FormGroup.cs index 6ddc387..e6cd465 100644 --- a/ProjectTimeTable/ProjectTimeTable/Forms/FormGroup.cs +++ b/ProjectTimeTable/ProjectTimeTable/Forms/FormGroup.cs @@ -30,10 +30,10 @@ namespace ProjectTimeTable.Forms { throw new InvalidDataException(nameof(group)); } - TextBoxName.Text = group.Name; numericUpDownCourse.Value = group.Course; numericUpDownCount.Value = group.CountPeople; + comboBoxPlan.SelectedValue = group.PlanID; _groupId = value; } catch (Exception ex) @@ -67,7 +67,7 @@ namespace ProjectTimeTable.Forms if (_groupId.HasValue) { - _groupRepository.UpdateGroup(CreateGroup(_groupId.Value)); + _groupRepository.UpdateGroup(CreateGroup((int)_groupId)); } else { @@ -83,7 +83,7 @@ namespace ProjectTimeTable.Forms } } - private Group CreateGroup(int id) => Group.CreateEntity(0, TextBoxName.Text, + private Group CreateGroup(int id) => Group.CreateEntity(id, TextBoxName.Text, Convert.ToInt32(numericUpDownCourse.Value), Convert.ToInt32(numericUpDownCount.Value), (int)comboBoxPlan.SelectedValue!); diff --git a/ProjectTimeTable/ProjectTimeTable/Forms/FormPlans.cs b/ProjectTimeTable/ProjectTimeTable/Forms/FormPlans.cs index 6410260..79e39bc 100644 --- a/ProjectTimeTable/ProjectTimeTable/Forms/FormPlans.cs +++ b/ProjectTimeTable/ProjectTimeTable/Forms/FormPlans.cs @@ -50,6 +50,5 @@ namespace ProjectTimeTable.Forms MessageBoxButtons.OK, MessageBoxIcon.Error); } } - } } diff --git a/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.Designer.cs b/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.Designer.cs index f59079c..7d842b3 100644 --- a/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.Designer.cs +++ b/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.Designer.cs @@ -115,6 +115,7 @@ ButtonSave.TabIndex = 18; ButtonSave.Text = "Сохранить"; ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // // ButtonCancel // @@ -125,6 +126,7 @@ ButtonCancel.TabIndex = 19; ButtonCancel.Text = "Отмена"; ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; // // groupBoxTimeTable // diff --git a/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.cs b/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.cs index 9ef751f..c392c27 100644 --- a/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.cs +++ b/ProjectTimeTable/ProjectTimeTable/Forms/FormTimeTable.cs @@ -25,7 +25,7 @@ namespace ProjectTimeTable.Forms ColumnDiscipline.DataSource =disciplineRepository.ReadDiscipline(); ColumnDiscipline.DisplayMember = "Name"; - ColumnDiscipline.ValueMember = "Id"; + ColumnDiscipline.ValueMember = "ID"; ColumnAuditorium.DataSource = auditoriumRepository.ReadAuditorium(); ColumnAuditorium.DisplayMember = "Name"; @@ -50,10 +50,9 @@ namespace ProjectTimeTable.Forms throw new Exception("Имеются незаполненные поля"); } - + dataGridViewTimeTable.RowCount--; _timeTableRepository.CreateTimeTable(TimeTable.CreateOperation(0, Convert.ToInt32(numericUpDownCountLesson.Value), - Convert.ToInt32(numericUpDownDay.Value), Convert.ToInt32(numericUpDownWeek.Value), (int)comboBoxDiscipline.SelectedValue!, - (int)comboBoxAuditorium.SelectedValue!, (int)comboBoxGroup.SelectedValue!, (int)comboBoxTeacher.SelectedValue!, + Convert.ToInt32(numericUpDownDay.Value), Convert.ToInt32(numericUpDownWeek.Value), CreateListGroupTimeTableFromDataGrid())); Close(); @@ -80,7 +79,8 @@ namespace ProjectTimeTable.Forms continue; } - list.Add(GroupTimetable.CreateElement(Convert.ToInt32(comboBoxGroup.SelectedIndex), 0)); + list.Add(GroupTimetable.CreateElement(Convert.ToInt32(comboBoxDiscipline.SelectedIndex), Convert.ToInt32(comboBoxAuditorium.SelectedIndex), + Convert.ToInt32(comboBoxGroup.SelectedIndex), Convert.ToInt32(comboBoxTeacher.SelectedIndex), 0)); } return list; diff --git a/ProjectTimeTable/ProjectTimeTable/Program.cs b/ProjectTimeTable/ProjectTimeTable/Program.cs index f275dce..3f3982e 100644 --- a/ProjectTimeTable/ProjectTimeTable/Program.cs +++ b/ProjectTimeTable/ProjectTimeTable/Program.cs @@ -1,6 +1,10 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using ProjectTimeTable.Repositories; using ProjectTimeTable.Repositories.Implementation; +using Serilog; using Unity; +using Unity.Microsoft.Logging; namespace ProjectTimeTable { @@ -21,14 +25,29 @@ namespace ProjectTimeTable private static IUnityContainer CreateContainer() { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); + + container.RegisterType(); 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/ProjectTimeTable/ProjectTimeTable/ProjectTimeTable.csproj b/ProjectTimeTable/ProjectTimeTable/ProjectTimeTable.csproj index 1bfb289..120434b 100644 --- a/ProjectTimeTable/ProjectTimeTable/ProjectTimeTable.csproj +++ b/ProjectTimeTable/ProjectTimeTable/ProjectTimeTable.csproj @@ -9,7 +9,20 @@ + + + + + + + + + + + + + @@ -31,4 +44,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/IConnectionString.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/IConnectionString.cs new file mode 100644 index 0000000..8c5c7ab --- /dev/null +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/IConnectionString.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTimeTable.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get; } +} diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/ITeacherRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/ITeacherRepositories.cs index 6464a63..a90cbd9 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/ITeacherRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/ITeacherRepositories.cs @@ -9,6 +9,7 @@ namespace ProjectTimeTable.Repositories; public interface ITeacherRepositories { + IEnumerable ReadTeacher(); Teacher ReadTeacherById(int id); diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/AuditoriumRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/AuditoriumRepositories.cs index 6bf515c..e35af5c 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/AuditoriumRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/AuditoriumRepositories.cs @@ -1,5 +1,9 @@ -using ProjectTimeTable.Entites; +using Newtonsoft.Json; +using Npgsql; +using ProjectTimeTable.Entites; using ProjectTimeTable.Entites.Enums; +using Dapper; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; @@ -9,23 +13,110 @@ using System.Threading.Tasks; namespace ProjectTimeTable.Repositories.Implementation; public class AuditoriumRepositories : IAuditoriumRepositories { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public AuditoriumRepositories(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateAuditorium(Auditorium classs) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(classs)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @"INSERT INTO auditorium (Name, ClassType, Size) + VALUES (@Name, @ClassType, @Size)"; + connection.Execute(queryInsert, classs); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } public void DeleteAuditorium(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM Auditorium WHERE Id=@Id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadAuditorium() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Auditorium"; + var auditorium = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(auditorium)); + return auditorium; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } - public Auditorium ReadAuditoriumById(int id) { return Auditorium.CreateEntity(0, string.Empty, ClassType.None, 0); } + public Auditorium ReadAuditoriumById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Auditorium WHERE Id=@id"; + var auditorium = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(auditorium)); + return auditorium; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } public void UpdateAuditorium(Auditorium classs) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(classs)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @"UPDATE Auditorium SET + Name=@Name, + ClassType=@ClassType, + Size=@Size + WHERE Id=@id"; + connection.Execute(queryUpdate, classs); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/ConnectionString.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/ConnectionString.cs new file mode 100644 index 0000000..a6356a0 --- /dev/null +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/ConnectionString.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTimeTable.Repositories.Implementation; + +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "Server=localhost, 5432;Database=timetable_otp;Uid=postgres;Pwd=postgres;"; +} diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/DisciplineRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/DisciplineRepositories.cs index ab75ba0..5fe5f98 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/DisciplineRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/DisciplineRepositories.cs @@ -1,4 +1,8 @@ -using ProjectTimeTable.Entites; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTimeTable.Entites; using System; using System.Collections.Generic; using System.Linq; @@ -9,25 +13,107 @@ namespace ProjectTimeTable.Repositories.Implementation; public class DisciplineRepositories : IDisciplineRepositories { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public DisciplineRepositories(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateDiscipline(Discipline discipline) { + _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 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 IEnumerable ReadDiscipline() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Discipline"; + var discipline = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(discipline)); + return discipline; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Discipline ReadDisciplineById(int id) { - return Discipline.CreateEntity(0, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Discipline WHERE ID=@id"; + var auditorium = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(auditorium)); + return auditorium; + } + 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; + } } } diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/GroupRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/GroupRepositories.cs index 8af9593..b14e9b9 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/GroupRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/GroupRepositories.cs @@ -1,4 +1,8 @@ -using ProjectTimeTable.Entites; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTimeTable.Entites; using System; using System.Collections.Generic; using System.Linq; @@ -9,25 +13,112 @@ namespace ProjectTimeTable.Repositories.Implementation; public class GroupRepositories : IGroupRepositories { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public GroupRepositories(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 Groupp (Name, Course, CountPeople, PlanID) + VALUES (@Name, @Course, @CountPeople, @PlanID)"; + connection.Execute(queryInsert, 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 Groupp WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadGroup() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Groupp"; + var group = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(group)); + return group; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Group ReadGroupById(int id) { - return Group.CreateEntity(0, string.Empty, 0, 0, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Groupp 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 void UpdateGroup(Group group) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(group)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @"UPDATE Groupp SET + Name=@Name, + Course=@Course, + CountPeople=@CountPeople, + PlanID=@PlanID + WHERE ID=@id"; + connection.Execute(queryUpdate, group); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/PlanRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/PlanRepositories.cs index c6de4c0..9948bc4 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/PlanRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/PlanRepositories.cs @@ -1,26 +1,100 @@ -using ProjectTimeTable.Entites; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTimeTable.Entites; +using Dapper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Unity; namespace ProjectTimeTable.Repositories.Implementation; public class PlanRepositories : IPlanRepositories { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public PlanRepositories(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreatePlan(Plan plan) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(plan)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @"INSERT INTO Plan (Name) + VALUES (@Name); + SELECT MAX(Id) FROM Plan"; + var PlanID = connection.QueryFirst(queryInsert, plan, transaction); + var querySubInsert = @"INSERT INTO DisciplinePlan (PlanId, DisciplineId, CountLesson, Course) + VALUES (@PlanId, @DisciplineId, @CountLesson, @Course)"; + foreach (var elem in plan.DisciplinePlan) + { + connection.Execute(querySubInsert, new + { + PlanID, + elem.DisciplineId, + elem.CountLesson, + elem.Course + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadPlan(int? groupId = null, int? disciplineId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Plan"; + var plan = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(plan)); + return plan; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Plan ReadPlanById(int id) { - return Plan.CreateOperation(0, string.Empty,[]); - + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM teacher WHERE [Id]=@id"; + var plan = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(plan)); + return plan; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } } diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TeacherRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TeacherRepositories.cs index 25f0010..86edc6a 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TeacherRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TeacherRepositories.cs @@ -1,4 +1,8 @@ -using ProjectTimeTable.Entites; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTimeTable.Entites; using ProjectTimeTable.Entites.Enums; using System; using System.Collections.Generic; @@ -10,25 +14,110 @@ namespace ProjectTimeTable.Repositories.Implementation; public class TeacherRepositories : ITeacherRepositories { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public TeacherRepositories(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 (FirstName, SecondName, LastName, TeacherPost) + VALUES (@FirstName, @SecondName, @LastName, @TeacherPost)"; + connection.Execute(queryInsert, 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 IEnumerable ReadTeacher() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM teacher"; + var teacher = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(teacher)); + return teacher; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Teacher ReadTeacherById(int id) { - return Teacher.CreateEntity(0, string.Empty, string.Empty, string.Empty, TeacherPost.None); + _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 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 + FirstName=@FirstName, + SecondName=@SecondName, + LastName=@LastName, + TeacherPost=@TeacherPost + WHERE Id=@id"; + connection.Execute(queryUpdate, teacher); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + + } } } diff --git a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TimeTableRepositories.cs b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TimeTableRepositories.cs index 4f8c545..d952105 100644 --- a/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TimeTableRepositories.cs +++ b/ProjectTimeTable/ProjectTimeTable/Repositories/Implementation/TimeTableRepositories.cs @@ -1,7 +1,12 @@ -using ProjectTimeTable.Entites; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTimeTable.Entites; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -9,16 +14,80 @@ namespace ProjectTimeTable.Repositories.Implementation; public class TimeTableRepositories : ITimeTableRepositories { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public TimeTableRepositories(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateTimeTable(TimeTable timeTable) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(timeTable)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @"INSERT INTO TimeTable (NumberLesson, Day, Week) + VALUES (@NumberLesson, @Day, @Week); + SELECT MAX(Id) FROM TimeTable"; + var PlanID = connection.QueryFirst(queryInsert, timeTable, transaction); + var querySubInsert = @"INSERT INTO GroupTimetable (GroupId, timeTableId, DisciplineID, ClassID, GroupID, TeacherID) + VALUES (@GroupId, @timeTableId, @DisciplineID, @ClassID, @GroupID, @TeacherID)"; + foreach (var elem in timeTable.GroupTimetable) + { + connection.Execute(querySubInsert, new + { + elem.GroupId, + elem.TimeTableId, + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteTimeTable(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @"DELETE FROM TimeTable + WHERE ID=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadTimeTable(int? week = null, int? day = null, int? groupId = null, int? classId = null, int? teacherId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM timetable"; + var timetable = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(timetable)); + return timetable; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectTimeTable/ProjectTimeTable/appsettings.json b/ProjectTimeTable/ProjectTimeTable/appsettings.json new file mode 100644 index 0000000..4c6d038 --- /dev/null +++ b/ProjectTimeTable/ProjectTimeTable/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/timetable_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file