using HotelContracts.BindingModels; using HotelContracts.BusinessLogicsContracts; using HotelContracts.SearchModels; using HotelDatabaseImplement.Models; 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 HotelView { public partial class FormRoom : Form { private readonly IRoomLogic _logicR; private readonly IWorkerLogic _logicW; private int? _id; public int Id { set { _id = value; } } public FormRoom(IWorkerLogic logicW, IRoomLogic logicR) { InitializeComponent(); _logicW = logicW; _logicR = logicR; } private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxNumber.Text)) { MessageBox.Show("Заполните номер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxWorker.SelectedValue == null) { MessageBox.Show("Выберите рабочего", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new RoomBindingModel { Id = _id ?? 0, Number = Convert.ToInt32(textBoxNumber.Text), Floor = Convert.ToInt32(textBoxFloor.Text), NumberOfBeds = Convert.ToInt32(textBoxNumberOfBeds.Text), Condition = textBoxCondition.Text, Cost = Convert.ToInt32(textBoxCost.Text), WorkerId = Convert.ToInt32(comboBoxWorker.SelectedValue), }; var operationResult = _id.HasValue ? _logicR.Update(model) : _logicR.Create(model); var provercaMade = _logicR.ProvercaMade(model); if (!provercaMade) { _logicR.Delete(model); throw new Exception("Данный работник не горнечная"); } 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 void FormRoom_Load(object sender, EventArgs e) { try { var workerList = _logicW.ReadList(null); if (workerList != null) { comboBoxWorker.DisplayMember = "FIO"; comboBoxWorker.ValueMember = "Id"; comboBoxWorker.DataSource = workerList; comboBoxWorker.SelectedItem = null; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }