87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using ProjectHotel.Entities;
|
|
using ProjectHotel.Repositories;
|
|
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 ProjectHotel.Forms
|
|
{
|
|
public partial class FormRoom : Form
|
|
{
|
|
private readonly IRoomRepository _roomRepository;
|
|
private int? _roomId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var room = _roomRepository.ReadRoomById(value);
|
|
if (room == null || comboBoxHotel.SelectedIndex < 0)
|
|
{
|
|
throw new
|
|
InvalidDataException(nameof(room));
|
|
}
|
|
textBoxRoomName.Text = room.Name;
|
|
numericUpDownCapacity.Value = room.Capacity;
|
|
_roomId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormRoom(IRoomRepository roomRepository, IHotelRepository hotelRepository)
|
|
{
|
|
InitializeComponent();
|
|
_roomRepository = roomRepository ??
|
|
throw new ArgumentNullException(nameof(roomRepository));
|
|
comboBoxHotel.DataSource = hotelRepository.ReadHotels();
|
|
comboBoxHotel.DisplayMember = "Name";
|
|
comboBoxHotel.ValueMember = "Id";
|
|
}
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(textBoxRoomName.Text))
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_roomId.HasValue)
|
|
{
|
|
_roomRepository.UpdateRoom(CreateRoom(_roomId.Value));
|
|
}
|
|
else
|
|
{
|
|
_roomRepository.CreateRoom(CreateRoom(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private Room CreateRoom(int id) => Room.CreateEntity(id, (int)comboBoxHotel.SelectedValue!, textBoxRoomName.Text, Convert.ToInt32(numericUpDownCapacity.Value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|