121 lines
4.9 KiB
C#
121 lines
4.9 KiB
C#
using StudentPerformanceContracts.BindingModels;
|
||
using StudentPerformanceContracts.BusinessLogicContracts;
|
||
using StudentPerformanceContracts.SearchModels;
|
||
using StudentPerformanceContracts.ViewModels;
|
||
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 Lab3Form
|
||
{
|
||
public partial class FormStudent : Form
|
||
{
|
||
public int? _id;
|
||
private readonly IStudentLogic _logic;
|
||
private readonly IFormatLogic _formatLogic;
|
||
private List<FormatViewModel> _Formats;
|
||
public int Id { set { _id = value; } }
|
||
|
||
public FormStudent(IStudentLogic logic, IFormatLogic formatLogic)
|
||
{
|
||
InitializeComponent();
|
||
|
||
_logic = logic;
|
||
_formatLogic = formatLogic;
|
||
_Formats = new List<FormatViewModel>();
|
||
_Formats = _formatLogic.ReadList(null);
|
||
var cityNames = _Formats.Select(city => city.Name).ToList();
|
||
customListBox1.PopulateListBox(cityNames);
|
||
var orderStatuses = new List<string> { "Очное", "Заочное", "Очно-заочное", "Дистанционное" };
|
||
listBox1.Items.AddRange(orderStatuses.ToArray());
|
||
DateTime now = DateTime.Now;
|
||
customInputRangeDate1.MinDate = now.AddYears(-6);
|
||
customInputRangeDate1.MaxDate = now;
|
||
this.Load += FormOrder_Load;
|
||
}
|
||
|
||
private void FormOrder_Load(object sender, EventArgs e)
|
||
{
|
||
if (_id.HasValue)
|
||
{
|
||
try
|
||
{
|
||
StudentSearchModel searchModel = new StudentSearchModel { Id = _id.Value };
|
||
StudentViewModel orderViewModel = _logic.ReadElement(searchModel);
|
||
|
||
if (orderViewModel != null)
|
||
{
|
||
textBoxFIO.Text = orderViewModel.Fullname;
|
||
|
||
FormatViewModel selectedCity = _Formats.FirstOrDefault(city => city.Id == orderViewModel.FormatId);
|
||
if (selectedCity != null)
|
||
{
|
||
customListBox1.SelectedItem = selectedCity.Name;
|
||
}
|
||
|
||
customInputRangeDate1.Date = orderViewModel.AdmissionDate;
|
||
|
||
foreach (string status in orderViewModel.AverageScore)
|
||
{
|
||
int index = listBox1.Items.IndexOf(status);
|
||
if (index != -1)
|
||
{
|
||
listBox1.SetSelected(index, true);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Студент с указанным ID не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void buttonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(textBoxFIO.Text))
|
||
{
|
||
MessageBox.Show("Заполните ФИО студента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
if (customListBox1.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Укажите форму обучения", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
try
|
||
{
|
||
var model = new StudentBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
Fullname = textBoxFIO.Text,
|
||
FormatId = _Formats.First(x => x.Name == customListBox1.SelectedItem).Id,
|
||
AdmissionDate = customInputRangeDate1.Date,
|
||
AverageScore = listBox1.SelectedItems.Cast<string>().ToList(),
|
||
};
|
||
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.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);
|
||
}
|
||
}
|
||
}
|
||
}
|