SUBD_SportCompetitions/SportCompetitionsView/FormResult.cs

86 lines
2.4 KiB
C#

using SportCompetitionsContracts.BindingModels;
using SportCompetitionsContracts.BusinessLogicsContracts;
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace SportCompetitionsView
{
public partial class FormResult : Form
{
private readonly ICompetitionLogic _logicC;
private readonly ITeamLogic _teamLogic;
private readonly IResultLogic _resultLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormResult(ICompetitionLogic _logicC2, ITeamLogic _teamLogic2, IResultLogic _resultLogic2)
{
InitializeComponent();
_logicC = _logicC2;
_teamLogic = _teamLogic2;
_resultLogic = _resultLogic2;
}
private void FormResult_Load(object sender, EventArgs e)
{
var _listP = _logicC.ReadList(null);
if (_listP != null)
{
comboBox1.DisplayMember = "CompetitionName";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = _listP;
comboBox1.SelectedItem = null;
}
var _listP2 = _teamLogic.ReadList(null);
if (_listP2 != null)
{
comboBox2.DisplayMember = "TeamName";
comboBox2.ValueMember = "Id";
comboBox2.DataSource = _listP2;
comboBox2.SelectedItem = null;
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
var model = new ResultBindingModel
{
Id = _id ?? 0,
CompetitionId = comboBox1.SelectedIndex,
TeamId = comboBox2.SelectedIndex,
ResultName = textBox1.Text,
ResultPosition = Convert.ToInt32(textBox.Text),
};
var operationResult = _id.HasValue ? _resultLogic.Update(model) : _resultLogic.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();
}
}
}