2024-11-25 08:31:54 +04:00

74 lines
2.5 KiB
C#

using ProjectHotel.Entities;
using ProjectHotel.Repositories;
using ProjectHotel.Repositories.Implementations;
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 FormPlacingAthlete : Form
{
private readonly IPlacingAthleteRepository _placingAthleteRepository;
public FormPlacingAthlete(IPlacingAthleteRepository placingAthleteRepository,
IRoomRepository roomRepository,
IAthleteRepository athleteRepository)
{
InitializeComponent();
_placingAthleteRepository = placingAthleteRepository ??
throw new ArgumentNullException(nameof(placingAthleteRepository));
comboBoxRoom.DataSource = roomRepository.ReadRooms();
comboBoxRoom.DisplayMember = "Name";
comboBoxRoom.ValueMember = "Id";
ColumnAthlete.DataSource = athleteRepository.ReadAthletes();
ColumnAthlete.DisplayMember = "FirstName";
ColumnAthlete.ValueMember = "Id";
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (dataGridView.RowCount < 1 ||
comboBoxRoom.SelectedIndex < 0)
{
throw new Exception("Имеются незаполненные поля");
}
_placingAthleteRepository.CreatePlacingAthlete(PlacingAthlete.CreateOpeartion(0,(int)comboBoxRoom.SelectedValue!,
CreateListAthletePlacingAthleteFromDataGrid()));
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private List<AthletePlacingAthlete>
CreateListAthletePlacingAthleteFromDataGrid()
{
var list = new List<AthletePlacingAthlete>();
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["ColumnFeed"].Value == null ||
row.Cells["ColumnCount"].Value == null)
{
continue;
}
list.Add(AthletePlacingAthlete.CreateElement(0,
Convert.ToInt32(row.Cells["ColumnAthlete"].Value),
Convert.ToInt32(row.Cells["ColumnLengthOfStay"].Value)));
}
return list;
}
}