PIbd-21_Rodionov_I.A._SUBD/ElectronicDiary/ElectronicDiaryView/FormTeacher.cs
2024-05-07 19:37:44 +04:00

202 lines
7.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ElectronicDiaryAbstractions.Models;
using ElectronicDiaryAbstractions.WorkAbstractions;
namespace ElectronicDiaryView
{
public partial class FormTeacher : Form
{
private readonly ITeacherWork _teacherLogic;
private readonly IUserWork _userLogic;
private readonly ISubjectWork _subjectLogic;
public FormTeacher(ITeacherWork teacherLogic, IUserWork userLogic, ISubjectWork subjectLogic)
{
InitializeComponent();
_teacherLogic = teacherLogic;
_userLogic = userLogic;
_subjectLogic = subjectLogic;
}
private void FormTeacher_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
var teachers = _teacherLogic.GetAll();
dataGridView.Rows.Clear();
if (dataGridView.ColumnCount == 0)
{
dataGridView.Columns.Add("Id", "ID");
dataGridView.Columns.Add("Name", "Имя учителя");
dataGridView.Columns.Add("UserId", "UserId");
dataGridView.Columns["UserId"].Visible = false;
dataGridView.Columns.Add("UserLogin", "Логин");
}
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["UserLogin"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
comboBoxSubject.DataSource = _subjectLogic.GetAll();
comboBoxSubject.DisplayMember = "Name";
comboBoxSubject.ValueMember = "Id";
foreach (var teacher in teachers)
{
dataGridView.Rows.Add(teacher.Id, teacher.LastName + " " + teacher.FirstName + " " + teacher.Patronymic, teacher.UserId, _userLogic.Get(teacher.UserId)?.Login);
}
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
int someNumber = _userLogic.GetAll().Max(user => user.Id) + 1;
User newUser = new()
{
Login = "login" + someNumber,
Email = "email" + someNumber,
Password = "password" + someNumber,
PhoneNumber = "phonenumber" + someNumber,
AccessLevel = "Учитель",
};
_userLogic.Create(newUser);
int newUserId = _userLogic.GetAll().Max(user => user.Id);
Teacher newTeacher = new()
{
LastName = textBoxLastName.Text,
FirstName = textBoxFirstName.Text,
Patronymic = textBoxPatronymic.Text,
UserId = newUserId,
};
_teacherLogic.Create(newTeacher);
LoadData();
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int teacherId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
Teacher updatedTeacher = new()
{
Id = teacherId,
LastName = textBoxLastName.Text,
FirstName = textBoxFirstName.Text,
Patronymic = textBoxPatronymic.Text,
};
_teacherLogic.Update(updatedTeacher);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите учителя, информацию о котором необходимо обновить");
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int teacherId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_teacherLogic.Delete(teacherId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите учителя, информацию о котором необходимо удалить");
}
}
private void ButtonGetSubjects_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSubjects));
if (service is FormSubjects form)
{
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
form.IsStudent = false;
form.ShowDialog();
}
}
else
{
MessageBox.Show("Пожалуйста, выберите учителя, информацию о предметах которого необходимо посмотреть");
}
}
private void ButtonAddSubject_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int teacherId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_teacherLogic.AddTeacherSubject(teacherId, ((Subject)comboBoxSubject.SelectedItem).Id);
}
else
{
MessageBox.Show("Пожалуйста, выберите учителя, с которым необходимо связать предмет");
}
}
private void ButtonDeleteSubject_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int teacherId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_teacherLogic.DeleteTeacherSubject(teacherId, ((Subject)comboBoxSubject.SelectedItem).Id);
}
else
{
MessageBox.Show("Пожалуйста, выберите учителя, от которого необходимо отвязать предмет");
}
}
private void ButtonDeleteSubjects_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
int teacherId = Convert.ToInt32(selectedRow.Cells["Id"].Value);
_teacherLogic.DeleteAllTeacherSubjects(teacherId);
LoadData();
}
else
{
MessageBox.Show("Пожалуйста, выберите учителя, информацию о предметах которого необходимо удалить");
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
string[] nameParts = (row != null && row.Cells["Name"] != null) ? row.Cells["Name"].Value.ToString().Split(' ') : new string[] { "", "", "" };
textBoxLastName.Text = nameParts[0];
textBoxFirstName.Text = nameParts[1];
textBoxPatronymic.Text = nameParts[2];
}
}
}
}