94 lines
2.6 KiB
C#
94 lines
2.6 KiB
C#
using PersonnelDepartmentBusinessLogic.BusinessLogics;
|
|
using PersonnelDepartmentContracts.BindingModels;
|
|
using PersonnelDepartmentContracts.BusinessLogicContracts;
|
|
using PersonnelDepartmentContracts.SearchModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace PersonnelDepartmentView
|
|
{
|
|
public partial class FormDepartment : Form
|
|
{
|
|
private readonly IDepartmentLogic _departmentLogic;
|
|
private int? _id;
|
|
public int Id { set { _id = value; } }
|
|
public FormDepartment(IDepartmentLogic departmentLogic)
|
|
{
|
|
InitializeComponent();
|
|
_departmentLogic = departmentLogic;
|
|
}
|
|
|
|
private void FormDepartment_Load(object sender, EventArgs e)
|
|
{
|
|
if (_id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
var view = _departmentLogic.ReadElement(new DepartmentSearchModel
|
|
{
|
|
Id = _id.Value
|
|
});
|
|
if (view != null)
|
|
{
|
|
textBoxName.Text = view.Name;
|
|
textBoxTelephone.Text = view.Telephone.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))
|
|
{
|
|
MessageBox.Show("Введите название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(textBoxTelephone.Text))
|
|
{
|
|
MessageBox.Show("Введите контактный номер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var model = new DepartmentBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
Name = textBoxName.Text,
|
|
Telephone = long.Parse(textBoxTelephone.Text)
|
|
};
|
|
var operationResult = _id.HasValue ? _departmentLogic.Update(model) : _departmentLogic.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();
|
|
}
|
|
}
|
|
}
|