using ElectronicDiaryAbstractions.Models; using ElectronicDiaryAbstractions.WorkAbstractions; namespace ElectronicDiaryView { public partial class FormHomework : Form { private readonly IHomeworkWork _homeworkLogic; private readonly IMarkWork _markLogic; public FormHomework(IHomeworkWork homeworkLogic, IMarkWork markLogic) { InitializeComponent(); _homeworkLogic = homeworkLogic; _markLogic = markLogic; } private void FormHomework_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { var homeworks = _homeworkLogic.GetAll(); dataGridView.Rows.Clear(); if (dataGridView.ColumnCount == 0) { dataGridView.Columns.Add("Id", "ID"); dataGridView.Columns.Add("Description", "Описание"); dataGridView.Columns.Add("Deadline", "Срок сдачи"); dataGridView.Columns.Add("IsAccepted", "Состояние сдачи"); dataGridView.Columns.Add("MarkId", "MarkId"); dataGridView.Columns["MarkId"].Visible = false; dataGridView.Columns.Add("Mark", "Оценка"); } dataGridView.Columns["Description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Deadline"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["IsAccepted"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Mark"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; comboBoxMark.DataSource = _markLogic.GetAll(); comboBoxMark.DisplayMember = "Value"; comboBoxMark.ValueMember = "Id"; foreach (var homework in homeworks) { dataGridView.Rows.Add(homework.Id, homework.Description, homework.Deadline, homework.IsAccepted ? "Принято" : "Не принято", homework.MarkId, _markLogic.Get(homework.MarkId)?.Value); } } private void ButtonCreate_Click(object sender, EventArgs e) { Homework newHomework = new() { Description = textBoxDescription.Text, Deadline = dateTimePickerDeadline.Value, IsAccepted = checkBoxIsAccepted.Checked, MarkId = ((Mark?)comboBoxMark.SelectedItem)?.Id ?? 0, }; _homeworkLogic.Create(newHomework); LoadData(); } private void ButtonUpdate_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; int homeworkId = Convert.ToInt32(selectedRow.Cells["Id"].Value); Homework updatedHomework = new() { Id = homeworkId, Description = textBoxDescription.Text, Deadline = dateTimePickerDeadline.Value, IsAccepted = checkBoxIsAccepted.Checked, MarkId = ((Mark?)comboBoxMark.SelectedItem)?.Id ?? 0, }; _homeworkLogic.Update(updatedHomework); LoadData(); } else { MessageBox.Show("Пожалуйста, выберите домашнее задание, которое необходимо обновить"); } } private void ButtonDelete_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; int homeworkId = Convert.ToInt32(selectedRow.Cells["Id"].Value); _homeworkLogic.Delete(homeworkId); LoadData(); } else { MessageBox.Show("Пожалуйста, выберите домашнее задание, которое необходимо удалить"); } } private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView.Rows[e.RowIndex]; textBoxDescription.Text = row.Cells["Description"].Value.ToString(); dateTimePickerDeadline.Value = Convert.ToDateTime(row.Cells["Deadline"].Value); checkBoxIsAccepted.Checked = row.Cells["IsAccepted"].Value.ToString() == "Принято"; comboBoxMark.SelectedValue = Convert.ToInt32(row.Cells["MarkId"].Value); } } } }