StudentEnrollmentNew/StudentEnrollment/StudentEnrollmentView/FormExamPoints.cs

106 lines
3.3 KiB
C#

using StudentEnrollmentContracts.BindingModels;
using StudentEnrollmentContracts.BusinessLogicContracts;
using StudentEnrollmentContracts.SearchModels;
using StudentEnrollmentDatabaseImplement.Models;
using StudentEnrollmentDataModels.Models;
using System.Windows.Forms;
namespace StudentEnrollmentView
{
public partial class FormExamPoints : Form
{
public int? Id { get; set; }
public IExamPointsModel ExamPoints { get; }
private IExamPointsLogic _logic;
public FormExamPoints(IExamPointsLogic logic)
{
_logic = logic;
InitializeComponent();
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxFirstExamPoints.Text))
{
MessageBox.Show("Заполните баллы за первый экзамен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxSecondExamPoints.Text))
{
MessageBox.Show("Заполните баллы за второй экзамен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxThirdExamPoints.Text))
{
MessageBox.Show("Заполните баллы за третий экзамен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new ExamPointsBindingModel
{
Id = Id ?? 0,
FirstExamPoints = Convert.ToInt32(textBoxFirstExamPoints.Text),
SecondExamPoints = Convert.ToInt32(textBoxSecondExamPoints.Text),
ThirdExamPoints = Convert.ToInt32(textBoxThirdExamPoints.Text),
AddPoints = textBoxAddPoints.Text != string.Empty ? Convert.ToInt32(textBoxAddPoints.Text) : 0,
Summary = CalcSum(),
};
var operationResult = _logic.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();
}
private int CalcSum()
{
return Convert.ToInt32(textBoxFirstExamPoints.Text)
+ Convert.ToInt32(textBoxSecondExamPoints.Text)
+ Convert.ToInt32(textBoxThirdExamPoints.Text)
+ (textBoxAddPoints.Text != string.Empty ? Convert.ToInt32(textBoxAddPoints.Text) : 0);
}
private void FormExamPoints_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
if (Id != 0)
{
try
{
var view = _logic.ReadElement(new ExamPointsSearchModel
{
exampoints_id = Id,
});
textBoxFirstExamPoints.Text = view.FirstExamPoints.ToString();
textBoxSecondExamPoints.Text = view.SecondExamPoints.ToString();
textBoxThirdExamPoints.Text = view.ThirdExamPoints.ToString();
textBoxAddPoints.Text = view.AddPoints.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}