From 0fc8f3fd477a87e4b6290ca102f72a86e41a1c61 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Mon, 11 Nov 2024 23:50:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entities/CurriculumSubject.cs | 4 +- .../ProjectShedule/Entities/Shedule.cs | 7 +- .../Entities/{Group.cs => StudentsGroup.cs} | 6 +- .../FormSchedulingBureau.Designer.cs | 2 +- .../ProjectShedule/Forms/FormClassroom.cs | 11 +- .../Forms/FormCurriculum.Designer.cs | 2 +- .../ProjectShedule/Forms/FormCurriculum.cs | 2 +- .../Forms/FormCurriculumSubject.cs | 7 +- .../Forms/FormCurriculumSubjects.Designer.cs | 25 +++- .../Forms/FormCurriculumSubjects.cs | 36 ++++++ .../Forms/FormCurriculums.Designer.cs | 3 +- .../ProjectShedule/Forms/FormCurriculums.cs | 10 -- .../ProjectShedule/Forms/FormGroup.cs | 17 +-- .../Forms/FormGroups.Designer.cs | 3 +- .../ProjectShedule/Forms/FormGroups.cs | 10 -- .../ProjectShedule/Forms/FormShedule.cs | 2 - .../Forms/FormShedules.Designer.cs | 16 ++- .../ProjectShedule/Forms/FormShedules.cs | 41 ++++++- .../ProjectShedule/Forms/FormSubject.cs | 16 +-- .../Forms/FormSubjects.Designer.cs | 3 +- .../ProjectShedule/Forms/FormSubjects.cs | 10 -- .../ProjectShedule/Forms/FormTeacher.cs | 12 +- .../ProjectShedule/Forms/FormTeachers.cs | 10 -- ProjectShedule/ProjectShedule/Program.cs | 20 +++ .../ProjectShedule/ProjectShedule.csproj | 20 +++ .../Repositories/IConnectionString.cs | 12 ++ .../ICurriculumSubjectRepository.cs | 1 + .../Repositories/IGroupRepository.cs | 8 +- .../Repositories/ISheduleRepository.cs | 1 - .../Implementations/ClassroomRepository.cs | 102 +++++++++++++++- .../Implementations/ConnectionString.cs | 6 + .../Implementations/CurriculumRepository.cs | 102 ++++++++++++++-- .../CurriculumSubjectRepository.cs | 89 ++++++++++++-- .../Implementations/GroupRepository.cs | 114 ++++++++++++++++-- .../Implementations/SheduleRepository.cs | 69 +++++++++-- .../Implementations/SubjectRepository.cs | 102 ++++++++++++++-- .../Implementations/TeacherRepository.cs | 103 ++++++++++++++-- .../ProjectShedule/appsettings.json | 15 +++ 38 files changed, 837 insertions(+), 182 deletions(-) rename ProjectShedule/ProjectShedule/Entities/{Group.cs => StudentsGroup.cs} (78%) create mode 100644 ProjectShedule/ProjectShedule/Repositories/IConnectionString.cs create mode 100644 ProjectShedule/ProjectShedule/Repositories/Implementations/ConnectionString.cs create mode 100644 ProjectShedule/ProjectShedule/appsettings.json diff --git a/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs b/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs index b021065..e2d85a7 100644 --- a/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs +++ b/ProjectShedule/ProjectShedule/Entities/CurriculumSubject.cs @@ -9,14 +9,16 @@ namespace ProjectShedule.Entities; public class CurriculumSubject { + public int Id { get; set; } public int CurriculumId { get; private set; } public IEnumerable SubjectSubject { get; private set; } = []; public Course Course { get; private set; } - public static CurriculumSubject CreateCurriculumSubject(int curriculumId, Course course, IEnumerable subjectSubjects) + public static CurriculumSubject CreateCurriculumSubject(int id, int curriculumId, Course course, IEnumerable subjectSubjects) { return new CurriculumSubject() { + Id = id, CurriculumId = curriculumId, Course = course, SubjectSubject = subjectSubjects diff --git a/ProjectShedule/ProjectShedule/Entities/Shedule.cs b/ProjectShedule/ProjectShedule/Entities/Shedule.cs index 428e327..21e23c7 100644 --- a/ProjectShedule/ProjectShedule/Entities/Shedule.cs +++ b/ProjectShedule/ProjectShedule/Entities/Shedule.cs @@ -12,20 +12,21 @@ public class Shedule public int Id { get; private set; } public DateTime Date { get; private set; } public PairNumber PairNumber { get; private set; } - public int GroupId { get; private set; } + public int StudentsGroupId { get; private set; } public int TeacherId { get; private set; } public int SubjectId { get; private set; } public int ClassroomId { get; private set; } - public static Shedule CreateOperation(int id, DateTime date, PairNumber pairNumber, int groupId, int teacherId, int subjectId, int classroomId) + public static Shedule CreateOperation(int id, DateTime date, PairNumber pairNumber + , int studentsGroupId, int teacherId, int subjectId, int classroomId) { return new Shedule { Id = id, Date = date, PairNumber = pairNumber, - GroupId = groupId, + StudentsGroupId = studentsGroupId, TeacherId = teacherId, SubjectId = subjectId, ClassroomId = classroomId diff --git a/ProjectShedule/ProjectShedule/Entities/Group.cs b/ProjectShedule/ProjectShedule/Entities/StudentsGroup.cs similarity index 78% rename from ProjectShedule/ProjectShedule/Entities/Group.cs rename to ProjectShedule/ProjectShedule/Entities/StudentsGroup.cs index 93b3ea0..1d1751a 100644 --- a/ProjectShedule/ProjectShedule/Entities/Group.cs +++ b/ProjectShedule/ProjectShedule/Entities/StudentsGroup.cs @@ -7,16 +7,16 @@ using System.Threading.Tasks; namespace ProjectShedule.Entities; -public class Group +public class StudentsGroup { public int Id { get; private set; } public int StudentsCount { get; private set; } public string GroupNumber { get; private set; } = String.Empty; public Course Course { get; private set; } public int CurriculumId { get; private set; } - public static Group CreateGroup(int id, int studentsCount, string groupNumber, Course course, int curriculumId) + public static StudentsGroup CreateGroup(int id, int studentsCount, string groupNumber, Course course, int curriculumId) { - return new Group + return new StudentsGroup { Id = id, StudentsCount = studentsCount, diff --git a/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs b/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs index 16658ef..4249d94 100644 --- a/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs +++ b/ProjectShedule/ProjectShedule/FormSchedulingBureau.Designer.cs @@ -84,7 +84,7 @@ // curriculumsToolStripMenuItem.Name = "curriculumsToolStripMenuItem"; curriculumsToolStripMenuItem.Size = new Size(224, 26); - curriculumsToolStripMenuItem.Text = "Учебные планы"; + curriculumsToolStripMenuItem.Text = "Направлении"; curriculumsToolStripMenuItem.Click += curriculumsToolStripMenuItem_Click; // // studentGroupsToolStripMenuItem diff --git a/ProjectShedule/ProjectShedule/Forms/FormClassroom.cs b/ProjectShedule/ProjectShedule/Forms/FormClassroom.cs index b2461f2..d584f34 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormClassroom.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormClassroom.cs @@ -1,15 +1,6 @@ using ProjectShedule.Entities; using ProjectShedule.Entities.Enums; using ProjectShedule.Repositories; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; namespace ProjectShedule.Forms { @@ -63,7 +54,7 @@ namespace ProjectShedule.Forms } else { - _classroomRepository.UpdateClassroom(CreateClassroom(0)); + _classroomRepository.CreateClassroom(CreateClassroom(0)); } Close(); } diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculum.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculum.Designer.cs index 7ac90a5..e20d151 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculum.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculum.Designer.cs @@ -80,7 +80,7 @@ Controls.Add(textBoxCurriculumName); Controls.Add(label2); Name = "FormCurriculum"; - Text = "Учебный план(дисциплина)"; + Text = "Учебный план(направление)"; ResumeLayout(false); PerformLayout(); } diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculum.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculum.cs index a0c89c4..a5b57de 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculum.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculum.cs @@ -49,7 +49,7 @@ namespace ProjectShedule.Forms } else { - _curriculumRepository.UpdateCurriculum(CreateCurriculum(0)); + _curriculumRepository.CreateCurriculum(CreateCurriculum(0)); } Close(); } diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs index 87ce6c8..ef75583 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubject.cs @@ -34,13 +34,14 @@ namespace ProjectShedule.Forms try { if (comboBoxDirection.SelectedIndex < 0 || - comboBoxCourse.SelectedIndex < 0 || dataGridView.RowCount < 1) + comboBoxCourse.SelectedIndex < 1 || dataGridView.RowCount < 1) { throw new Exception("Есть незаполненные поля"); } - _curriculumSubjectRepository.CreateCurriculumSubject(CurriculumSubject.CreateCurriculumSubject( + _curriculumSubjectRepository.CreateCurriculumSubject(CurriculumSubject.CreateCurriculumSubject(0, (int)comboBoxDirection.SelectedValue!, - (Course)comboBoxCourse.SelectedItem!, CreateListSubjectSubjectFromDataGrid())); + (Course)comboBoxCourse.SelectedItem!, + CreateListSubjectSubjectFromDataGrid())); Close(); } diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.Designer.cs index ddde3d4..ddfa4f2 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.Designer.cs @@ -29,6 +29,7 @@ private void InitializeComponent() { panel1 = new Panel(); + buttonDel = new Button(); buttonAdd = new Button(); dataGridView = new DataGridView(); panel1.SuspendLayout(); @@ -37,18 +38,30 @@ // // panel1 // + panel1.Controls.Add(buttonDel); panel1.Controls.Add(buttonAdd); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(622, 0); + panel1.Location = new Point(600, 0); panel1.Name = "panel1"; - panel1.Size = new Size(178, 352); + panel1.Size = new Size(200, 352); panel1.TabIndex = 2; // + // buttonDel + // + buttonDel.BackgroundImage = Properties.Resources.minus_PNG39; + buttonDel.BackgroundImageLayout = ImageLayout.Stretch; + buttonDel.Location = new Point(42, 214); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(94, 93); + buttonDel.TabIndex = 4; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; + // // buttonAdd // buttonAdd.BackgroundImage = Properties.Resources.Ambox_plus_svg; buttonAdd.BackgroundImageLayout = ImageLayout.Stretch; - buttonAdd.Location = new Point(42, 125); + buttonAdd.Location = new Point(42, 45); buttonAdd.Name = "buttonAdd"; buttonAdd.Size = new Size(94, 93); buttonAdd.TabIndex = 0; @@ -63,7 +76,6 @@ dataGridView.AllowUserToResizeRows = false; dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Fill; dataGridView.Location = new Point(0, 0); dataGridView.MultiSelect = false; dataGridView.Name = "dataGridView"; @@ -71,7 +83,7 @@ dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersWidth = 51; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(800, 352); + dataGridView.Size = new Size(601, 352); dataGridView.TabIndex = 3; // // FormCurriculumSubjects @@ -82,7 +94,7 @@ Controls.Add(panel1); Controls.Add(dataGridView); Name = "FormCurriculumSubjects"; - Text = "FormCurriculumSubjects"; + Text = "Учебные планы"; Load += FormCurriculumSubjects_Load; panel1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); @@ -94,5 +106,6 @@ private Panel panel1; private Button buttonAdd; private DataGridView dataGridView; + private Button buttonDel; } } \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.cs index 89f82bb..dd05a1e 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculumSubjects.cs @@ -41,5 +41,41 @@ namespace ProjectShedule.Forms } } private void LoadList() => dataGridView.DataSource = _curriculumSubjectRepository.ReadCurriculumSubject(); + + private void buttonDel_Click(object sender, EventArgs e) + { + if (TryGetIdentifierFromSelectedRow(out var findId)) + { + return; + } + + if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + try + { + _curriculumSubjectRepository.DeleteCurriculumSubject(findId); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private bool TryGetIdentifierFromSelectedRow(out int id) + { + id = 0; + if (dataGridView.SelectedRows.Count < 1) + { + MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return true; + } + else + { + id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[id].Value); + return false; + } + } } } diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculums.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculums.Designer.cs index 2404735..c173d44 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculums.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculums.Designer.cs @@ -89,7 +89,6 @@ dataGridView.AllowUserToResizeRows = false; dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Fill; dataGridView.Location = new Point(0, 0); dataGridView.MultiSelect = false; dataGridView.Name = "dataGridView"; @@ -97,7 +96,7 @@ dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersWidth = 51; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(800, 353); + dataGridView.Size = new Size(625, 353); dataGridView.TabIndex = 3; // // FormCurriculums diff --git a/ProjectShedule/ProjectShedule/Forms/FormCurriculums.cs b/ProjectShedule/ProjectShedule/Forms/FormCurriculums.cs index 6f8b64e..8a2d4a8 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormCurriculums.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormCurriculums.cs @@ -1,14 +1,4 @@ using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using Unity; namespace ProjectShedule.Forms diff --git a/ProjectShedule/ProjectShedule/Forms/FormGroup.cs b/ProjectShedule/ProjectShedule/Forms/FormGroup.cs index 04e6b25..15c9d70 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormGroup.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormGroup.cs @@ -1,17 +1,6 @@ using ProjectShedule.Entities; using ProjectShedule.Entities.Enums; using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - namespace ProjectShedule.Forms { public partial class FormGroup : Form @@ -59,7 +48,7 @@ namespace ProjectShedule.Forms try { if (string.IsNullOrWhiteSpace(textBoxGroupName.Text) || comboBoxCourse.SelectedIndex < 1 || - comboBoxCurriculum.SelectedIndex < 1) + comboBoxCurriculum.SelectedIndex < 0) { throw new Exception("Имеются незаполненные поля"); } @@ -80,11 +69,11 @@ namespace ProjectShedule.Forms } private void buttonCancel_Click(object sender, EventArgs e) => Close(); - private Group CreateGroup(int id) => Group.CreateGroup( + private StudentsGroup CreateGroup(int id) => StudentsGroup.CreateGroup( id, Convert.ToInt32(numericUpDownStudentsCount.Value), textBoxGroupName.Text, (Course)comboBoxCourse.SelectedItem!, - (int)comboBoxCourse.SelectedValue!); + (int)comboBoxCurriculum.SelectedValue!); } } diff --git a/ProjectShedule/ProjectShedule/Forms/FormGroups.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormGroups.Designer.cs index 3aeb5f2..501ef92 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormGroups.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormGroups.Designer.cs @@ -89,7 +89,6 @@ dataGridView.AllowUserToResizeRows = false; dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Fill; dataGridView.Location = new Point(0, 0); dataGridView.MultiSelect = false; dataGridView.Name = "dataGridView"; @@ -97,7 +96,7 @@ dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersWidth = 51; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(800, 354); + dataGridView.Size = new Size(626, 354); dataGridView.TabIndex = 5; // // FormGroups diff --git a/ProjectShedule/ProjectShedule/Forms/FormGroups.cs b/ProjectShedule/ProjectShedule/Forms/FormGroups.cs index b048ce5..3fc0c54 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormGroups.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormGroups.cs @@ -1,14 +1,4 @@ using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using Unity; namespace ProjectShedule.Forms diff --git a/ProjectShedule/ProjectShedule/Forms/FormShedule.cs b/ProjectShedule/ProjectShedule/Forms/FormShedule.cs index 57b35a3..d22007f 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormShedule.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormShedule.cs @@ -1,8 +1,6 @@ using ProjectShedule.Entities; using ProjectShedule.Entities.Enums; using ProjectShedule.Repositories; -using System.Diagnostics.Contracts; -using System.Windows.Forms; namespace ProjectShedule.Forms { diff --git a/ProjectShedule/ProjectShedule/Forms/FormShedules.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormShedules.Designer.cs index b0d43be..9b47eb7 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormShedules.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormShedules.Designer.cs @@ -31,6 +31,7 @@ dataGridView = new DataGridView(); buttonAdd = new Button(); panel1 = new Panel(); + buttonDel = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); panel1.SuspendLayout(); SuspendLayout(); @@ -58,7 +59,7 @@ // buttonAdd.BackgroundImage = Properties.Resources.Ambox_plus_svg; buttonAdd.BackgroundImageLayout = ImageLayout.Stretch; - buttonAdd.Location = new Point(48, 112); + buttonAdd.Location = new Point(42, 46); buttonAdd.Name = "buttonAdd"; buttonAdd.Size = new Size(94, 93); buttonAdd.TabIndex = 0; @@ -67,6 +68,7 @@ // // panel1 // + panel1.Controls.Add(buttonDel); panel1.Controls.Add(buttonAdd); panel1.Dock = DockStyle.Right; panel1.Location = new Point(622, 0); @@ -74,6 +76,17 @@ panel1.Size = new Size(178, 315); panel1.TabIndex = 4; // + // buttonDel + // + buttonDel.BackgroundImage = Properties.Resources.minus_PNG39; + buttonDel.BackgroundImageLayout = ImageLayout.Stretch; + buttonDel.Location = new Point(42, 175); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(94, 93); + buttonDel.TabIndex = 4; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; + // // FormShedules // AutoScaleDimensions = new SizeF(8F, 20F); @@ -94,5 +107,6 @@ private DataGridView dataGridView; private Button buttonAdd; private Panel panel1; + private Button buttonDel; } } \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Forms/FormShedules.cs b/ProjectShedule/ProjectShedule/Forms/FormShedules.cs index 9444ef1..5076e43 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormShedules.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormShedules.cs @@ -1,6 +1,4 @@ - -using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; +using ProjectShedule.Repositories; using Unity; namespace ProjectShedule.Forms @@ -45,5 +43,42 @@ namespace ProjectShedule.Forms } } private void LoadList() => dataGridView.DataSource = _sheduleRepository.ReadShedule(); + + private void buttonDel_Click(object sender, EventArgs e) + { + if (TryGetIdentifierFromSelectedRow(out var findId)) + { + return; + } + + if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + try + { + _sheduleRepository.DeleteShedule(findId); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private bool TryGetIdentifierFromSelectedRow(out int id) + { + id = 0; + if (dataGridView.SelectedRows.Count < 1) + { + MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return true; + } + else + { + id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[id].Value); + + return false; + } + } } } diff --git a/ProjectShedule/ProjectShedule/Forms/FormSubject.cs b/ProjectShedule/ProjectShedule/Forms/FormSubject.cs index f07f7d5..34d48cd 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormSubject.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormSubject.cs @@ -1,16 +1,6 @@ -using ProjectShedule.Entities.Enums; -using ProjectShedule.Entities; +using ProjectShedule.Entities; using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; + namespace ProjectShedule.Forms; @@ -60,7 +50,7 @@ public partial class FormSubject : Form } else { - _subjectRepository.UpdateSubject(CreateSubject(0)); + _subjectRepository.CreateSubject(CreateSubject(0)); } Close(); } diff --git a/ProjectShedule/ProjectShedule/Forms/FormSubjects.Designer.cs b/ProjectShedule/ProjectShedule/Forms/FormSubjects.Designer.cs index ab0ff91..974c208 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormSubjects.Designer.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormSubjects.Designer.cs @@ -89,7 +89,6 @@ dataGridView.AllowUserToResizeRows = false; dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Fill; dataGridView.Location = new Point(0, 0); dataGridView.MultiSelect = false; dataGridView.Name = "dataGridView"; @@ -97,7 +96,7 @@ dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersWidth = 51; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(800, 354); + dataGridView.Size = new Size(623, 354); dataGridView.TabIndex = 3; // // FormSubjects diff --git a/ProjectShedule/ProjectShedule/Forms/FormSubjects.cs b/ProjectShedule/ProjectShedule/Forms/FormSubjects.cs index 391075a..a371e76 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormSubjects.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormSubjects.cs @@ -1,14 +1,4 @@ using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using Unity; namespace ProjectShedule.Forms diff --git a/ProjectShedule/ProjectShedule/Forms/FormTeacher.cs b/ProjectShedule/ProjectShedule/Forms/FormTeacher.cs index 646a3de..a498643 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormTeacher.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormTeacher.cs @@ -1,15 +1,5 @@ using ProjectShedule.Entities; using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; namespace ProjectShedule.Forms { @@ -61,7 +51,7 @@ namespace ProjectShedule.Forms } else { - _teacherRepository.UpdateTeacher(CreateTeacher(0)); + _teacherRepository.CreateTeacher(CreateTeacher(0)); } Close(); } diff --git a/ProjectShedule/ProjectShedule/Forms/FormTeachers.cs b/ProjectShedule/ProjectShedule/Forms/FormTeachers.cs index 61fac0d..80d9872 100644 --- a/ProjectShedule/ProjectShedule/Forms/FormTeachers.cs +++ b/ProjectShedule/ProjectShedule/Forms/FormTeachers.cs @@ -1,14 +1,4 @@ using ProjectShedule.Repositories; -using ProjectShedule.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using Unity; namespace ProjectShedule.Forms diff --git a/ProjectShedule/ProjectShedule/Program.cs b/ProjectShedule/ProjectShedule/Program.cs index 820317e..423f827 100644 --- a/ProjectShedule/ProjectShedule/Program.cs +++ b/ProjectShedule/ProjectShedule/Program.cs @@ -1,6 +1,10 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using ProjectShedule.Repositories; using ProjectShedule.Repositories.Implementations; +using Serilog; using Unity; +using Unity.Microsoft.Logging; namespace ProjectShedule { @@ -21,6 +25,9 @@ namespace ProjectShedule { var container = new UnityContainer(); + container.AddExtension(new +LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); @@ -28,8 +35,21 @@ namespace ProjectShedule 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/ProjectShedule/ProjectShedule/ProjectShedule.csproj b/ProjectShedule/ProjectShedule/ProjectShedule.csproj index accbdf0..f946d78 100644 --- a/ProjectShedule/ProjectShedule/ProjectShedule.csproj +++ b/ProjectShedule/ProjectShedule/ProjectShedule.csproj @@ -9,7 +9,19 @@ + + + + + + + + + + + + @@ -27,4 +39,12 @@ + + + Always + + + + + \ No newline at end of file diff --git a/ProjectShedule/ProjectShedule/Repositories/IConnectionString.cs b/ProjectShedule/ProjectShedule/Repositories/IConnectionString.cs new file mode 100644 index 0000000..ea0e1e2 --- /dev/null +++ b/ProjectShedule/ProjectShedule/Repositories/IConnectionString.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectShedule.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get;} +} diff --git a/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs b/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs index 36af6d7..152ce12 100644 --- a/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/ICurriculumSubjectRepository.cs @@ -5,5 +5,6 @@ namespace ProjectShedule.Repositories; public interface ICurriculumSubjectRepository { IEnumerable ReadCurriculumSubject(); + void DeleteCurriculumSubject(int id); void CreateCurriculumSubject(CurriculumSubject curriculumSubject); } diff --git a/ProjectShedule/ProjectShedule/Repositories/IGroupRepository.cs b/ProjectShedule/ProjectShedule/Repositories/IGroupRepository.cs index 1dbb0e5..d896be2 100644 --- a/ProjectShedule/ProjectShedule/Repositories/IGroupRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/IGroupRepository.cs @@ -4,9 +4,9 @@ namespace ProjectShedule.Repositories; public interface IGroupRepository { - IEnumerable ReadGroup(); - Group ReadGroupById(int id); - void CreateGroup(Group group); - void UpdateGroup(Group group); + IEnumerable ReadGroup(); + StudentsGroup ReadGroupById(int id); + void CreateGroup(StudentsGroup group); + void UpdateGroup(StudentsGroup group); void DeleteGroup(int id); } diff --git a/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs b/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs index bd2f01b..22df657 100644 --- a/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/ISheduleRepository.cs @@ -6,6 +6,5 @@ public interface ISheduleRepository { IEnumerable ReadShedule(DateTime? dateTime = null, int? groupId = null); void CreateShedule(Shedule shedule); - void UpdateShedule(Shedule shedule); void DeleteShedule(int id); } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/ClassroomRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/ClassroomRepository.cs index c733ac7..3b4fdd3 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/ClassroomRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/ClassroomRepository.cs @@ -1,28 +1,124 @@ -using ProjectShedule.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; namespace ProjectShedule.Repositories.Implementations; public class ClassroomRepository : IClassroomRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public ClassroomRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateClassroom(Classroom classroom) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(classroom)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Classroom (Size, Name, ClassType) + VALUES (@Size, @Name, @ClassType)"; + connection.Execute(queryInsert, classroom); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } public void DeleteClassroom(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Classroom + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadClassroom() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Classroom"; + var classroms = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(classroms)); + return classroms; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Classroom ReadClassroomById(int id) { - return Classroom.CreateClassroom(0, 0, string.Empty, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Classroom + WHERE Id= @id"; + var classroom = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(classroom)); + return classroom; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public void UpdateClassroom(Classroom classroom) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(classroom)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Classroom + SET + Size= @Size, + Name= @Name, + ClassType = @ClassType + WHERE Id= @Id"; + connection.Execute(queryUpdate, classroom); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/ConnectionString.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..2ca1a05 --- /dev/null +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/ConnectionString.cs @@ -0,0 +1,6 @@ +namespace ProjectShedule.Repositories.Implementations; + +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=Xaliullov05;Database=otp;Include Error Detail=true"; +} diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumRepository.cs index 59935bc..f280168 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumRepository.cs @@ -1,33 +1,119 @@ -using ProjectShedule.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; namespace ProjectShedule.Repositories.Implementations; public class CurriculumRepository : ICurriculumRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public CurriculumRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateCurriculum(Curriculum curriculum) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(curriculum)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Curriculum (Direction) + VALUES (@Direction)"; + connection.Execute(queryInsert, curriculum); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteCurriculum(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Curriculum + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadCurriculum() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Curriculum"; + var curriculums = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(curriculums)); + return curriculums; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Curriculum ReadCurriculumById(int id) { - return Curriculum.CreateCurriculum(0, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Curriculum + WHERE Id= @id"; + var curriculum = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(curriculum)); + return curriculum; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public void UpdateCurriculum(Curriculum curriculum) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(curriculum)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Curriculum + SET + Direction= @Direction + WHERE Id= @Id"; + connection.Execute(queryUpdate, curriculum); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs index 67bc64c..f8c8d44 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/CurriculumSubjectRepository.cs @@ -1,20 +1,93 @@ -using ProjectShedule.Entities; -using ProjectShedule.Entities.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; namespace ProjectShedule.Repositories.Implementations; public class CurriculumSubjectRepository : ICurriculumSubjectRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public CurriculumSubjectRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateCurriculumSubject(CurriculumSubject curriculumSubject) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(curriculumSubject)); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = + @" + INSERT INTO CurriculumSubject (CurriculumId, Course) + VALUES (@CurriculumId, @Course); + SELECT MAX(Id) FROM CurriculumSubject"; + var curriculumSubjectId = + connection.QueryFirst(queryInsert, curriculumSubject, transaction); + var querySubInsert = @" + INSERT INTO SubjectSubject (CurriculumSubjectId, SubjectId, LessonsCount) + VALUES (@CurriculumSubjectId, @SubjectId, @LessonsCount)"; + + foreach (var elem in curriculumSubject.SubjectSubject) + { + connection.Execute(querySubInsert, new + { + curriculumSubjectId, + elem.SubjectId, + elem.LessonsCount + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } + public void DeleteCurriculumSubject(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM CurriculumSubject + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadCurriculumSubject() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM CurriculumSubject"; + var curriculumSubjects = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(curriculumSubjects)); + return curriculumSubjects; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/GroupRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/GroupRepository.cs index 30f3c2e..d24f9f6 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/GroupRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/GroupRepository.cs @@ -1,33 +1,123 @@ -using ProjectShedule.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; + namespace ProjectShedule.Repositories.Implementations; public class GroupRepository : IGroupRepository { - public void CreateGroup(Group group) + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public GroupRepository(IConnectionString connectionString, ILogger logger) { + _connectionString = connectionString; + _logger = logger; + } + public void CreateGroup(StudentsGroup group) + { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(group)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO StudentsGroup (StudentsCount, GroupNumber, Course, CurriculumId) + VALUES (@StudentsCount, @GroupNumber, @Course, @CurriculumId)"; + 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 StudentsGroup + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - public IEnumerable ReadGroup() + public IEnumerable ReadGroup() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM StudentsGroup"; + var groups = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(groups)); + return groups; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } - public Group ReadGroupById(int id) + public StudentsGroup ReadGroupById(int id) { - return Group.CreateGroup(0, 0, string.Empty, 0, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM StudentsGroup + 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) + public void UpdateGroup(StudentsGroup group) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(group)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE StudentsGroup + SET + StudentsCount= @StudentsCount, + GroupNumber= @GroupNumber, + Course= @Course, + CurriculumId= @CurriculumId + WHERE Id= @Id"; + connection.Execute(queryUpdate, group); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs index 429b15b..a80aecd 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/SheduleRepository.cs @@ -1,28 +1,75 @@ -using ProjectShedule.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; namespace ProjectShedule.Repositories.Implementations; public class SheduleRepository : ISheduleRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public SheduleRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateShedule(Shedule shedule) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(shedule)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Shedule (Date, PairNumber, StudentsGroupId, TeacherId, SubjectId, ClassroomId) + VALUES (@Date, @PairNumber, @StudentsGroupId, @TeacherId, @SubjectId, @ClassroomId)"; + connection.Execute(queryInsert, shedule); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteShedule(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Shedule + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadShedule(DateTime? dateTime = null, int? groupId = null) { - return []; - } - - public void UpdateShedule(Shedule shedule) - { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Shedule"; + var shedule = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(shedule)); + return shedule; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/SubjectRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/SubjectRepository.cs index 751a977..60190d8 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/SubjectRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/SubjectRepository.cs @@ -1,33 +1,119 @@ -using ProjectShedule.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; namespace ProjectShedule.Repositories.Implementations; public class SubjectRepository : ISubjectRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public SubjectRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateSubject(Subject subject) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(subject)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Subject (Name) + VALUES (@Name)"; + connection.Execute(queryInsert, subject); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteSubject(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Subject + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadSubject() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Subject"; + var subjects = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(subjects)); + return subjects; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Subject ReadSubjectById(int id) { - return Subject.CreateSubject(0,string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Subject + WHERE Id= @id"; + var subject = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(subject)); + return subject; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public void UpdateSubject(Subject subject) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(subject)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Subject + SET + Name = @Name + WHERE Id= @Id"; + connection.Execute(queryUpdate, subject); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/Repositories/Implementations/TeacherRepository.cs b/ProjectShedule/ProjectShedule/Repositories/Implementations/TeacherRepository.cs index 37b3861..eb890f2 100644 --- a/ProjectShedule/ProjectShedule/Repositories/Implementations/TeacherRepository.cs +++ b/ProjectShedule/ProjectShedule/Repositories/Implementations/TeacherRepository.cs @@ -1,33 +1,120 @@ -using ProjectShedule.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectShedule.Entities; namespace ProjectShedule.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 (FirstName, LastName) + VALUES (@FirstName, @LastName)"; + 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 teachers = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(teachers)); + return teachers; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Teacher ReadTeacherById(int id) { - return Teacher.CreateTeacher(0,string.Empty,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 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, + LastName = @FirstName, + WHERE Id= @Id"; + connection.Execute(queryUpdate, teacher); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectShedule/ProjectShedule/appsettings.json b/ProjectShedule/ProjectShedule/appsettings.json new file mode 100644 index 0000000..9954497 --- /dev/null +++ b/ProjectShedule/ProjectShedule/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/log.txt", + "rollingInterval": "Day" + } + } + ] + } +}