PIbd-21_SagirovM.M._SUBD/Journal/FormTeacher.cs
2024-05-04 01:10:09 +04:00

186 lines
6.4 KiB
C#

using JournalContracts.BindingModels;
using JournalContracts.BusinessLogicContracts;
using JournalContracts.SearchModels;
using JournalDataModels.Models;
using JournalView;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JournalView
{
public partial class FormTeacher : Form
{
private readonly ISubjectLogic _logicP;
private readonly ITeacherLogic _logicC;
private int? _id;
private Dictionary<int, ISubjectModel> _teacherSubjects;
public int Id { set { _id = value; } }
public FormTeacher(ISubjectLogic plogic, ITeacherLogic clogic)
{
InitializeComponent();
_logicP = plogic;
_logicC = clogic;
_teacherSubjects = new Dictionary<int, ISubjectModel>();
}
private void FormTeacher_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logicC.ReadElement(new TeacherSearchModel { Id = _id.Value });
if (view != null)
{
textBoxFCS.Text = view.TeacherName;
textBoxCafedra.Text = view.Cafedra;
textBoxPosition.Text = view.Position;
textBoxExperience.Text = view.Experience.ToString();
_teacherSubjects = view.TeacherSubjects ?? new Dictionary<int, ISubjectModel>();
LoadData();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
try
{
if (_teacherSubjects != null)
{
dataGridView.Rows.Clear();
foreach (var rc in _teacherSubjects)
{
dataGridView.Rows.Add(new object[] {rc.Key ,rc.Value.SubjectName });
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSelectSubject));
if (service is FormSelectSubject form)
{
if (form.ShowDialog() == DialogResult.OK)
{
if (form.SubjectModel == null)
{
return;
}
if (_teacherSubjects.ContainsKey(form.Id))
{
_teacherSubjects[form.Id] = form.SubjectModel;
}
else
{
_teacherSubjects.Add(form.Id, form.SubjectModel);
}
LoadData();
}
}
}
private void buttonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSelectSubject));
if (service is FormSelectSubject form)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
if (form.ShowDialog() == DialogResult.OK)
{
if (form.SubjectModel == null)
{
return;
}
_teacherSubjects[form.Id] = form.SubjectModel;
LoadData();
}
}
}
}
private void buttonDel_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
_teacherSubjects?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (_teacherSubjects == null || _teacherSubjects.Count == 0)
{
MessageBox.Show("Заполните предметы", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new TeacherBindingModel
{
Id = _id ?? 0,
TeacherName = textBoxFCS.Text,
TeacherSubjects = _teacherSubjects,
Cafedra = textBoxCafedra.Text,
Experience = Convert.ToInt32(textBoxExperience.Text),
Position = textBoxPosition.Text,
};
var operationResult = _id.HasValue ? _logicC.Update(model) : _logicC.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Доп информация в логах");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}