82 lines
2.6 KiB
C#

using ProjectOpticsSalon.Entites;
using ProjectOpticsSalon.Repositories;
using ProjectOpticsSalon.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 ProjectOpticsSalon.Forms
{
public partial class FormLens : Form
{
public readonly ILensRepository _lensRepository;
private int? _lensId;
public int Id
{
set
{
try
{
var lens = _lensRepository.ReadLensById(value);
if (lens != null)
{
throw new InvalidDataException(nameof(lens));
}
maskedTextBoxDioptres.Text = lens.Dioptres.ToString();
maskedTextBoxAstigmatism.Text = lens.Astigmatism.ToString();
_lensId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormLens(ILensRepository lensRepository)
{
InitializeComponent();
_lensRepository = lensRepository ?? throw new ArgumentNullException(nameof(lensRepository));
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(maskedTextBoxDioptres.Text) ||
string.IsNullOrEmpty(maskedTextBoxAstigmatism.Text))
{
throw new Exception("Имеются незаполненные поля");
}
if (_lensId.HasValue)
{
_lensRepository.UpdateLens(CreateLens(_lensId.Value));
}
else
{
_lensRepository.CreateLens(CreateLens(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e) => Close();
private Lens CreateLens(int id) => Lens.CreateLens(id,
(float)Convert.ToDouble(maskedTextBoxDioptres.Text), (float)Convert.ToDouble(maskedTextBoxAstigmatism.Text));
}
}