Rogashova_E.A._CourseWork_H.../HospitalView/FormMedicine.cs

96 lines
3.4 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using Microsoft.Extensions.Logging;
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 HospitalView
{
public partial class FormMedicine : Form
{
private readonly ILogger _logger;
private readonly IMedicinesLogic _logic;
private int? _id;
public int Id { set { _id = value; } }
public FormMedicine(ILogger<FormMedicine> logger, IMedicinesLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormMedicines_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
_logger.LogInformation("Получение лекарства");
var view = _logic.ReadElement(new MedicinesSearchModel
{
Id = _id.Value
});
if (view != null)
{
textBoxNameMedicine.Text = view.MedicinesName;
textBoxGroup.Text = view.Group;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения лекарства");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxNameMedicine.Text))
{
MessageBox.Show("Заполните название", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение лекарства");
try
{
var model = new MedicinesBindingModel
{
Id = _id ?? 0,
MedicinesName = textBoxNameMedicine.Text,
Group = textBoxGroup.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)
{
_logger.LogError(ex, "Ошибка сохранения лекарства");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}