SUBD_SportCompetitions/SportCompetitionsView/FormMember.cs

80 lines
2.1 KiB
C#

using Microsoft.VisualBasic.Logging;
using SportCompetitionsContracts.BindingModels;
using SportCompetitionsContracts.BusinessLogicsContracts;
using SportCompetitionsContracts.SearchModels;
using SportCompetitionsContracts.ViewModels;
using SportCompetitionsDataModels.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 SportCompetitionsView
{
public partial class FormMember : Form
{
private readonly IMemberLogic _logicM;
private readonly ITeamLogic _logicT;
private int? _id;
public int Id { set { _id = value; } }
public FormMember(IMemberLogic logicM, ITeamLogic logicT)
{
InitializeComponent();
_logicM = logicM;
_logicT = logicT;
}
private void FormMember_Load(object sender, EventArgs e)
{
var _listP = _logicT.ReadList(null);
if (_listP != null)
{
comboBoxTeam.DisplayMember = "TeamName";
comboBoxTeam.ValueMember = "Id";
comboBoxTeam.DataSource = _listP;
comboBoxTeam.SelectedItem = null;
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
var model = new MemberBindingModel
{
Id = _id ?? 0,
MemberFCs = textBoxFCs.Text,
MemberBirthDate = dateTimePicker1.Value,
MemberGender = textBoxGender.Text,
TeamId = comboBoxTeam.SelectedIndex+1,
};
var operationResult = _id.HasValue ? _logicM.Update(model) : _logicM.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();
}
}
}