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

87 lines
2.8 KiB
C#

using JournalContracts.BindingModels;
using JournalContracts.BusinessLogicContracts;
using JournalContracts.SearchModels;
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 FormGroup : Form
{
private readonly IGroupLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormGroup(IGroupLogic logic)
{
InitializeComponent();
_logic = logic;
}
private void FormMenu_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _logic.ReadElement(new GroupSearchModel { Id = _id.Value });
if (view != null)
{
textBoxName.Text = view.GroupName;
textBoxCourse.Text = view.Course.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxName.Text) || string.IsNullOrEmpty(textBoxCourse.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new GroupBindingModel
{
Id = _id ?? 0,
GroupName = textBoxName.Text,
Course = Convert.ToInt32(textBoxCourse.Text),
};
var operationResult = _id.HasValue ? _logic.Update(model) : _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();
}
}
}