102 lines
3.5 KiB
C#
102 lines
3.5 KiB
C#
using Microsoft.VisualBasic.FileIO;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Atelier.Forms
|
|
{
|
|
public partial class FormModel : Form
|
|
{
|
|
private readonly IModelRepository _modelRepository;
|
|
private int? _modelId;
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var model = _modelRepository.ReadModelById(value);
|
|
if (model == null)
|
|
{
|
|
throw new
|
|
InvalidDataException(nameof(model));
|
|
}
|
|
comboBoxModel.SelectedItem = model.ModelType;
|
|
numericUpDownPrice.Value = (decimal)model.Price;
|
|
_modelId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
public FormModel(IModelRepository modelRepository, IFabricRepository fabricRepository)
|
|
{
|
|
InitializeComponent();
|
|
_modelRepository = modelRepository ??
|
|
throw new ArgumentNullException(nameof(modelRepository));
|
|
comboBoxModel.DataSource = Enum.GetValues(typeof(ModelType));
|
|
ColumnFabric.DataSource = fabricRepository.ReadFabrics();
|
|
ColumnFabric.DisplayMember = "FabricType";
|
|
ColumnFabric.ValueMember = "Id";
|
|
|
|
}
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (numericUpDownPrice.Value < 500 || dataGridView1.RowCount < 1 || comboBoxModel.SelectedIndex < 1)
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (_modelId.HasValue)
|
|
{
|
|
_modelRepository.UpdateModel(CreateModel(_modelId.Value));
|
|
}
|
|
else
|
|
{
|
|
_modelRepository.CreateModel(CreateModel(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
|
|
private Model CreateModel(int id)
|
|
{
|
|
List<FabricModel> fabricModels = new List<FabricModel>();
|
|
|
|
foreach (DataGridViewRow row in dataGridView1.Rows)
|
|
{
|
|
if (row.Cells["ColumnFabric"].Value == null || row.Cells["ColumnCount"].Value == null || !(bool)row.Cells["CheckBoxColumn"].Value)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
int fabricId = Convert.ToInt32(row.Cells["ColumnFabric"].Value);
|
|
int count = Convert.ToInt32(row.Cells["ColumnCount"].Value);
|
|
|
|
fabricModels.Add(FabricModel.CreateElement(0, fabricId, count));
|
|
}
|
|
|
|
return Model.CreateEntity(id, (ModelType)comboBoxModel.SelectedItem!, Convert.ToDouble(numericUpDownPrice.Value), fabricModels);
|
|
}
|
|
}
|
|
}
|
|
|